iOS开发UI篇—CAlayer(自定义layer)
iOS开发UI篇—CAlayer(自定义layer)
一、第一种方式
1.简单说明
以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的DrawRect:方法,然后在该方法中画图。


1 //
2 // YYMylayer.m
3 // 05-自定义layer(1)
4 //
5 // Created by apple on 14-6-21.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYMylayer.h"
10
11 @implementation YYMylayer
12 //重写该方法,在该方法内绘制图形
13 -(void)drawInContext:(CGContextRef)ctx
14 {
15 //1.绘制图形
16 //画一个圆
17 CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
18 //设置属性(颜色)
19 // [[UIColor yellowColor]set];
20 CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
21
22 //2.渲染
23 CGContextFillPath(ctx);
24 }
25 @end


1 //
2 // YYViewController.m
3 // 05-自定义layer(1)
4 //
5 // Created by apple on 14-6-21.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "YYMylayer.h"
11
12 @interface YYViewController ()
13
14 @end
15
16 @implementation YYViewController
17
18 - (void)viewDidLoad
19 {
20 [super viewDidLoad];
21
22 //1.创建自定义的layer
23 YYMylayer *layer=[YYMylayer layer];
24 //2.设置layer的属性
25 layer.backgroundColor=[UIColor brownColor].CGColor;
26 layer.bounds=CGRectMake(0, 0, 200, 150);
27 layer.anchorPoint=CGPointZero;
28 layer.position=CGPointMake(100, 100);
29 layer.cornerRadius=20;
30 layer.shadowColor=[UIColor blackColor].CGColor;
31 layer.shadowOffset=CGSizeMake(10, 20);
32 layer.shadowOpacity=0.6;
33
34 [layer setNeedsDisplay];
35 //3.添加layer
36 [self.view.layer addSublayer:layer];
37
38 }
39
40 @end



1 #import "YYVIEW.h"
2
3 @implementation YYVIEW
4
5
6 - (void)drawRect:(CGRect)rect
7 {
8 //1.获取上下文
9 CGContextRef ctx=UIGraphicsGetCurrentContext();
10 //2.绘制图形
11 CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
12 //设置属性(颜色)
13 // [[UIColor yellowColor]set];
14 CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
15
16 //3.渲染
17 CGContextFillPath(ctx);
18 //在执行渲染操作的时候,本质上它的内部相当于调用了下面的方法
19 [self.layer drawInContext:ctx];
20 }

说明:在UIView中绘制图形,获取的上下文就是这个view对应的layer的上下文。在渲染的时候,就是把图形渲染到对应的layer上。
在执行渲染操作的时候,本质上它的内部相当于执行了 [self.layer drawInContext:ctx];
二、第二种方式
方法描述:设置CALayer的delegate,然后让delegate实现drawLayer:inContext:方法,当CALayer需要绘图时,会调用delegate的drawLayer:inContext:方法进行绘图。
代码示例:

1 //
2 // YYViewController.m
3 // 06-自定义layer(2)
4 //
5 // Created by apple on 14-6-21.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7
8 #import "YYViewController.h"
9 @interface YYViewController ()
10 @end
11
12 @implementation YYViewController
13
14 - (void)viewDidLoad
15 {
16 [super viewDidLoad];
17 //1.创建自定义的layer
18 CALayer *layer=[CALayer layer];
19 //2.设置layer的属性
20 layer.backgroundColor=[UIColor brownColor].CGColor;
21 layer.bounds=CGRectMake(0, 0, 200, 150);
22 layer.anchorPoint=CGPointZero;
23 layer.position=CGPointMake(100, 100);
24 layer.cornerRadius=20;
25 layer.shadowColor=[UIColor blackColor].CGColor;
26 layer.shadowOffset=CGSizeMake(10, 20);
27 layer.shadowOpacity=0.6;
28
29 //设置代理
30 layer.delegate=self;
31 [layer setNeedsDisplay];
32 //3.添加layer
33 [self.view.layer addSublayer:layer];
34 }
35
36 -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
37 {
38 //1.绘制图形
39 //画一个圆
40 CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
41 //设置属性(颜色)
42 // [[UIColor yellowColor]set];
43 CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
44
45 //2.渲染
46 CGContextFillPath(ctx);
47 }
48 @end

实现效果:
注意点:不能再将某个UIView设置为CALayer的delegate,因为UIView对象已经是它内部根层的delegate,再次设置为其他层的delegate就会出问题。

iOS开发UI篇—CAlayer(自定义layer)的更多相关文章
- iOS开发UI篇—CAlayer层的属性
iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...
- iOS开发UI篇—CAlayer(创建图层)
iOS开发UI篇—CAlayer(创建图层) 一.添加一个图层 添加图层的步骤: 1.创建layer 2.设置layer的属性(设置了颜色,bounds才能显示出来) 3.将layer添加到界面上(控 ...
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
- iOS开发UI篇—Quartz2D(自定义UIImageView控件)
iOS开发UI篇—Quartz2D(自定义UIImageView控件) 一.实现思路 Quartz2D最大的用途在于自定义View(自定义UI控件),当系统的View不能满足我们使用需求的时候,自定义 ...
- IOS开发UI篇之──自定义UIActionSheet
转载自:http://www.cnblogs.com/pengyingh/articles/2343200.html UIActionSheet类系IOS开发中实现警告框的重要的类,而在好多应用中,都 ...
- iOS开发UI篇—CALayer
一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能显示在屏幕上,完全 ...
- iOS开发UI 篇—CAlayer层的属性
一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设 ...
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
本文转载至 http://blog.csdn.net/xunyn/article/details/8064984 原文地址http://www.189works.com/article-89289 ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
随机推荐
- 自己对js对原型链的理解
js对象分为2种 函数对象和普通对象 函数对象 比如 function Show(){}var x=function Show2(){}var b=new Function("show3&q ...
- GCD总结
//用block只有两种:同步执行/异步执行(参数1:队列;参数二:任务) dispatch_async(dispatch_get_global_queue(0, 0),^{ });//异步在新的线程 ...
- Git学习笔记
1.第一次提交文件的过程: 1)创建代码库:cd到要作为git代码库的文件夹下,执行git init命令. 2)设置邮箱和用户名:设置后才可提交代码,git config user.e ...
- Linux命令-自动挂载文件/etc/fstab功能详解[转]
一./etc/fstab文件的作用 磁盘被手动挂载之后都必须把挂载信息写入/etc/fstab这个文件中,否则下次开机启动时仍然需要重新挂载. 系统开机时会主动读取/etc/fstab这个文件中的内容 ...
- 处理Assetbundle依赖关系时想到的一道题
在处理unit3d的assetbundle依赖关系的时候,想到了一道有趣的题目: 给定一堆数据,例如{A = {1, 3, 4}, B = {3, 4}, C = {5, 6}, D = {6, 7, ...
- NSIS检测.NET Framework并在线下载
Section -.NET Framework ;检测是否是需要的.NET Framework版本 Call GetNetFrameworkVersion Pop $R1 ;${If} $R1 < ...
- 给Excel2013添加WebADI的Oracle加载项
大家都知道,在Excel2013的加载项中是找不到WebADI的加载项的,EBS貌似有一个补丁,这里讲手动设置的步骤: 打开一个下载的WebADI的模板: 依次打开菜单: 文件>选项>自定 ...
- 搭建Apache Web服务器
1.下载Apache服务器的安装包 地址:http://httpd.apache.org/download.cgi 从http://archive.apache.org/dist/httpd/bina ...
- bind绑定参数
const curry = (fn) => (...args)=>fn.bind(null,...args); const split = curry((splitOn, str) =&g ...
- mfc对话询问窗体
// 显示消息对话框 nRes = MessageBox(_T("您确定要跳转网站?"), _T("提示"), MB_OKCANCEL | MB_I ...