UIView、Layer、Animation

来源:互联网 发布:php面试让现场写代码 编辑:程序博客网 时间:2024/05/16 16:08
在ios的所有控件中都是继承于UIView
对于UIIView而言又具有很多的属性,其中密切的相关的属性就是Layer
Layer 是CALayer类的对象。CALayer:(Core Animation Layer)
可以看出Layer存在的主要意义就是Animation。

什么是Animation呢?(在一定时间内,显示的变化)在ios系统中实现一系列的动画的方式有很多种

按照实现对象区分:
1.处于View层的动画设置

a.第一种方式:(UIViewAnimation)通过Category中的API
1.告诉系统动画要 begin
2.在begin之后设置改变量 setChange
3.最后执行动画 commitAnimation
一套动画就完成了。

    [UIView beginAnimations:@"animation" context:nil];

    [UIView setAnimationDuration:5];

    [self.animationView setOrigin:CGPointMake(200200)];

    [UIView commitAnimations];


b.第二种方式:(UIViewAnimationWithBlocks)通过Category中的API

     + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

第二种方式相对于第一种方式更加的简单,使用block的方式进行了相关代码的回调。


c.第三种方式:(特殊的UIView子类)通过特定的API方法实现动画 

    UIImageView *testimage = [[UIImageView allocinit];

    [testimage setAnimationDuration:2];

    [testimage setAnimationImages:imageArray];

    [testimage startAnimating];

       特殊的UIView子类——UIImageView具有自己的动画实现机制。按照帧的概念填充到动画数组中,自定义播放完成动画展示。


2.处于Layer层的动画设置
a.第一种方式:CATransaction 事务型动画设置
    [CATransaction begin];

    [CATransaction setAnimationDuration:2];

    sublayer2.position=CGPointMake(150.0,150.0);  //x,y轴最后位置

    sublayer2.opacity=0.5;       //最后透明度

    [CATransaction commit];

类似于UIView层动画的第一种方式,但是由于是处理layer的动画,所以需要用layer专用的方法进行设置。

(在UIView的beginAnimation中也是可以去控制Layer去进行动画,不过会有不可靠的设置,比如:无法确定动画的播放时间)


b.第二种方式:CABasicAnimation 核心基础动画(通过修改相关的属性进行动画的设置)


945405.tmp

  • CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@”opacity”; 
  • fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0]; 
  • fadeAnimation.toValue = [NSNumber numberWithFloat:0.0]; 
  • fadeAnimation.duration = 1.0; 
  • [theLayer addAnimation:fadeAnimation forKey:nil]; 
  • //改变图层实际的最后数据值 
  • theLayer.opacity = 0.0;  // 记得更新图层树 

  • 对于这种动画方式,动画仅仅是一个动画播放的概念,并不能做到更新属性,所以需要去在动画播放结束之后,确认设置属性

    c.第三种方式:CAKeyframeAnimation 关键帧动画(是基础动画的延伸,解决比如抖动类型的动画(无法仅仅给出两个状态就可以完成的动画))
    *************************************************//关键帧动画——path赋值方式实现

        CGMutablePathRef    path = CGPathCreateMutable();

        //将路径的起点定位到    50  120

        CGPathMoveToPoint(path,NULL,50.0,120.0);

        //下面5行添加5条直线的路径到path

        CGPathAddLineToPoint(path, NULL60130);

        CGPathAddLineToPoint(path, NULL70140);

        CGPathAddLineToPoint(path, NULL80150);

        CGPathAddLineToPoint(path, NULL90160);

        CGPathAddLineToPoint(path, NULL100170);

        //下面四行添加四条曲线路径到path

        CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);

        CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);

        CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);

        CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);

        //“position”为关键字创建 实例

        CAKeyframeAnimation    *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

        //设置path属性

        [animation     setPath:path];

        [animation     setDuration:3.0];

        //这句代码    表示 是否动画回到原位

        //    [animation setAutoreverses:YES];

        CFRelease(path);

        [sublayer2  addAnimation:animation forKey:NULL];

      

        *****************************************//关键帧动画——values赋值方式实现

        //1.创建核心动画

         CAKeyframeAnimation *keyAnima=[CAKeyframeAnimation animation];

         keyAnima.keyPath=@"transform.rotation";

         //设置动画时间

         keyAnima.duration=5;

         //设置图标抖动弧度

         //把度数转换为弧度  度数/180*M_PI

        keyAnima.values=@[@(M_PI/64),@(-M_PI/64),@(M_PI/64),@(-M_PI/64),@(M_PI/64),@(-M_PI/64)];

         //设置动画的重复次数(设置为最大值)

         keyAnima.repeatCount=1;

        [keyAnima setCalculationMode:kCAAnimationLinear];

        keyAnima.keyTimes = [[NSArray allocinitWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.45],[NSNumber numberWithFloat:0.5],[NSNumbernumberWithFloat:0.55],[NSNumber numberWithFloat:1], nil];

         //2.添加动画

         [sublayer2 addAnimation:keyAnima forKey:nil];

    关键帧的动画模式也是类似于属性修改型动画模式。相对basicAnimation具有更多的空间(可以设置多个动画状态,以及时间)

    keytimes:参数的设置并不是动画的时间,而是百分比的概念从0-1,设置间隔百分比


    动画类型区分:
    对于ios的动画而言,本质的表现是view属性的变化,“位置的变化”-2D(相对superView位置的变化),“自身的变化”-2D&Color(大小变化,透明度变化),“摄像机视角变化”-3D&2D(x轴旋转,y轴旋转,z轴旋转)
    通过动画与自身属性的变化与否区分:
    隐式动画、显式动画
    1.隐式动画特点是,根据动画持有者的属性,衍生出动画。当动画结束之后,保留属性的修改。
    2.显式动画特点是,同样是通过动画持有者的属性,衍生出动画。但当动画结束之后,不保留属性的修改。(就算是animation.removedOnCompletion = NO;)对于其本身的属性还是保持初始状态。
    隐式动画的实现:创建一个Layer,将这个Layer添加到需要动画的View之上。对Layer中的需要动画的属性,进行设置。系统会根据属性自动添加,“过渡动画”。(系统给予过渡动画的动画时间默认是0.25秒,如果希望设置动画时间,只需要重置时间“[CATransactionsetAnimationDuration:1(希望动画所需时间)];”)
    显式动画的实现:显式动画首先就需要一个动画的对象Animation!。实现这个Animation(具体是basic或是keyframe的类型可以自定义)。完成动画的配置之后,添加到需要动画的View上。则显式动画的实现就完成了。
    隐式动画-更加是一种“过渡动画”的概念,为属性变化添加过渡。
    显式动画-更加宽泛的动画。不是属性变化的附属品。而是独立于View动画的展现。

    0 0