zoukankan      html  css  js  c++  java
  • (一二二)核心动画进阶

    上节我们介绍了核心动画基础,这一节介绍核心动画的主要应用和局限性。

    【核心动画的局限性】

    有一点需要注意,所有的核心动画都是假象,只是修改了View的展示位置,而不能修改真实位置,即使设置了不复位,仿佛是位置移动了,其实layer还在原位,要证明这一点,可以在动画完成后打印layer的位置。

    为了获得动画结束时机,可以通过代理,UIView已经遵循了相关协议,可以实现动画开始和结束的两个方法。

    - (void)animationDidStart:(CAAnimation *)anim{
          
    }
    
    - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
        
    }
    我们可以自定义一个View,然后改变它的位置,在animationDidStop::方法中打印它的位置,可以发现位置并没有真的被改变。

    【转场动画】

    这样说来,用核心动画来改变View的核心位置是不合适的,核心动画一般只用来做转场动画,例如多张图片的切换,假设我们有1、2、3三张图片,默认是第一张,每点击一次屏幕就切换一张,用下面的代码可以设置切换动画。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        static int clickCnt = 2;
        NSString *imageName = [NSString stringWithFormat:@"%d",clickCnt++];
        if(clickCnt > 3) clickCnt = 1;
        _imageView.image = [UIImage imageNamed:imageName];
        
        CATransition *anim = [CATransition animation];
        anim.duration = 1.0;
        anim.type = @"cube";
        [_imageView.layer addAnimation:anim forKey:nil];
        
    }
    anim的type有多种设置方式,并且在type之下还有subtype,例如翻页属性,可以设置翻页方向:

    anim.type = @"pageCurl";
    anim.subtype = kCATransitionFromLeft;
    除此之外,也可直接通过系统定义的const设置type:

    anim.type = kCATransitionReveal;

    【动画组】

    上一节介绍的核心动画一次性只能执行一个动画,为了能同时执行多个动画,只需要用CAAnimationGroup包装所有的动画,并且要设置不复位只需要设置group的属性即可,例如下面的代码,可以实现三个动画同时执行。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        
        CAAnimationGroup *anims = [CAAnimationGroup animation];
        
        CABasicAnimation *rotation = [CABasicAnimation animation];
        rotation.keyPath = @"transform.rotation";
        rotation.toValue = @M_PI_2;
        rotation.duration = 0.25;
        
        CABasicAnimation *move = [CABasicAnimation animation];
        move.keyPath = @"position";
        move.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
        move.duration = 0.5;
        
        CABasicAnimation *scale = [CABasicAnimation animation];
        scale.keyPath = @"transform.scale";
        scale.toValue = @0.5;
        scale.duration = 0.5;
        
        anims.animations = @[rotation, move, scale];
        anims.removedOnCompletion = NO;
        anims.fillMode = kCAFillModeForwards;
        [_blueView.layer addAnimation:anims forKey:nil];
        
    }

    【UIView的动画】

    除去使用核心动画,可以直接使用UIView的动画,通过设置各个参数同样可以达到核心动画的作用,并且可以真实改变View的位置。使用下面的方法,通过options设置动画类型,在animations中设置动画内容。

    [UIView transitionWithView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]

  • 相关阅读:
    .NET 应用架构指导 V2 [1]
    MSSQL优化之————探索MSSQL执行计划
    删除代码中所有的空行
    微软企业库5.0学习笔记(一)企业库是什么?
    C# MP3操作类
    Microsoft Enterprise Library 5.0系列学习笔记【1】
    基于Asp.net的CMS系统We7架设实验(环境WIN7,SQL2005,.NET3.5)(初学者参考贴) 【转】
    C#中用ILMerge将所有引用的DLL和exe文件打成一个exe文件,有图解
    “Singleton”模式
    阅读技术类图书的思考
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154053.html
Copyright © 2011-2022 走看看