您的位置:首页 > 移动开发 > IOS开发

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

2015-07-24 23:31 567 查看
<1>CALayer简介

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

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

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

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

开篇代码练习:

#import "ViewController.h"

@interface ViewController ()
//2单独创建一个layer
@property (nonatomic,strong) CALayer *layer;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//1创建一个容器View
UIView *containerview = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 100, 3)];
containerview.backgroundColor = [UIColor blueColor];
[self.view addSubview:containerview];
//3使用并设置layer的相关值
self.layer = [CALayer layer];
[self.layer setFrame:CGRectMake(0, 0, 50, 3)];
self.layer.backgroundColor = [UIColor redColor].CGColor;//别忘了最后面还要一个  .CGColor
[containerview.layer addSublayer:self.layer];
}

@end


继续努力敲代码:

#import "ViewController.h"

@interface ViewController ()
//创建一个独立的layer
@property (nonatomic,strong) CALayer *layer;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//创建一个UIView作为父容器
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 200, 3)];
containerView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:containerView];
//layer
self.layer = [CALayer layer];
self.layer.frame = CGRectMake(0, 0, 10, 3);
self.layer.backgroundColor = [UIColor blueColor].CGColor;
[containerView.layer addSublayer:self.layer];

// 使用了消息处理方法,并添加layerAnimatiion方法
[self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3.f];

}
-(void)layerAnimation{
NSLog(@"修改了layer");
self.layer.frame = CGRectMake(0, 0, 150, 3);
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: