一、CALayer
- UIView之所以能显示在屏幕上,完全是因为他内部的一个图层
- 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView的layer属性可以访问这个层
- 当UIView需要显示到屏幕上时,会调用drawRect:方法进行绘制,并且会将所有内容绘制在自己的图层上,绘图完毕后,系统会将图层拷贝到屏幕上,于是就完成了UIView的显示
- UIView本身不具备显示的功能,是他内部的层才有显示功能
二、CALayer的基本使用
三、关于CALayer的疑惑---用CGImage、CGColor 而不用UIImage、UIColor
- 首先CGImageRef、CGColorRef两种数据类型是定义在CoreGraphics框架中的
- UIColor、UIImage是定义在UIKit框架中
- 其次QuartzCore框架和CoreGraphics框架是可以跨平台使用的,在iOS和Mac OS X上都能使用,但是UIKit只能在iOS中使用
- 为了保证可移植性,QuartzCore不能使用UIImage、UIColor,只能使用CGImageRef、CGColorRef
四、UIView和CALayer的选择
- 既然CALayer和UIView都能实现相同的显示效果,那究竟该选择谁好呢?
- 其实,对比CALayer,UIView多了一个事件处理的功能。也就是说,CALayer不能处理用户的触摸事件,而UIView可以
- 所以,如果显示出来的东西需要跟用户进行交互的话,用UIView;如果不需要跟用户进行交互,用UIView或者CALayer都可以
- 当然,CALayer的性能会高一些,因为它少了事件处理的功能,更加轻量级
- 下面是创建新CALayer的代码,来展示图片
五、CALayer的两个重要属性 position 和 anchorPoint
- @property CGPoint position;
- 用来设置CALayer在父层中的位置
- 以父层的左上角为原点(0,0)
- @property CGPoint anchorPoint;
- 称为“定位点”、“锚点”
- 决定着CALayer身上的那个点会在position属性所指的位置
- 以自己的左上角为原点(0,0)
- 他的x、y取值范围都是0~1,默认值为(0.5,0.5)
六、非根层的隐式动画
- 每一个UIView内部都默认关联着一个CALayer,我们可以称这个Layer为Root Layer(根层)
- 所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动画
什么是隐式动画?
当对非Root Layer的部分属性进行修改时,默认会自动产生一些动画效果
而这些属性称为Animatable Properties(可动画属性)
- 代码中实现的是点击屏幕,CALayer的圆角,颜色,position,边框等随机改变并做动画
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. CALayer *layer = [CALayer layer];
layer.bounds = CGRectMake(, , , );
layer.backgroundColor = [UIColor redColor].CGColor;
layer.anchorPoint = CGPointMake(, );
layer.position = CGPointMake(, ); [self.view.layer addSublayer:layer];
_layer = layer;
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_layer.transform = CATransform3DMakeRotation(angle2rotation(arc4random_uniform()), , , ); _layer.backgroundColor = [self randomColor].CGColor; _layer.position = CGPointMake(arc4random_uniform() + , arc4random_uniform() + );
_layer.borderWidth = arc4random_uniform();
_layer.borderColor = [self randomColor].CGColor;
_layer.cornerRadius = arc4random_uniform();
} - (UIColor *)randomColor
{
CGFloat r = arc4random_uniform() / 255.0;
CGFloat g = arc4random_uniform() / 255.0;
CGFloat b = arc4random_uniform() / 255.0;
return [UIColor colorWithRed:r green:g blue:b alpha:];
}
七、钟表的练习--难点是通过NSCalendar获取当前时间
#import "ViewController.h"
// 通过秒数计算秒针转过的角度
#define secondRotation(second) ((second * 6) / 180.0 * M_PI)
// 通过分数计算分针转过的角度
#define minuteRotation(minute) ((minute * 6) / 180.0 * M_PI)
// 通过时数计算时针转过的角度
#define hourRotation(hour) ((hour * 30) / 180.0 * M_PI) @interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *clockView;
/** second */
@property(nonatomic,strong) CALayer *secondL;
/** minute */
@property(nonatomic,strong) CALayer *minuteL;
/** hour */
@property(nonatomic,strong) CALayer *hourL;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 添加秒针
[self setUpSecondLayer]; // 添加时针
[self setUpHourLayer]; // 添加分针
[self setUpMinuteLayer];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
[self timeChange];
} - (void)timeChange
{
// 获取当前时间,通过秒数计算秒针的旋转弧度
// 当前的日历对象,从日历对象中获取时间组件
NSCalendar *calendar = [NSCalendar currentCalendar]; // 日历组件包含了:年月日时分秒等
// 经验:以后枚举中有移位运算符,通常可以使用并运算(|)
NSDateComponents *compontent = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; NSInteger second = compontent.second;
NSInteger minute = compontent.minute;
NSInteger hour = compontent.hour; _hourL.transform = CATransform3DMakeRotation(hourRotation(hour) + minuteRotation(minute) / , , , );
_minuteL.transform = CATransform3DMakeRotation(minuteRotation(minute) + secondRotation(second) / , , , );
_secondL.transform = CATransform3DMakeRotation(secondRotation(second), , , ); } - (void)setUpHourLayer
{
// 获取钟表的宽高
CGFloat clockWH = _clockView.frame.size.height * 0.5;
// 创建时针的图层
CALayer *hourL = [CALayer layer];
hourL.bounds = CGRectMake(, , , clockWH - );
hourL.backgroundColor = [UIColor blackColor].CGColor;
hourL.position = CGPointMake(clockWH, clockWH);
hourL.anchorPoint = CGPointMake(0.5, );
hourL.cornerRadius = ; [_clockView.layer addSublayer:hourL];
_hourL = hourL;
} - (void)setUpMinuteLayer
{
// 获取钟表的宽高
CGFloat clockWH = _clockView.frame.size.height * 0.5;
// 创建分针的图层
CALayer *minuteL = [CALayer layer];
minuteL.bounds = CGRectMake(, , , clockWH - );
minuteL.backgroundColor = [UIColor blackColor].CGColor;
minuteL.position = CGPointMake(clockWH, clockWH);
minuteL.anchorPoint = CGPointMake(0.5, );
minuteL.cornerRadius = ; [_clockView.layer addSublayer:minuteL];
_minuteL = minuteL;
} - (void)setUpSecondLayer
{
// 获取钟表的宽高
CGFloat clockWH = _clockView.frame.size.height * 0.5;
// 创建秒针的图层
CALayer *secondL = [CALayer layer];
secondL.bounds = CGRectMake(, , , clockWH - );
secondL.backgroundColor = [UIColor redColor].CGColor;
secondL.position = CGPointMake(clockWH, clockWH);
secondL.anchorPoint = CGPointMake(0.5, ); [_clockView.layer addSublayer:secondL];
_secondL = secondL;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end