关于隐式动画

作者: 一个很帅的蓝孩子 | 来源:发表于2017-04-11 11:19 被阅读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的头文件中有详细的说明,实质上是如下几步:

  • 图层首先检测它是否有委托,并且是否实现CALayerDelegate协议指定的-actionForLayer:forKey方法。如果有,直接调用并返回结果。
  • 如果没有委托,或者委托没有实现-actionForLayer:forKey方法,图层接着检查包含属性名称对应行为映射的actions字典。
  • 如果actions字典没有包含对应的属性,那么图层接着在它的style字典接着搜索属性名。
  • 最后,如果在style里面也找不到对应的行为,那么图层将会直接调用定义了每个属性的标准行为的-defaultActionForKey:方法。
    所以一轮完整的搜索结束之后,-actionForKey:要么返回空(这种情况下将不会有动画发生),要么是CAAction协议对应的对象,最后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添加之前。

相关文章

  • 关于隐式动画

    最近看了点动画方面的知识,做个笔记记录一下。 这就是隐式动画,仅仅改变了Layer的backgroundColor...

  • 为什么 View 的根 Layer 没有隐式动画?

    上一篇整理了 关于隐式动画的笔记 我们知道根 layer 是不能做隐式动画的就像这样, 是没有动画效果的, 颜色瞬...

  • 取消(关闭)隐式动画

    取消(关闭)隐式动画** 可以通过动画事务(CATransaction)关闭默认的隐式动画效果[CATransac...

  • 隐式动画&显式动画 学习笔记

    隐式动画 CALayer的属性基本上都可以进行隐式动画 CATransaction可以控制隐式动画(执行时间或者关...

  • iOS-CALayer (四)

    上一篇 : iOS-CALayer (三) 前言:继续深入学习动画,主要从隐式动画、显式动画上车。 一、隐式动画 ...

  • iOS动画笔记

    在iOS各类动画效果中,习惯分为两类:隐式动画和显式动画。 隐式动画 简单的讲,由系统进行相关动画配置,执行动画效...

  • Core Animation小记(三)

    动画 1.隐式动画。没有制定任何动画的类型叫做隐式动画。事务,是通过CATransaction类来做管理,只能通过...

  • iOS之动画

    1.隐式动画 1.1.什么是隐式动画? 了解什么是隐式动画之前,要先了解是什么根层和非根层.根层:UIView内部...

  • iOS隐式动画与显式动画的区别

    请参考iOS隐式动画与显式动画的区别

  • SwiftUI -- View 动画

    SwiftUI 中的动画有两种类型:显式动画和隐式动画。 一、显式动画 显式动画通过 withAnimation ...

网友评论

    本文标题:关于隐式动画

    本文链接:https://www.haomeiwen.com/subject/ftdyattx.html