CALayer, CoreGraphics与CABasicAnimation介绍

简介:

今天我们来看一下CALayer、CoreGraphics和CABasicAnimation。这些东西在处理界面绘制、动画效果上非常有用。

本篇博文就讲介绍CALayer的基本概念,使用CoreGraphics自定义绘制,以及基于CABasicAnimation的动画。以下内容都假定您有一定的Object-C基础,也熟悉UIView等相关的操作。如果不熟的话,您还要自行查阅资料。这里就不多讲了。

要使用CALayer,首先要添加QuartzCore框架。然后在你的Controller里添加包含该框架的头文件

#import <QuartzCore/QuartzCore.h>

在Controller的实现中添加viewDidAppear:方法。

每个view都有layer对象。可以通过view的layer属性访问。也可以创建一个layer对象:

CALayer *layer = [CALayer layer];

默认的,layer的frame是CGRectZero。虽然默认的添加了之后(addSublayer)看不见,但是layer已经存在了。为了让这个layer现实出来,修改一下这些可视属性。

layer.frame = CGRectMake(100, 100, 100, 100);
layer.backgroundColor = [UIColor orangeColor].CGColor;

这里layer接受的是color的CGColor属性值。添加到controller的view.layer的子layer中:

[self.view.layer addSublayer:layer];

运行起来项目,就可以看到这个orange色的一片。那就是你刚刚添加的layer。

现在开始研究自定义绘制。开始在layer上绘制之前需要给layer设置代理

[layer setDelegate:self];

注意:layer的代理如果是Controller的话,没有什么问题。如果是CAlayer或者UIView及其子类的话就会出问题。当自定义绘制时,会调用这个方法

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    NSLog(@"layer custom draw");
}

但是运行项目你会发现这个方法不会自动被调用,在Console里不会打印出“layer custom draw”。很有意思啊,这是为什么呢?这是因为我们需要自己调用触发layer绘制的方法。很简单!咱们已经设定好了代理,那么只需要调用这个方法

[layer setNeedsDisplay];

那么这时你再运行起来项目看看,Console就会打印出“layer custom draw”这个字符串了。

把下面的代码复制到你的绘制方法里。看看CoreGraphics是如何起作用的。

大家可以看到一条黑线,额,黑线,贯穿layer。

这个layer看起来太方了,来个圆角是不是更好。再加个边条,加个阴影。

复制代码
    layer.cornerRadius = 20;
    layer.borderColor = [UIColor yellowColor].CGColor;
    layer.borderWidth = 5;
    layer.shadowColor = [UIColor blackColor].CGColor;
    layer.shadowOffset = CGSizeMake(10, 10);
    layer.shadowOpacity = .8f;
复制代码

下面来看看CABasicAnimation。

在viewDidAppear方法中,添加完layer之后添加动画的代码:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[animation setDuration:1.0];
[animation setRepeatCount:INT_MAX];
[animation setFromValue:[NSNumber numberWithFloat:0.0]];
[animation setToValue:[NSNumber numberWithFloat:1.0]];
[layer addAnimation:animation forKey:nil];

跑起来看看,对于layer透明度动画在视图加载完之后动画开始重复播放1000次。

全部代码(略有更改)

复制代码
 1 #import "ADImplicitViewController.h"
 2 #import <QuartzCore/QuartzCore.h>
 3 
 4 @interface ADImplicitViewController ()
 5 @property (nonatomic, weak) CALayer *animLayer;
 6 @end
 7 
 8 @implementation ADImplicitViewController
 9 
10 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
11 {
12     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
13     if (self) {
14         // Custom initialization
15     }
16     return self;
17 }
18 
19 - (void)viewDidLoad
20 {
21     [super viewDidLoad];
22     // Do any additional setup after loading the view.
23 }
24 
25 - (void)viewDidAppear:(BOOL)animated{
26     [super viewDidAppear: animated];
27     self.view.backgroundColor = [UIColor whiteColor];
28     
29     CALayer *layer = [CALayer layer];
30     layer.frame = CGRectMake(100, 100, 100, 100);
31     layer.backgroundColor = [UIColor orangeColor].CGColor;
32     _animLayer = layer;
33     [layer setDelegate:self];
34     
35     [self.view.layer addSublayer:layer];
36     
37 //    [layer setNeedsDisplay];
38     
39     layer.cornerRadius = 20;
40     layer.borderColor = [UIColor yellowColor].CGColor;
41     layer.borderWidth = 5;
42     layer.shadowColor = [UIColor blackColor].CGColor;
43     layer.shadowOffset = CGSizeMake(10, 10);
44     layer.shadowOpacity = .8f;
45     
46     CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
47     [animation setDuration:1.0];
48     [animation setRepeatCount:1000];
49     [animation setFromValue:[NSNumber numberWithFloat:0.0]];
50     [animation setToValue:[NSNumber numberWithFloat:1.0]];
51     [layer addAnimation:animation forKey:nil];
52 }
53 
54 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
55     NSLog(@"layer custom draw");
56     
57 //    CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
58 //    CGContextSetLineWidth(ctx, 5);
59 //    
60 //    CGContextMoveToPoint(ctx, 5, 5);
61 //    CGContextAddLineToPoint(ctx, 95, 95);
62 //    
63 //    CGContextStrokePath(ctx);
64 }
65 
66 - (void)didReceiveMemoryWarning
67 {
68     [super didReceiveMemoryWarning];
69     // Dispose of any resources that can be recreated.
70 }
71 
72 - (IBAction)startAction:(id)sender {
73     
74     _animLayer.backgroundColor = [UIColor redColor].CGColor;
75 }
76 
77 @end
复制代码

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/p/3412532.html
相关文章
CABasicAnimation旋转动画
CABasicAnimation旋转动画
135 0
CABasicAnimation旋转动画
|
容器
UIView与CALayer的关系
UIView与CALayer的关系
420 0
|
Swift
swift Animation-CABasicAnimation
swift Animation-CABasicAnimation
285 0
一个重要的类 CALayer
一个重要的类CALayer —— 基本概览(一)一个重要的类CALayer —— 其与UIView的区别(二)一个重要的类CALayer ——主要属性及其在显示图片中的简单应用(三)
761 0