一.第一种方式
1.简单说明
以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的DrawRect:方法,然后在该方法中画图。
绘制圆形的步骤:
(1)获取上下文
(2)绘制圆形
(3)渲染圆形
DBMyLayer.m文件
@implementation DBMyLayer
//重写该方法,在该方法内绘制圆形
- (void)drawInContext:(CGContextRef)ctx{
//画一个圆
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
//设置属性
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//渲染
CGContextFillPath(ctx);
}
@end
在控制器中,创建一个自定义的类
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
DBMyLayer *layer = [DBMyLayer layer];
layer.backgroundColor = [UIColor brownColor].CGColor;
layer.bounds = CGRectMake(0, 0, 200, 150);
layer.anchorPoint = CGPointZero;
layer.position = CGPointMake(100, 100);
layer.cornerRadius = 20;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOffset = CGSizeMake(10, 20);
layer.shadowOpacity = 0.6;
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
注意点:
(1)默认为无色,不会显示。要想让绘制的图形显示出来,还需要设置圆形的颜色。注意不能直接使用UI框架中的类。
(2)在自定义layer中的-(void)drawInContext:方法不会自动调用,只能自己通过setNeedDisplay方法调用。在view中画东西drawRect:方法在view第一次显示的时候会自动调用。
2.拓展
UIView中绘图说明
@implementation DBMyView
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.绘制圆形
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
//设置颜色
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//3.渲染
CGContextFillPath(ctx);
//在执行渲染操作的时候,本质上它的内部相当于调用了下面的方法
//[self.layer drawInContext:ctx];
}
@end
说明:
在UIView中绘制图形,获取的上下文就是这个view对应的layer的上下文。在渲染的时候,就是把圆形渲染到对应的layer上。
在执行渲染操作的时候,本质上它的内部相当于执行了 [self.layer drawInContext:ctx];
二.第二种方式
方法描述:设置CALayer的delegate,然后让delegate实现drawLayer:inContext:方法,当 CALayer需要绘制时,会调用delegate的drawLayer:inContext:方法进行绘制。
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建自定义的layer
CALayer *layer = [CALayer layer];
//设置layer属性
layer.backgroundColor = [UIColor brownColor].CGColor;
layer.bounds = CGRectMake(0, 0, 200, 200);
layer.anchorPoint = CGPointZero;
layer.position = CGPointMake(100, 100);
layer.cornerRadius = 20;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOffset = CGSizeMake(10, 20);
layer.shadowOpacity = 0.6;
//设置代理
layer.delegate = self;
[layer setNeedsDisplay];
//添加layer
[self.view.layer addSublayer:layer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark CALayer Delegate
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
//画一个圆形
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
//设置属性
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//渲染
CGContextFillPath(ctx);
}
@end
注意点:
不能再将某个UIView设置为CALayer的delegate,因为UIView对象已经是它内部根层的delegate,再次设置为其他层的delegate就会出问题。
在设置代理的时候,它并不要求我们遵守协议,说明这个方法是NSObject中的,就不需要在额外的显示遵守协议了。
提示:
以后如果要设置某个类的代理,但是这个代理没有要求我们遵守什么特定的协议,那么可以认为这个协议方法是NSObject里边的。
三.补充说明
1.无论采取哪种方法来自定义层,都必须调用CALayer的setNeedsDisplay方法才能正常绘图。
2.详细实现过程:
当UIView需要显示时,它内部的层会准备好一个CGContextRef(图形上下文),然后调用delegate(这里就是UIView)的drawLayer:inContext:方法,并且传入已经准备好的CGContextRef对象。而UIView在drawLayer:inContext:方法中又会调用自己的drawRect:方法。平时在drawRect:中通过UIGraphicsGetCurrentContext()获取的就是由层传入的CGContextRef对象,在drawRect:中完成的所有绘图都会填入层的CGContextRef中,然后被拷贝至屏幕。