- CALayer的基本属性
- CALayer和UIView的关系
- position和anchorPoint的作用
其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层
在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView的layer属性可以访问这个层
@property(nonatomic,readonly,retain) CALayer *layer;
当UIView需要显示到屏幕上时,会调用drawRect:方法进行绘图,并且会将所有内容绘制在自己的图层上,绘图完毕后,系统会将图层拷贝到屏幕上,于是就完成了UIView的显示
换句话说,UIView本身不具备显示的功能,是它内部的层才有显示功能
阴影
圆角大小
边框宽度和颜色
… …
还可以给图层添加动画,来实现一些比较炫酷的效果
边框宽度
@property CGFloat borderWidth;
圆角半径
@property CGColorRef borderColor;
内容(比如设置为图片CGImageRef)
@property(retain) id contents;
//
// ViewController.m
// CALayerTest
//
// Created by hellovoidworld on 15/1/14.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *orangeView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // [self testBasicLayer]; // [self testOrangeView]; // [self testImageView]; [self testTransform];
} /** 基本属性 */
- (void) testBasicLayer {
// 创建一个图层
CALayer *layer = [[CALayer alloc] init];
// 设置图层边框颜色
layer.borderColor = [UIColor blueColor].CGColor;
// 设置图层边框粗细,边框是占据view内的尺寸的
layer.borderWidth = ;
//
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor redColor].CGColor;
layer.cornerRadius = ;
// layer.masksToBounds = YES; // UIImage *image = [UIImage imageNamed:@"M3Mini"];
// layer.contents = (id)image.CGImage; layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOffset = CGSizeMake(, );
layer.shadowOpacity = 0.5; [self.view.layer addSublayer:layer];
} /** UIView的圆角+阴影 */
- (void) testOrangeView {
self.orangeView.layer.shadowColor = [UIColor blackColor].CGColor;
self.orangeView.layer.shadowOffset = CGSizeMake(, );
self.orangeView.layer.shadowOpacity = 0.5;
self.orangeView.layer.cornerRadius = ; self.orangeView.layer.borderWidth = ;
} /** ImageView的圆角+阴影效果 */
- (void) testImageView {
// 用于产生阴影的辅助view,作为父view,显示在底层
UIView *shadowView = [[UIView alloc] init];
shadowView.frame = CGRectMake(, , , );
shadowView.layer.cornerRadius = ;
shadowView.backgroundColor = [UIColor redColor]; // 产生阴影
shadowView.layer.shadowColor = [UIColor blueColor].CGColor;
shadowView.layer.shadowOffset = CGSizeMake(, );
shadowView.layer.shadowOpacity = 0.7; // 用于显示圆角内容的imageView,作为子view,显示在表层
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"M3Mini"]];
imageView.frame = shadowView.bounds;
imageView.layer.cornerRadius = shadowView.layer.cornerRadius ; // 圆角裁剪
imageView.layer.masksToBounds = YES; // “合成”两个view
[shadowView addSubview:imageView]; // 添加到屏幕
[self.view addSubview:shadowView];
} /** 3D transform */
- (void) testTransform {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"M3Mini"]];
imageView.frame = CGRectMake(, , , ); // 1.从3D的x、y、z轴三个向量的缩放
// imageView.layer.transform = CATransform3DMakeScale(2, 2, 0); // 2.沿着x=1,y=1,z=0的向量为轴旋转
// imageView.layer.transform = CATransform3DMakeRotation(M_PI_4, 1, 1, 0); // 3.使用key paths设置
// 3.1使用整个transform对象来设置,3D向量旋转
// NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, 1, 0)];
// [imageView.layer setValue:value forKeyPath:@"transform"]; // 3.2直接利用rotation来设置,2D旋转(跟view的transform旋转一样)
[imageView.layer setValue:@(M_PI_4) forKeyPath:@"transform.rotation"]; [self.view addSubview:imageView];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
- (void) testImageView {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"M3Mini"]];
imageView.frame = CGRectMake(, , , );
imageView.layer.cornerRadius = ;
imageView.layer.masksToBounds = YES; imageView.layer.shadowColor = [UIColor blueColor].CGColor;
imageView.layer.shadowOffset = CGSizeMake(, );
imageView.layer.shadowOpacity = 0.7; [self.view addSubview:imageView];
}
- (void) testImageView {
// 用于产生阴影的辅助view,作为父view,显示在底层
UIView *shadowView = [[UIView alloc] init];
shadowView.frame = CGRectMake(, , , );
shadowView.layer.cornerRadius = ;
shadowView.backgroundColor = [UIColor redColor]; // 产生阴影
shadowView.layer.shadowColor = [UIColor blueColor].CGColor;
shadowView.layer.shadowOffset = CGSizeMake(, );
shadowView.layer.shadowOpacity = 0.7; // 用于显示圆角内容的imageView,作为子view,显示在表层
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"M3Mini"]];
imageView.frame = shadowView.bounds;
imageView.layer.cornerRadius = shadowView.layer.cornerRadius ; // 圆角裁剪
imageView.layer.masksToBounds = YES; // “合成”两个view
[shadowView addSubview:imageView]; // 添加到屏幕
[self.view addSubview:shadowView];
}
CALayer是定义在QuartzCore框架中的
CGImageRef、CGColorRef两种数据类型是定义在CoreGraphics框架中的
UIColor、UIImage是定义在UIKit框架中的
其次
QuartzCore框架和CoreGraphics框架是可以跨平台使用的,在iOS和Mac OS X上都能使用
但是UIKit只能在iOS中使用
为了保证可移植性,QuartzCore不能使用UIImage、UIColor,只能使用CGImageRef、CGColorRef
既然CALayer和UIView都能实现相同的显示效果,那究竟该选择谁好呢?
其实,对比CALayer,UIView多了一个事件处理的功能。也就是说,CALayer不能处理用户的触摸事件,而UIView可以
所以,如果显示出来的东西需要跟用户进行交互的话,用UIView;如果不需要跟用户进行交互,用UIView或者CALayer都可以
当然,CALayer的性能会高一些,因为它少了事件处理的功能,更加轻量级
@property CGPoint position;
用来设置CALayer在父层中的位置
以父层的左上角为原点(0, 0)
@property CGPoint anchorPoint;
称为“定位点”、“锚点”
决定着CALayer身上的哪个点会在position属性所指的位置
以自己的左上角为原点(0, 0)
它的x、y取值范围都是0~1,默认值为(0.5, 0.5)
CALayer *layer = [[CALayer alloc] init];
layer.backgroundColor = [UIColor redColor].CGColor;
layer.frame = CGRectMake(, , , );
layer.position = CGPointMake(, );
// anchorPoint默认是(0.5,0.5),即中心点
layer.anchorPoint = CGPointMake(,); [self.view.layer addSublayer:layer];
所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动画
什么是隐式动画?
当对非Root Layer的部分属性进行修改时,默认会自动产生一些动画效果
而这些属性称为Animatable Properties(可动画属性)
列举几个常见的Animatable Properties:
bounds:用于设置CALayer的宽度和高度。修改这个属性会产生缩放动画
backgroundColor:用于设置CALayer的背景色。修改这个属性会产生背景色的渐变动画
position:用于设置CALayer的位置。修改这个属性会产生平移动画
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.myview.layer.position = CGPointMake(, );
[CATransaction commit];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 缩放
self.layer.frame = CGRectMake(self.layer.position.x, self.layer.position.y, , );
// 位移
self.layer.position = CGPointMake(, );
// 背景色
self.layer.backgroundColor = [UIColor blueColor].CGColor;
}
//
// HVWLayer.m
// CALayerTest
//
// Created by hellovoidworld on 15/1/14.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWLayer.h" @implementation HVWLayer /**
* 必须调用一次layer的setNeedDisplay方法,才能触发此方法
*/
- (void)drawInContext:(CGContextRef)ctx {
NSLog(@"%@ - drawInContext", [self class]); // 绿色
CGContextSetRGBFillColor(ctx, , , , );
// 矩形
CGContextFillRect(ctx, CGRectMake(, , , ));
CGContextFillPath(ctx);
} @end
- (void) diyLayer1 {
HVWLayer *layer = [[HVWLayer alloc] init];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor grayColor].CGColor;
// 必须调用重绘,才能触发layer的drawInContent:方法
[layer setNeedsDisplay]; [self.view.layer addSublayer:layer];
}
- (void) diyLayer2 {
CALayer *layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(, , , );
layer.anchorPoint = CGPointZero;
layer.backgroundColor = [UIColor grayColor].CGColor;
layer.delegate = self; // 必须要进行重绘
[layer setNeedsDisplay]; [self.view.layer addSublayer:layer];
} #pragma mark - layer代理方法
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
// 绿色
CGContextSetRGBFillColor(ctx, , , , );
// 矩形
CGContextFillRect(ctx, CGRectMake(, , , ));
CGContextFillPath(ctx);
}
- view准备一个图层上下文Layer Context Ref
- 调用[view.layer.delegate drawLayer:inContext:],传入上下文
- 在drawLayer:inContext:内会调用view的drawRect:方法
- 所以drawRect:内实现的绘图方法,会画到layer上
- 系统把layer上的内容描绘到屏幕,完成view的显示
//
// HVWView.m
// CALayerTest
//
// Created by hellovoidworld on 15/1/14.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWView.h" @implementation HVWView - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
NSLog(@"%@ - drawLayer", [self class]);
[super drawLayer:layer inContext:ctx];
} - (void)drawRect:(CGRect)rect {
NSLog(@"%@ - drawRect", [self class]);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, , , , );
CGContextFillEllipseInRect(ctx, CGRectMake(, , , ));
CGContextFillPath(ctx);
} @end
//
// HVWLayer.m
// CALayerTest
//
// Created by hellovoidworld on 15/1/14.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWLayer.h" @implementation HVWLayer - (void)drawInContext:(CGContextRef)ctx {
NSLog(@"%@ - drawInContext", [self class]); // 绿色
CGContextSetRGBFillColor(ctx, , , , );
// 矩形
CGContextFillRect(ctx, CGRectMake(, , , ));
CGContextFillPath(ctx);
} @end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 创建view
HVWView *hvwView = [[HVWView alloc] init];
hvwView.backgroundColor = [UIColor grayColor];
hvwView.frame = CGRectMake(, , , ); // 创建layer
HVWLayer *layer = [[HVWLayer alloc] init];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor blueColor].CGColor; // 必须要重绘才能调用layer的drawInContext
[layer setNeedsDisplay]; // 配置layer到view,会设置layer的delegate就是那个view
[hvwView.layer addSublayer:layer]; [self.view addSubview:hvwView];
// [self.view.layer addSublayer:layer];
}
2015-01-14 21:59:45.560 CALayerTest3[3246:115305] HVWView - drawRect