CALayer的简单使用
原文地址:http://www.raywenderlich.com/2502/calayers-tutorial-for-ios-introduction-to-calayers-tutorial
如果你已经在iPhone上做过开发,你可能对UIView和它的子类-Button,text,slider等,非常熟悉。
![](/assets/blank.gif)
什么是层?
开始
- // Import QuartzCore.h at the top of the file
- #import <QuartzCore/QuartzCore.h>
- // Uncomment viewDidLoad and add the following lines
- self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
- self.view.layer.cornerRadius = 20.0;
- self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);
![](/assets/blank.gif)
层和子层
- CALayer *sublayer = [CALayer layer];
- sublayer.backgroundColor = [UIColor blueColor].CGColor;
- sublayer.shadowOffset = CGSizeMake(0, 3);
- sublayer.shadowRadius = 5.0;
- sublayer.shadowColor = [UIColor blackColor].CGColor;
- sublayer.shadowOpacity = 0.8;
- sublayer.frame = CGRectMake(30, 30, 128, 192);
- [self.view.layer addSublayer:sublayer];
![](/assets/blank.gif)
设置层的图像内容
- sublayer.contents = (id) [UIImage imageNamed:@"BattleMapSplashScreen.jpg"].CGImage;
- sublayer.borderColor = [UIColor blackColor].CGColor;
- sublayer.borderWidth = 2.0;
![](/assets/blank.gif)
圆角半径和图片内容的注意点
- CALayer *sublayer = [CALayer layer];
- sublayer.backgroundColor = [UIColor blueColor].CGColor;
- sublayer.shadowOffset = CGSizeMake(0, 3);
- sublayer.shadowRadius = 5.0;
- sublayer.shadowColor = [UIColor blackColor].CGColor;
- sublayer.shadowOpacity = 0.8;
- sublayer.frame = CGRectMake(30, 30, 128, 192);
- sublayer.borderColor = [UIColor blackColor].CGColor;
- sublayer.borderWidth = 2.0;
- sublayer.cornerRadius = 10.0;
- [self.view.layer addSublayer:sublayer];
- CALayer *imageLayer = [CALayer layer];
- imageLayer.frame = sublayer.bounds;
- imageLayer.cornerRadius = 10.0;
- imageLayer.contents = (id) [UIImage imageNamed:@"BattleMapSplashScreen.jpg"].CGImage;
- imageLayer.masksToBounds = YES;
- [sublayer addSublayer:imageLayer];
![](/assets/blank.gif)
层和自定义绘画内容
- CALayer *customDrawn = [CALayer layer];
- customDrawn.delegate = self;
- customDrawn.backgroundColor = [UIColor greenColor].CGColor;
- customDrawn.frame = CGRectMake(30, 250, 128, 40);
- customDrawn.shadowOffset = CGSizeMake(0, 3);
- customDrawn.shadowRadius = 5.0;
- customDrawn.shadowColor = [UIColor blackColor].CGColor;
- customDrawn.shadowOpacity = 0.8;
- customDrawn.cornerRadius = 10.0;
- customDrawn.borderColor = [UIColor blackColor].CGColor;
- customDrawn.borderWidth = 2.0;
- customDrawn.masksToBounds = YES;
- [self.view.layer addSublayer:customDrawn];
- [customDrawn setNeedsDisplay];
- void MyDrawColoredPattern (void *info, CGContextRef context) {
- CGColorRef dotColor = [UIColor colorWithHue:0 saturation:0 brightness:0.07 alpha:1.0].CGColor;
- CGColorRef shadowColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1].CGColor;
- CGContextSetFillColorWithColor(context, dotColor);
- CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 1, shadowColor);
- CGContextAddArc(context, 3, 3, 4, 0, radians(360), 0);
- CGContextFillPath(context);
- CGContextAddArc(context, 16, 16, 4, 0, radians(360), 0);
- CGContextFillPath(context);
- }
- - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
- CGColorRef bgColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
- CGContextSetFillColorWithColor(context, bgColor);
- CGContextFillRect(context, layer.bounds);
- static const CGPatternCallbacks callbacks = { 0, &MyDrawColoredPattern, NULL };
- CGContextSaveGState(context);
- CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
- CGContextSetFillColorSpace(context, patternSpace);
- CGColorSpaceRelease(patternSpace);
- CGPatternRef pattern = CGPatternCreate(NULL,
- layer.bounds,
- CGAffineTransformIdentity,
- 24,
- 24,
- kCGPatternTilingConstantSpacing,
- true,
- &callbacks);
- CGFloat alpha = 1.0;
- CGContextSetFillPattern(context, pattern, &alpha);
- CGPatternRelease(pattern);
- CGContextFillRect(context, layer.bounds);
- CGContextRestoreGState(context);
- }
![](/assets/blank.gif)
将来
CALayer的简单使用相关推荐
- CALayer 一些简单的小例子
//设定calayer self.view.layer.backgroundColor=[UIColor orangeColor].CGColor; self.view.layer.c ...
- iOS核心动画之CALayer(1)
本文目录 一.什么是CALayer 二.CALayer的简单使用 回到顶部 一.什么是CALayer * 在iOS系统中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个 ...
- iOS - CALayer 绘图层
1.CALayer 绘图层 在 iOS 系统中,你能看得见摸得着的东西基本上都是 UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是 UIView.其实 UIView 之 ...
- IOS--CALayer的介绍及使用技巧
2019独角兽企业重金招聘Python工程师标准>>> 首先对CALayer进行简单的介绍: 1.在UIView中,CALayer只是一个类的声明,因此需要添加 QuartzCore ...
- CALayer 详解 -----转自李明杰
本文目录 一.什么是CALayer 二.CALayer的简单使用 回到顶部 一.什么是CALayer * 在iOS系统中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个 ...
- Core Animation之多种动画效果
前面介绍了Core Animation基础知识,还有CALayer的简单使用,最终还是有要动画的滴,这里列出几个动画效果,参考下能加深对Core Animation的认识和理解 1.把图片移到右下角变 ...
- CALayer 简单的使用
转:http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial If you've been programming for ...
- 1小时学会:最简单的iOS直播推流(三)使用系统接口捕获音视频数据
最简单的iOS 推流代码,视频捕获,软编码(faac,x264),硬编码(aac,h264),美颜,flv编码,rtmp协议,陆续更新代码解析,你想学的知识这里都有,愿意懂直播技术的同学快来看!! 源 ...
- 【动画2】CALayer动画
一)UIView动画 二)CoreAnimation动画 前言:上一篇已经介绍了UIKit给我们封装好的UIView动画的使用,UIKit动画是建立在CoreAnimation动画之上的,CoreAn ...
最新文章
- 第十三周项目二-动物这样叫(3)
- 修改电脑开机时间记录6005_这些优化步骤,让你的电脑远离卡顿!
- [Apple开发者帐户帮助]二、管理你的团队(6)找到您的团队ID
- Java LinkedList指南
- activeMQ的三种通讯模式
- java手机号判断运营商_用Java对手机号所属运营商进行判断
- anaconda下载的python在哪_Anaconda下Python环境下载及安装
- 面向对象类的使用(15)
- Linux64位steam,这下没得玩了! Steam无奈抛弃Linux用户
- 运行时数据区——Java虚拟机栈
- python刷leetcode_零基础python刷leetcode -- 1. Two Sum
- 《OCA认证考试指南(1Z0-061):Oracle Database 12c SQL基础》
- 网络中的网络 NiN 动手学深度学习v2 pytorch
- 微信小程序抽奖转盘实现案例
- Springboot 返回数据提示语 国际化 (AOP实现)
- 美团买菜助手来了,自动点击,助你买菜
- WebGoat攻略 for Mac(1)
- ui界面设计是什么:ui设计常用软件
- ppt中加载html,PPT页面中插入浏览器方法 PPT中插入浏览器教程-PPT家园
- 职场上情商高的人有什么特征,盘点职场上情商高的人必做的3件事