CALayer
CALayer 是UIView内部的一个图层。当UIView 需要显示到屏幕上时,会调用drawRect:方法进行绘图,并且会将所有内容绘制在自己的图层上,绘图完毕后,系统会将图层拷贝到屏幕上,于是就完成了UIView的显示。
话句话说,UIView 本身不具备显示的功能,是它内部的层才有显示功能。
-
CALayer 的属性
-
@property(retain) id contents;
内容(比如设置为图片CGImageRef) -
@property CGPoint position;
用来设置CALayer在父层找那个的位置; 以父层的左上角为原点(0,0) -
@property CGPoint anchorPoint
成为锚点、"定位点" ;决定这CALayer身上的哪个点会在position属性所指的位置; 以自己的左上角为原点(0,0);它的x、y 取值范围都是0-1,默认指为(0.5,0.5)
-
UIView和CALayer的选择
-既然CALayer和UIView都能实现相同的显示效果,那究竟该选择谁好呢?其实,对比CALayer,UIView多了一个事件处理的功能,也就是说,CALayer不能处理用户的触摸事件,而UIView可以
所以,如果显示出来的东西需要跟用户进行交互的话,用UIView;如果不需要跟用户进行交互,用UIView或者CALayer都可以
当然,CALayer的性能会高一些,以为它少了事件处理功能,更加轻量级。
3D旋转 (M_PI,1,1,0) x,y,z 轴
_redView.layer.transform = CATransform3DMakeRotaiton(M_PI,1,1,0)
缩放
_redView.layer.transform = CATransform3DMakeScale(0.5,0.5,1);
快速进行图层缩放,KVC
[_redVIew.layer setValue:@0.5 forkeyPath:@"transform.scale"];
[_redView.layer setValue:@(M_PI) forkeyPath:@“transform.rotation”];
UIView简单实用CALayer
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(50,50,200,200);
layer.backgroundColor = [UIColor redColor].CGColor;
// 设置图层内容
layer.contents = (id)[UIImage imageNamed:@"阿狸头像"].CGImage;
[self.view.layer addSubLayer:layer];
时钟
#import "ViewController.h"
#define kClockW _clockView.bounds.size.width
#define angle2radion(a) ((a) / 180.0 * M_PI)
// 一秒钟秒针转6°
#define perSecondA 6
// 一分钟分针转6°
#define perMinuteA 6
// 一小时时针转30°
#define perHourA 30
// 每分钟时针转多少度
#define perMinuteHourA 0.5
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *clockView;
@property (nonatomic, weak) CALayer *secondLayer;
@property (nonatomic, weak) CALayer *minuteLayer;
@property (nonatomic, weak) CALayer *hourLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 添加时针
[self setUpHourLayer];
// 添加分针
[self setUpMinuteLayer];
// 添加秒针
[self setUpSecondLayer];
// 添加定时器
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
[self timeChange];
}
- (void)timeChange
{
// 获取当前的系统的时间
// 获取当前日历对象
NSCalendar *calendar = [NSCalendar currentCalendar];
// 获取日期的组件:年月日小时分秒
// components:需要获取的日期组件
// fromDate:获取哪个日期的组件
// 经验:以后枚举中有移位运算符,通常一般可以使用并运算(|)
NSDateComponents *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
// 获取秒
NSInteger second = cmp.second;
// 获取分
NSInteger minute = cmp.minute;
// 获取小时
NSInteger hour = cmp.hour;
// 计算秒针转多少度
CGFloat secondA = second * perSecondA;
// 计算分针转多少度
CGFloat minuteA = minute * perMinuteA;
// 计算时针转多少度
CGFloat hourA = hour * perHourA + minute * perMinuteHourA;
// 旋转秒针
_secondLayer.transform = CATransform3DMakeRotation(angle2radion(secondA), 0, 0, 1);
// 旋转分针
_minuteLayer.transform = CATransform3DMakeRotation(angle2radion(minuteA), 0, 0, 1);
// 旋转小时
_hourLayer.transform = CATransform3DMakeRotation(angle2radion(hourA), 0, 0, 1);
}
#pragma mark - 添加秒针
- (void)setUpSecondLayer
{
CALayer *secondL = [CALayer layer];
secondL.backgroundColor = [UIColor redColor].CGColor;
// 设置锚点
secondL.anchorPoint = CGPointMake(0.5, 1);
secondL.position = CGPointMake(kClockW * 0.5, kClockW * 0.5);
secondL.bounds = CGRectMake(0, 0, 1, kClockW * 0.5 - 20);
[_clockView.layer addSublayer:secondL];
_secondLayer = secondL;
}
#pragma mark - 添加分针
- (void)setUpMinuteLayer
{
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor blackColor].CGColor;
// 设置锚点
layer.anchorPoint = CGPointMake(0.5, 1);
layer.position = CGPointMake(kClockW * 0.5, kClockW * 0.5);
layer.bounds = CGRectMake(0, 0, 4, kClockW * 0.5 - 20);
layer.cornerRadius = 4;
[_clockView.layer addSublayer:layer];
_minuteLayer = layer;
}
#pragma mark - 添加时针
- (void)setUpHourLayer
{
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor blackColor].CGColor;
// 设置锚点
layer.anchorPoint = CGPointMake(0.5, 1);
layer.position = CGPointMake(kClockW * 0.5, kClockW * 0.5);
layer.bounds = CGRectMake(0, 0, 4, kClockW * 0.5 - 40);
layer.cornerRadius = 4;
[_clockView.layer addSublayer:layer];
_hourLayer = layer;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end