// 邊框?qū)挾?self.purpleView.layer.borderWidth = 10;
// 邊框顏色
self.purpleView.layer.borderColor = [UIColor greenColor].CGColor;
// 圓角
self.purpleView.layer.cornerRadius = 10;
// self.purpleView.layer.masksToBounds = YES;
// 陰影顏色
self.purpleView.layer.shadowColor = [UIColor blueColor].CGColor;
// 陰影偏差
self.purpleView.layer.shadowOffset = CGSizeMake(20, 20);
// 陰影不透明度
self.purpleView.layer.shadowOpacity = 0.5;
// 平移
self.iconView.layer.transform = CATransform3DMakeScale(1.5, 0.5, 0);
// 旋轉(zhuǎn)二維
self.iconView.transform = CGAffineTransformMakeRotation(M_PI_4);
// 旋轉(zhuǎn)三維 方式一
self.iconView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
// 旋轉(zhuǎn)三維 方式二
NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
[self.iconView.layer setValue:value forKeyPath:@"transform"];
// 旋轉(zhuǎn)
[self.iconView.layer setValue:@(M_PI_2) forKeyPath:@"transform.rotation"];
self.iconView.layer.transform = CATransform3DMakeScale(0.5, 2, 0);
[self.iconView.layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 2, 0)] forKeyPath:@"transform"];
// 可以傳遞哪些key path, 在官方文檔搜索 "CATransform3D key paths"
[self.iconView.layer setValue:@(-100) forKeyPath:@"transform.translation.x"];
新建圖層
// CALayer *layer = [[CALayer alloc] init];
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor redColor].CGColor;
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.position = CGPointMake(200, 100);
layer.cornerRadius = 10;
layer.masksToBounds = YES;
layer.contents = (id)[UIImage imageNamed:@"icon"].CGImage;
[self.view.layer addSublayer:layer];
自定義圖層
方式一
繼承自CALayer
/**
* 只有明顯地調(diào)用setNeedsDisplay方法,才會調(diào)用drawInContext:方法進(jìn)行繪制
*/
- (void)drawInContext:(CGContextRef)ctx
{
// 紅色
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
// 添加圓
CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));
// 實(shí)心繪制
CGContextFillPath(ctx);
}
自定義View是這樣的
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 紅色
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
// 添加圓
CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));
// 實(shí)心繪制
CGContextFillPath(ctx);
}
MyLayer *layer = [MyLayer layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.backgroundColor = [UIColor blueColor].CGColor;
layer.anchorPoint = CGPointZero;
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
方式二
CALayer *layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.backgroundColor = [UIColor blackColor].CGColor;
layer.anchorPoint = CGPointZero;
layer.position = CGPointMake(100, 100);
layer.delegate = self;
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
#pragma mark - 圖層的代理方法
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextAddRect(ctx, CGRectMake(0, 0, 20, 20));
CGContextFillPath(ctx);
}
view的完整顯示過程
- view.layer會準(zhǔn)備一個(gè)Layer Graphics Contex(圖層類型的上下文)
- 調(diào)用view.layer.delegate(view)的drawLayer:inContext:,并傳入剛才準(zhǔn)備好的上下文
- view的drawLayer:inContext:方法內(nèi)部又會調(diào)用view的drawRect:方法
- view就可以在drawRect:方法中實(shí)現(xiàn)繪圖代碼, 所有東西最終都繪制到view.layer上面
- 系統(tǒng)再將view.layer的內(nèi)容拷貝到屏幕, 于是完成了view的顯示