<极客学院>视频教程学习笔记-iOS中CALayer的使用

<1>CALayer简介

1、CALayer一般作为UIView的容器而使用。

2、CALayer是一个管理者图片载体(image-based content)的层结构

3、直接修改单独创建出的CALayer的属性可以触发隐式动画

4、UIView中的CALayer动画必须显式触发才能生效

 开篇代码练习:

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 //2单独创建一个layer
 5 @property (nonatomic,strong) CALayer *layer;
 6 
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     //1创建一个容器View
14     UIView *containerview = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 100, 3)];
15     containerview.backgroundColor = [UIColor blueColor];
16     [self.view addSubview:containerview];
17     //3使用并设置layer的相关值
18     self.layer = [CALayer layer];
19     [self.layer setFrame:CGRectMake(0, 0, 50, 3)];
20     self.layer.backgroundColor = [UIColor redColor].CGColor;//别忘了最后面还要一个  .CGColor
21     [containerview.layer addSublayer:self.layer];
22 }
23 
24 
25 @end

 继续努力敲代码:

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 //创建一个独立的layer
 5 @property (nonatomic,strong) CALayer *layer;
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     //创建一个UIView作为父容器
13     UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 200, 3)];
14     containerView.backgroundColor = [UIColor yellowColor];
15     [self.view addSubview:containerView];
16     //layer
17     self.layer = [CALayer layer];
18     self.layer.frame = CGRectMake(0, 0, 10, 3);
19     self.layer.backgroundColor = [UIColor blueColor].CGColor;
20     [containerView.layer addSublayer:self.layer];
21     
22     // 使用了消息处理方法,并添加layerAnimatiion方法
23     [self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3.f];
24     
25 }
26 -(void)layerAnimation{
27     NSLog(@"修改了layer");
28     self.layer.frame = CGRectMake(0, 0, 150, 3);
29 }
30 @end

 

 
 
 
 
 
 

你可能感兴趣的:(学习笔记)