iOS动画总结

就两种,UIView自带动画和核心动画(Core Animation)

一、UIView自带动画(UIKit框架中)
               注意:只有当UIView的以下属性改变时才能产生动画效果,且这些动画效果都是平面性的(二维的),基于手机屏幕的平面,主要有“平移(位置改变,沿x轴、Y轴或同时改变)”、“缩放(view大小改变,宽高)”、“旋转(只有沿Z轴,即垂直于屏幕的轴,旋转平行于屏幕)”、“背景色的改变”、“透明度”:
                 重点介绍transform的使用:
                               eg:view.transform = CGAffineTransformMakeSca le(0.5, 0.5);
                 CGAffineTransform结构体详解 CGAffineTransform.h:
                                CGAffineTransform CGAffineTransformIdentit  默认值,保持原样不变
                CGAffineTransform CGAffineTransformMake(CGFloat a, CGFloat b,CGFloat c, CGFloat d, CGFloat tx, CGFloat ty)                                                          直接变换三维矩阵,很少用吧
              创建变换============= 
              //Translation(平移)创建一个平移变换,起始位置 x 会加上tx , y 会加上 ty
                CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,CGFloat ty)
               //Scale(缩放) 宽度就会变为  width*sx  ,对应高度变为  hight * sy
                CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
              //Rotation(旋转)将一个图片视图旋转了多少度,参数是弧度,先把度转化为弧度
                CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)  //90.0*(M_PI/180.0)  顺时针旋转90度,-90.0*(M_PI/180.0) 逆时针旋转90度,view的旋转方向仅以整个圆周最小的弧度,即270不会顺时针转3/4圈,而是逆时针转1/4圈,以180为界,大于等于180了就是逆时针了
             增加变换===============
             //为一个变换再加上平移变换
                CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,CGFloat tx, CGFloat ty)
              //为一个Transformation再加上缩放
                CGAffineTransform CGAffineTransformScale(CGAffineTransform t,CGFloat sx, CGFloat sy)
             //为一个Transformation再加上旋转
                 CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,CGFloat angle)
             其他====================
             //判断一个变换是不是原生变换,即没有什么变换
                bool CGAffineTransformIsIdentity(CGAffineTransform t)
             //创建一个变换的反向效果
                CGAffineTransform CGAffineTransformInvert(CGAffineTransform t)
              //合并两个变换
                CGAffineTransform CGAffineTransformConcat(CGAffineTransform t1,CGAffineTransform t2)
              //判断两个变换是否一样
                 bool CGAffineTransformEqualToTransform(CGAffineTransform t1,CGAffineTransform t2)


   
               (1).以begin commit的方式提交动画代码块
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:2.25];                                           //动画持续时间,秒
[UIView setAnimationCurve:UIViewAnimationCurveLine ar];//动画执行速度(UIViewAnimationCurveEase InOut--动画开始和结束慢,中间快   UIViewAnimationCurveEase In--开始慢,后面快  UIViewAnimationCurveEase Out--前面快,结束慢     UIViewAnimationCurveLine ar--匀速)
[UIView setAnimationDelegate:self];//一定要设置动画的委托
............... .{改变View属性的代码块}...................
.........
[UIView commitAnimations];

If you want to execute code immediately before or after an animation, you must associate a delegate object and a start or stop selector with your begin/commit animation block. You set your delegate object using the setAnimationDelegate: class method of UIView and you set your start and stop selectors using the setAnimationWillStartSelector: and setAnimationDidStopSelector: class methods. During the animation, the animation system calls your delegate methods at the appropriate times to give you a chance to perform your code.

The signatures of your animation delegate methods need to be similar to the following: 

- (void)animationWillStart:(NSString *)animationID context:(void *)context;
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

                     说明:以上代码块便可以执行一个动画,其中的“{...}”块又可以嵌套新的动画块,
                                         里面的动画块只有在                 最外层的动画提交之后才会一起执行
                      其中有一个特例,可以实现view的翻页、立体旋转等效果
+ (void) setAnimationTransition:(UIViewAnimationTransitio n)transition forView:(UIView *)view cache:(BOOL)cache;
                     UIViewAnimationTransitio n宏介绍如下:
                                     UIViewAnimationTransitio nNone,
                                     UIViewAnimationTransitio nFlipFromLeft,
                                     UIViewAnimationTransitio nFlipFromRight,
                                     UIViewAnimationTransitio nCurlUp,
                                     UIViewAnimationTransitio nCurlDown,
                       

二、使用核心动画(Core Animation)[QuartzCore.framework中]
                 (1).UIView与CALayer的关系:http://www.cnblogs.com/mjios/archive/2013/04/14/3019728.html
                                                                                                                 http://www.cnblogs.com/lovecode/articles/2249548.html
                          实质上UIView中元素的展示与渲染都是考CALayer来实现,每个UIView都包含有一个layer属性,view与layer操作上也是一样,既能addSubLayers(addSubView)也能获取父视图(父layer),创建layer对象时也需指定大小(frame)、内容(content)、背景色等属性,唯一不同的时UIView还继承自UIResponder,便能响应用户事件,而CALayer直接继承自NSObject,不能响应
                          第一点中的直接操作UIView的某些属性便能产生动画,其实质就是操作改UIView对象中的Layer对象的属性
                 (2).  QuartzCore框架中动画产生的原理
                  该框架中有三种类型的动画:
                     CABasicAnimation
                     CAKeyframeAnimation
                     CATransition
                  只要将创建好这三种动画对象add到layer上便能产生动画效果,不再需要使用第一点中UIView得代码块提交了,他们的继承关系如下:
                    
IOS <wbr>动画总结
 

其中CABasicAnimationCAKeyframeAnimation是对图层中的不同属性进行动画的。

如果要多整个图层进行动画,则应该使用CATransitionAnimation

如果要使用组合动画,例如要改变图层的大小和透明度,则可以先为每个属性创建一个CABasicAnimation对象,再把他们组合到CAAnimationGroup中,最后把这个组合添加到要进行动画的CALayer中。


           (3). CABasicAnimation CAKeyframeAnimation 是对图层中的不同属性进行动画的,即第一点所说的支持动画的那些属性

                 (4).使用:CATransition

                     - (void)changeUIView2{  
                              CATransition *transition [CATransition animation];  
                              transition.duration 2.0f; 
                             
  transition.type kCATransitionPush; 
                             
  transition.subtype kCATransitionFromTop; 
                             
  [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
                              
  [self.view.layer addAnimation:transition forKey:@"animation"];
                     }
    transition.type 的类型可以有

    淡化、推挤、揭开、覆盖

    NSString * const kCATransitionFade;

    NSString * const kCATransitionMoveIn;

    NSString * const kCATransitionPush;

    NSString * const kCATransitionReveal;


    transition.subtype 
    也有四种

    NSString * const kCATransitionFromRight;

    NSString * const kCATransitionFromLeft;

    NSString * const kCATransitionFromTop;

    NSString * const kCATransitionFromBottom;




    私有的类型的动画类型:

    立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关

    1. animation.type @"cube"  
    2. animation.type @"suckEffect"   
    3. animation.type @"oglFlip";//不管subType is "fromLeft" or "fromRight",official只有一种效果  
    4. animation.type @"rippleEffect"  
    5. animation.type @"pageCurl"  
    6. animation.type @"pageUnCurl"  
    7. animation.type @"cameraIrisHollowOpen " 
    8. animation.type @"cameraIrisHollowClose " 
    下图是第一个cube立方体的效果:

    CATransition的 startProgress  endProgress属性

    这两个属性是float类型的。
    可以控制动画进行的过程,可以让动画停留在某个动画点上,值在0.0到1.0之间。endProgress要大于等于startProgress。
    比如上面的立方体转到,可以设置endProgress= 0.5,让动画停留在转动一般的位置。
    上面这些私有的动画效果,在实际应用中要谨慎使用。因为在app store审核时可能会以为这些动画效果而拒绝通过。
                   

                       
         
                      
                (5). CAAnimation都是显式动画,即动画结束后会回归动画开始前的状态。想要解决的话,必须设置“removedOnCompletion”和“fillMode”这两个属性

           // 动画终了后不返回初始状态  
           animation.removedOnCompletion NO
          animation.fillMode kCAFillModeForwards;   

    以上通过给动画设置一个代理去监听动画的开始和结束,在动画结束的时候重新设置图层位置。并且通过一个自定义属性来保存动画结束时候图层的位置信息。

    但运行以上代码,又发现问题了,动画结束后设定图层位置,图层会重新从起始位置运动到终点位置,这是因为对于非根图层而言,设置图层的可动画属性会产生隐式动画,当然可以设置动画图层为根图层来解决这个问题,但实际当中是通过关闭隐式动画来解决这个问题。要关闭隐式动画,需要用到动画事务CATransaction,在事务内关闭隐式动画,例如:

    1. - (void) startBaseAnimation:(CGPoint) location {  
    2.       
    3.     endPoint = location;  
    4.       
    5.     //开启动画事物  
    6.     [CATransaction begin];  
    7.     //禁用隐式动画  
    8.     [CATransaction setDisableActions:YES];  
    9.     layer.position = endPoint;  
    10.       
    11.       
    12. //    1、创建动画,并且指定位置(position)作为动画属性  
    13.     CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"position"];  
    14.   
    15. //    2、设置动画属性初始值和结束值  
    16.     baseAnimation.fromValue = [NSValue valueWithCGPoint:startPoint];  
    17.     baseAnimation.toValue = [NSValue valueWithCGPoint:endPoint];  
    18.       
    19. //    设置其他动画属性  
    20.     baseAnimation.duration = 3.0;  
    21.       
    22. //    设定代理  
    23.     baseAnimation.delegate = self;  
    24.       
    25. //    存储要设定的目标值  
    26.     [baseAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"BaseAnimationPosition"];  
    27.       
    28. //    3、添加动画到图层,key相当于给动画命名,可以通过此key来获取该动画  
    29.     [layer addAnimation:baseAnimation forKey:@"kBaseAnimation"];  
    30.       
    31.     //提交动画事务  
    32.     [CATransaction commit];  
    33. }  


              (6). CABasicAnimation正在进行动画的时候,点击了Home按钮后再回到app的时候,动画会被清空。
        
       http://blog.csdn.net/kqjob/article/details/10417461
       http://blog.csdn.net/hufengvip/article/details/6913492
    http://blog.csdn.net/totogo2010/article/details/8501812
    http://www.cnblogs.com/project/archive/2011/09/27/2193556.html

    转载链接:http://blog.sina.com.cn/s/blog_74e9d98d0102uyts.html

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值