美文网首页
IOS 开发- 隐式动画

IOS 开发- 隐式动画

作者: 刘宇轩Freeman | 来源:发表于2017-04-24 08:46 被阅读0次

Do What I mean, not what I say

Implicit Animations

首先看一个demo

@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *layerView; @property (nonatomic, weak) IBOutlet CALayer *colorLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //create sublayer
    self.colorLayer = [CALayer layer];
    self.colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
    self.colorLayer.backgroundColor = [UIColor blueColor].CGColor;
    //add it to our view
    [self.layerView.layer addSublayer:self.colorLayer]; }

- (IBAction)changeColor {
    //randomize the layer background color 
    CGFloat red = arc4random() / (CGFloat)INT_MAX; 
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    self.colorLayer.backgroundColor = [UIColor colorWithRed:red]
}
@end

在这个程序中,我们并没有看到任何代码和动画相关。我们只是单纯的更改了一个非UIView关联的Layer的属性(一定要是非UIView相关联的Layer,后文会解释原因)。当我们改变一个属性时,Core Animation是怎么判断动画类型和持续时间呢?实际上动画执行的时间取决于当前事务(Transaction)的设置,动画类型取决于图层行为(Layer Action)。事务实际上是Core Animation用来包含一系列动画集合的机制,任何时候在一个指定事务中去改变都不会立刻发生变化,而是当事务提交的时候开始用一个动画过渡到新值。事务是通过CATransation 类来管理的,这个类的设计很有意思,不像它的名字,它管理了一个你不能直接访问的事务的栈。CATransaction没有属性或者实例方法,并且也不能使用alloc,init来创建。然而,你可以通过类方法,+begin和+commit来把一个新的事务压入栈中,或者推出当前事件。

任何的一个可动画的图层的属性的变化都会被添加到栈顶的事务。(Any layer property change that can be animated will be added to the topmost transaction in the stack.)你可以通过+setAnimationDuration: 方法来设置当前事务的动画时间,或者,通过+animationDuration 方法来获取当前的动画时间。

Core Animation 在每一个run loop周期中自动开始一个新的事务,即是你不显示的使用[CATransaction begin]开始一个新的事务,任何一次在run loop循环中属性的改变都会被集中起来,然后坐一次0.25秒的动画。明白了这些之后,我们就可以轻松的修改变色动画的时间了,我们当然可以使用当前事务的+setAnimationDuration:方法来修改动画持续时间,但是我们先起一个新的事务,这样的话修改时间就不会产生副作用,因为修改当前事务的时间可能会影响统一时刻的其他动画,所以最好还是在调整动画之前压入一个新的事务。

- (IBAction)changeColor {
    //begin a new transaction
    [CATransaction begin];
    //set the animation duration to 1 second
    [CATransaction setAnimationDuration:1.0];
    //randomize the layer background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    self.colorLayer.backgroundColor = [UIColor colorWithRed:red
    //commit the transaction
    [CATransaction commit]; 
}

完成块
基于UIView的block动画允许你在动画结束的时候提供一个完成块。CATransaction接口提供的+setCompleteBlock:方法也有同样的功能。

- (IBAction)changeColor {
    //begin a new transaction
    [CATransaction begin];
    //set the animation duration to 1 second
    [CATransaction setAnimationDuration:1.0]; //add the spin animation on completion
    [CATransaction setCompletionBlock:^{
    //rotate the layer 90 degrees
    CGAffineTransform transform = self.colorLayer.affineTransform; 
    transform = CGAffineTransformRotate(transform, M_PI_2); 
    self.colorLayer.affineTransform = transform;
}];

    //randomize the layer background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
    //commit the transaction
    [CATransaction commit]; 
}

注意,旋转动画要比颜色渐变快得多,这事因为完成块是在颜色渐变得事务提交并出栈之后才被执行,所以,用默认的事务做变换,默认的时间也变成了0.25秒。

相关文章

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

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

  • IOS 开发- 隐式动画

    Do What I mean, not what I say Implicit Animations 首先看一个d...

  • iOS-CALayer (四)

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

  • iOS动画笔记

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

  • iOS面试题-每日十道-第四天

    一. 简述iOS动画机制 iOS分为显式动画,隐式动画 显式动画: 对一些属性做指定的自定义动画,或者创建非线性动...

  • iOS动画

    iOS动画有隐式和显示之分。隐式动画指的是,无须创建动画对象,只需改变动画层的属性,让核心动画自己去完成动画效果,...

  • iOS核心动画高级技巧六(显式动画)

    目录 属性动画动画组过渡在动画过程中取消动画总结 序言 前面介绍了隐式动画的概念。隐式动画是在iOS平台创建动态用...

  • iOS 开发核心动画Core Animation(附demo)

    最近在研究iOS动画,iOS中添加动画的方法:UIView的简单动画,layer的隐式动画,还有Core Anim...

  • CALayer动画专题

    CALayer动画专题 来自《ios核心动画高级技巧》书中的一些例子总结,包含了 隐式动画 显式动画 缓冲-动画速...

  • 8、显式动画

    显式动画 如果想让事情变得顺利,只有靠自己 -- 夏尔·纪尧姆 上一章介绍了隐式动画的概念。隐式动画是iOS平台上...

网友评论

      本文标题:IOS 开发- 隐式动画

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