iOS开发代码段iOS Developer

关于隐式动画

2017-04-11  本文已影响40人  一个很帅的蓝孩子

最近看了点动画方面的知识,做个笔记记录一下。

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic,strong) CALayer *colorlayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.colorlayer = [CALayer layer];
    self.colorlayer.frame = CGRectMake(0, 0, 100, 100);
    self.colorlayer.backgroundColor = [UIColor redColor].CGColor;
    [self.contentView.layer addSublayer:self.colorlayer];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    CGFloat red = arc4random_uniform(256) / 255.0;
    CGFloat gre = arc4random_uniform(256) / 255.0;
    CGFloat blu = arc4random_uniform(256) / 255.0;
    self.colorlayer.backgroundColor = [UIColor colorWithRed:red green:gre blue:blu alpha:1.0].CGColor;
    
}

这就是隐式动画,仅仅改变了Layer的backgroundColor属性,运行却有动画效果。

实际上动画执行的时间取决于当前事务的设置,动画类型取决于图层行为。

事务是通过CATransaction类来做管理,CATransaction没有属性或者实例方法,并且也不能用+alloc和-init方法创建它。但是可以用+begin和+commit分别来入栈或者出栈。任何可以做动画的图层属性都会被添加到栈顶的事务,你可以通过+setAnimationDuration:方法设置当前事务的动画时间,或者通过+animationDuration方法来获取值(默认0.25秒)。

Core Animation在每个run loop周期中自动开始一次新的事务(run loop是iOS负责收集用户输入,处理定时器或者网络事件并且重新绘制屏幕的东西),即使你不显式的用[CATransaction begin]开始一次事务,任何在一次run loop循环中属性的改变都会被集中起来,然后做一次0.25秒的动画。

所以事务是通过CATransaction类隐式得设置了动画执行时间,我们也可以通过setAnimationDuration设置动画时间。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 如果我们要自己通过setAnimationDuration设置动画执行时间,必须要先起一个新的事务,因为修改当前事务的时间可能会导致同一时刻别的动画,所以最好是在调整动画之前压入一个新的事务。
    [CATransaction begin];
    
    // 默认为0.25秒
    [CATransaction setAnimationDuration:0.25];
    
    CGFloat red = arc4random_uniform(256) / 255.0;
    CGFloat gre = arc4random_uniform(256) / 255.0;
    CGFloat blu = arc4random_uniform(256) / 255.0;
    self.colorlayer.backgroundColor = [UIColor colorWithRed:red green:gre blue:blu alpha:1.0].CGColor;
    
    [CATransaction commit];
    
}
UIView有两个方法,+beginAnimations:context:和+commitAnimations、+animateWithDuration:animations:,和CATransaction的+begin和+commit方法类似。实际上在+beginAnimations:context:和+commitAnimations之间所有视图或者图层属性的改变而做的动画都是由于设置了CATransaction的原因。

UIView的+animateWithDuration:animations:提供了一个block允许在动画结束后做一些操作,CATransaction也提供了一个+setCompletionBlock:方法。

关于隐式动画,还有最重要的一点是:rootLayer不执行隐式动画。
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.contentView.layer.backgroundColor = [UIColor redColor].CGColor;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    CGFloat red = arc4random_uniform(256) / 255.0;
    CGFloat gre = arc4random_uniform(256) / 255.0;
    CGFloat blu = arc4random_uniform(256) / 255.0;
    self.contentView.layer.backgroundColor = [UIColor colorWithRed:red green:gre blue:blu alpha:1.0].CGColor;
    
}

点击屏幕,图层颜色是瞬间切换的,没有了动画效果。我们知道动画类型取决于图层行为,图层行为是
我们把改变属性时CALayer自动应用的动画称作行为,当CALayer的属性被修改时候,它会调用-actionForKey:方法,传递属性的名称。剩下的操作都在CALayer的头文件中有详细的说明,实质上是如下几步:

于是这就解释了UIKit是如何禁用隐式动画的:每个UIView对它关联的图层都扮演了一个委托,并且提供了-actionForLayer:forKey的实现方法。当不在一个动画块的实现中,UIView对所有图层行为返回nil,但是在动画block范围之内,它就返回了一个非空值。
我们可以测试一下:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.contentView.layer.backgroundColor = [UIColor redColor].CGColor;

    NSLog(@"1 --- %@",[self.contentView actionForLayer:self.contentView.layer forKey:@"backgroundColor"]);
    
    [UIView beginAnimations:nil context:nil];
    
    NSLog(@"2 --- %@",[self.contentView actionForLayer:self.contentView.layer forKey:@"backgroundColor"]);
    
    [UIView commitAnimations];
}
2017-04-11 11:05:22.758 隐式动画[3583:3064040] 1 --- <null>
2017-04-11 11:05:22.759 隐式动画[3583:3064040] 2 --- <CABasicAnimation: 0x61000003da00>```

所以-actionForKey:返回nil,这种情况下就不执行动画。我们也可以通过[CATransaction setDisableActions:YES];阻止动画执行。

行为通常是一个被Core Animation隐式调用的显式动画对象。
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
self.colorLayer.actions = @{@"backgroundColor": transition};

[self.contentView.layer addSublayer:self.colorlayer];   

需要注意的是动画效果的代码要在layer添加之前。
上一篇下一篇

猜你喜欢

热点阅读