美文网首页
iOS Core Animation Advanced Tech

iOS Core Animation Advanced Tech

作者: Helly1024 | 来源:发表于2016-04-13 17:45 被阅读62次

    隐式动画

    在Core Animation中,屏幕上的任何东西都可以做动画,并且默认动画就是打开的。例如当改变CALayer的一个可做动画属性时,它并不是立即在屏幕上提现出来,而是从先前的值平滑地过渡到新的值,这就是隐式动画

    事务

    在Core Animation中,动画执行的时间取决于当前事务的设置,动画类型取决于图层行为。

    事务是Core Animation用来封装一系列属性动画集合的机制,任何用指定事务去改变可以做动画的图层属性都不会立刻发生变化,而是当事务提交的时候开始用一个动画过渡到新值。

    事务是通过CATransaction类来管理的,这个类没有属性或实例方法,也不能通过+alloc-init方法来创建。但是CATransaction可以通过类方法+begin+commit将一个新的事务入栈或出栈当前的事务。

    CALayer的任何一个可动画属性的值发生改变,都会添加到栈中顶层的事务当中。可以通过下列方法设置和获取当前事务动画的执行时间。

    // 设置
    + (void)setAnimationDuration:(CFTimeInterval)dur;
    
    // 获取
    + (CFTimeInterval)animationDuration;
    

    在UIView的基于block的动画方法:+animateWithDuration:animations:中,CATransaction的+begin+commit方法在+animateWithDuration:animations:内部自动调用,这样block中所有属性的改变都会被事务所包含。

    完成块

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

    下面是在颜色变化动画完成后添加一个旋转90度的动画的示例代码:

    - (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秒。

    图层行为

    当我们修改UIView关联的图层的可动画属性时会发现,隐式动画并没有生效,这是由于UIKit禁用掉了图层的隐式动画。为了弄明白UIView是如何做到这一点的,首先我们需要知道隐式动画是如何实现的:

    我们把属性被修改时CALayer自动应用的动画称作行为。当CALayer的属性被修改时,它会调用- (id<CAAction>)actionForKey:(NSString *)event,传递属性的名称作为参数来获取对应的行为。在这个方法中会执行如下几个步骤:

    1. 首先检测CAlayer是否有delegate,并且是否实现了CALayerDelegate协议中的- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event。如果实现了,则直接调用这个方法得到行为。
    2. 如果CALayer没有delegate或者其delegate没有实现上述方法,则在CALayer的NSDictonary类型的actions中查找是否有该属性名称对应因为的映射。
    3. 如果actions中没有该属性名称对应行为的映射,那么接着在style属性中查找是否有行为同该属性名称匹配。
    4. 如果style中也没有,则会调用+(id<CAAction>)defaultActionForKey:(NSString *)event;方法来获取属性预定义的标准行为。

    在执行完-actionForKey:方法后,要么会返回nil(此时在属性值改变时将不会有动画),要么就返回一个符合CAAction协议的对象(此时在属性值改变时就会有动画效果)。

    这就解释了UIKit是如何禁用隐式动画的:每个UIView对它关联的图层都扮演了一个委托,并且提供了-actionForLayer:forKey的实现方法。当更改UIView对象对应图层的属性的动作不是在一个UIView动画block中实现,UIView对所有图层行为返回nil,但是如果在动画block范围之内,它就返回了一个非空值。

    除了可以在CALayer的代理方法-actionForLayer:forKey中返回nil来禁用隐式动画之外,还可以通过CATransacition的类方法+setDisableActions:来对所有属性启用或禁用隐式动画。

    总结:

    • UIView关联的图层禁用了隐式动画,对这种图层做动画的唯一办法就是使用UIView的动画函数(而不是依赖CATransaction),或者继承UIView,并覆盖-actionForLayer:forKey:方法,或者直接创建一个显式动画。
    • 对于单独存在的图层,我们可以通过实现图层的-actionForLayer:forKey:委托方法,或者提供一个actions字典来控制隐式动画。

    colorLayer设置一个自定义actions字典代码片段:

    //add a custom action
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    self.colorLayer.actions = @{@"backgroundColor": transition};
    

    呈现与模型

    当一个图层的属性发生改变时,属性值确实立刻更新了,但是屏幕上并没有马上发生改变。这是因为新设置的属性值并没有直接调整图层的外观,相反,它只是定义了图层动画结束之后将要呈现的外观。

    在CAlayer中,除了属性的值外,还有一个记录在隐式动画过程中显示在屏幕上属性的值,这个显示值被存储在一个叫做呈现图层的独立图层中。呈现图层实际上是模型图层的复制,但是它的属性值代表了在任何指定时刻当前外观效果。所以,与图层树相对应的,还存在着一个呈现树。

    获取图层的呈现图层:

    self.colorLayer.presentationLayer;
    

    获取呈现图层对应的模型图层:

    self.colorLayer.presentationLayer.modelLayer;
    

    在大多数情况下,都不需要我们直接访问呈现图层,但是,两种情况下呈现图层会变得很有用,一个是同步动画,一个是处理用户交互。

    使用presentationLayer图层来判断当前图层位置的示例代码:

    @interface ViewController ()
    
    @property (nonatomic, strong) CALayer *colorLayer;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //create a red layer
        self.colorLayer = [CALayer layer];
        self.colorLayer.frame = CGRectMake(0, 0, 100, 100);
        self.colorLayer.position = CGPointMake(
                                                self.view.bounds.size.width / 2,
                                                self.view.bounds.size.height / 2
                                                );
        self.colorLayer.backgroundColor = [UIColor redColor].CGColor;
        [self.view.layer addSublayer:self.colorLayer];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        //get the touch point
        CGPoint point = [[touches anyObject] locationInView:self.view];
        
        //check if we've tapped the moving layer
        if ([self.colorLayer.presentationLayer hitTest:point]) {
            //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;
        } else {
            //otherwise (slowly) move the layer to new position
            [CATransaction begin];
            [CATransaction setAnimationDuration:4.0];
            self.colorLayer.position = point;
            [CATransaction commit];
        }
    }
    
    @end

    相关文章

      网友评论

          本文标题:iOS Core Animation Advanced Tech

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