iOS的常用动画,带详细注释

h


  1. //    
  2. //  CoreAnimationEffect.h    
  3. //  CoreAnimationEffect    
  4. //    
  5. //  Created by VincentXue on 13-1-19.    
  6. //  Copyright (c) 2013年 VincentXue. All rights reserved.    
  7. //    
  8.      
  9. #import <Foundation/Foundation.h>    
  10.      
  11. /**  
  12.  !  导入QuartzCore.framework  
  13.  *  
  14.  *  Example:  
  15.  *  
  16.  *  Step.1  
  17.  *  
  18.  *      #import "CoreAnimationEffect.h"  
  19.  *  
  20.  *  Step.2  
  21.  *  
  22.  *      [CoreAnimationEffect animationMoveLeft:your view];  
  23.  *    
  24.  */    
  25.      
  26.      
  27. @interface CoreAnimationEffect : NSObject    
  28.      
  29. #pragma mark - Custom Animation    
  30.      
  31. /**  
  32.  *   @brief 快速构建一个你自定义的动画,有以下参数供你设置.  
  33.  *  
  34.  *   @note  调用系统预置Type需要在调用类引入下句  
  35.  *  
  36.  *          #import <QuartzCore/QuartzCore.h>  
  37.  *  
  38.  *   @param type                动画过渡类型  
  39.  *   @param subType             动画过渡方向(子类型)  
  40.  *   @param duration            动画持续时间  
  41.  *   @param timingFunction      动画定时函数属性  
  42.  *   @param theView             需要添加动画的view.  
  43.  *  
  44.  *  
  45.  */    
  46.      
  47. + (void)showAnimationType:(NSString *)type    
  48.               withSubType:(NSString *)subType    
  49.                  duration:(CFTimeInterval)duration    
  50.            timingFunction:(NSString *)timingFunction    
  51.                      view:(UIView *)theView;    
  52.      
  53. #pragma mark - Preset Animation    
  54.      
  55. /**  
  56.  *  下面是一些常用的动画效果  
  57.  */    
  58.      
  59. // reveal    
  60. + (void)animationRevealFromBottom:(UIView *)view;    
  61. + (void)animationRevealFromTop:(UIView *)view;    
  62. + (void)animationRevealFromLeft:(UIView *)view;    
  63. + (void)animationRevealFromRight:(UIView *)view;    
  64.      
  65. // 渐隐渐消    
  66. + (void)animationEaseIn:(UIView *)view;    
  67. + (void)animationEaseOut:(UIView *)view;    
  68.      
  69. // 翻转    
  70. + (void)animationFlipFromLeft:(UIView *)view;    
  71. + (void)animationFlipFromRigh:(UIView *)view;    
  72.      
  73. // 翻页    
  74. + (void)animationCurlUp:(UIView *)view;    
  75. + (void)animationCurlDown:(UIView *)view;    
  76.      
  77. // push    
  78. + (void)animationPushUp:(UIView *)view;    
  79. + (void)animationPushDown:(UIView *)view;    
  80. + (void)animationPushLeft:(UIView *)view;    
  81. + (void)animationPushRight:(UIView *)view;    
  82.      
  83. // move    
  84. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration;    
  85. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration;    
  86. + (void)animationMoveLeft:(UIView *)view;    
  87. + (void)animationMoveRight:(UIView *)view;    
  88.      
  89. // 旋转缩放    
  90.      
  91. // 各种旋转缩放效果    
  92. + (void)animationRotateAndScaleEffects:(UIView *)view;    
  93.      
  94. // 旋转同时缩小放大效果    
  95. + (void)animationRotateAndScaleDownUp:(UIView *)view;    
  96.      
  97.      
  98.      
  99. #pragma mark - Private API    
  100.      
  101. /**  
  102.  *  下面动画里用到的某些属性在当前API里是不合法的,但是也可以用.  
  103.  */    
  104.      
  105. + (void)animationFlipFromTop:(UIView *)view;    
  106. + (void)animationFlipFromBottom:(UIView *)view;    
  107.      
  108. + (void)animationCubeFromLeft:(UIView *)view;    
  109. + (void)animationCubeFromRight:(UIView *)view;    
  110. + (void)animationCubeFromTop:(UIView *)view;    
  111. + (void)animationCubeFromBottom:(UIView *)view;    
  112.      
  113. + (void)animationSuckEffect:(UIView *)view;    
  114.      
  115. + (void)animationRippleEffect:(UIView *)view;    
  116.      
  117. + (void)animationCameraOpen:(UIView *)view;    
  118. + (void)animationCameraClose:(UIView *)view;    
  119.      
  120. @end    
  121.      
  122.      


m


  1. //    
  2. //  CoreAnimationEffect.m    
  3. //  CoreAnimationEffect    
  4. //    
  5. //  Created by VincentXue on 13-1-19.    
  6. //  Copyright (c) 2013年 VincentXue. All rights reserved.    
  7. //    
  8.      
  9. #import "CoreAnimationEffect.h"    
  10.      
  11. #import <QuartzCore/QuartzCore.h>    
  12.      
  13. @implementation CoreAnimationEffect    
  14.      
  15. /**  
  16.  *  首先推荐一个不错的网站.   http://www.raywenderlich.com  
  17.  */    
  18.      
  19. #pragma mark - Custom Animation    
  20.      
  21. + (void)showAnimationType:(NSString *)type    
  22.               withSubType:(NSString *)subType    
  23.                  duration:(CFTimeInterval)duration    
  24.            timingFunction:(NSString *)timingFunction    
  25.                      view:(UIView *)theView    
  26. {    
  27.     /** CATransition  
  28.      *  
  29.      *  @see http://www.dreamingwish.com/dream-2012/the-concept-of-coreanimation-programming-guide.html  
  30.      *  @see http://geeklu.com/2012/09/animation-in-ios/  
  31.      *  
  32.      *  CATransition 常用设置及属性注解如下:  
  33.      */    
  34.      
  35.     CATransition *animation = [CATransition animation];    
  36.          
  37.     /** delegate  
  38.      *  
  39.      *  动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法.  
  40.      *  
  41.      *  @see CAAnimationDelegate    (按下command键点击)  
  42.      */    
  43.          
  44.     animation.delegate = self;    
  45.          
  46.     /** duration  
  47.      *  
  48.      *  动画持续时间  
  49.      */    
  50.          
  51.     animation.duration = duration;    
  52.          
  53.     /** timingFunction  
  54.      *  
  55.      *  用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是  
  56.      *  先快后慢,先慢后快还是先慢再快再慢.  
  57.      *  
  58.      *  动画的开始与结束的快慢,有五个预置分别为(下同):  
  59.      *  kCAMediaTimingFunctionLinear            线性,即匀速  
  60.      *  kCAMediaTimingFunctionEaseIn            先慢后快  
  61.      *  kCAMediaTimingFunctionEaseOut           先快后慢  
  62.      *  kCAMediaTimingFunctionEaseInEaseOut     先慢后快再慢  
  63.      *  kCAMediaTimingFunctionDefault           实际效果是动画中间比较快.  
  64.      */    
  65.          
  66.     /** timingFunction  
  67.      *  
  68.      *  当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction  
  69.      *  具体参见下面的URL  
  70.      *  
  71.      *  @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html  
  72.      *  
  73.      *  + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;  
  74.      *  
  75.      *  - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;  
  76.      */    
  77.          
  78.     animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];    
  79.          
  80.     /** fillMode  
  81.      *  
  82.      *  决定当前对象过了非active时间段的行为,比如动画开始之前,动画结束之后.  
  83.      *  预置为:  
  84.      *  kCAFillModeRemoved   默认,当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态  
  85.      *  kCAFillModeForwards  当动画结束后,layer会一直保持着动画最后的状态  
  86.      *  kCAFillModeBackwards 和kCAFillModeForwards相对,具体参考上面的URL  
  87.      *  kCAFillModeBoth      kCAFillModeForwards和kCAFillModeBackwards在一起的效果  
  88.      */    
  89.          
  90.     animation.fillMode = kCAFillModeForwards;    
  91.          
  92.     /** removedOnCompletion  
  93.      *  
  94.      *  这个属性默认为YES.一般情况下,不需要设置这个属性.  
  95.      *  
  96.      *  但如果是CAAnimation动画,并且需要设置 fillMode 属性,那么需要将 removedOnCompletion 设置为NO,否则  
  97.      *  fillMode无效  
  98.      */    
  99.          
  100. //    animation.removedOnCompletion = NO;    
  101.          
  102.     /** type  
  103.      *  
  104.      *  各种动画效果  其中除了'fade', `moveIn', `push' , `reveal' ,其他属于似有的API(我是这么认为的,可以点进去看下注释).  
  105.      *  ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.  
  106.      *  @"cube"                     立方体翻滚效果  
  107.      *  @"moveIn"                   新视图移到旧视图上面  
  108.      *  @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)  
  109.      *  @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果)  
  110.      *  @"pageCurl"                 向上翻一页  
  111.      *  @"pageUnCurl"               向下翻一页  
  112.      *  @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)  
  113.      *  @"rippleEffect"             滴水效果,(不支持过渡方向)  
  114.      *  @"oglFlip"                  上下左右翻转效果  
  115.      *  @"rotate"                   旋转效果  
  116.      *  @"push"                       
  117.      *  @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)  
  118.      *  @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)  
  119.      */    
  120.          
  121.     /** type  
  122.      *  
  123.      *  kCATransitionFade            交叉淡化过渡  
  124.      *  kCATransitionMoveIn          新视图移到旧视图上面  
  125.      *  kCATransitionPush            新视图把旧视图推出去  
  126.      *  kCATransitionReveal          将旧视图移开,显示下面的新视图  
  127.      */    
  128.          
  129.     animation.type = type;    
  130.          
  131.     /** subtype  
  132.      *  
  133.      *  各种动画方向  
  134.      *  
  135.      *  kCATransitionFromRight;      同字面意思(下同)  
  136.      *  kCATransitionFromLeft;  
  137.      *  kCATransitionFromTop;  
  138.      *  kCATransitionFromBottom;  
  139.      */    
  140.          
  141.     /** subtype  
  142.      *  
  143.      *  当type为@"rotate"(旋转)的时候,它也有几个对应的subtype,分别为:  
  144.      *  90cw    逆时针旋转90°  
  145.      *  90ccw   顺时针旋转90°  
  146.      *  180cw   逆时针旋转180°  
  147.      *  180ccw  顺时针旋转180°  
  148.      */    
  149.          
  150.     /**  
  151.      *  type与subtype的对应关系(必看),如果对应错误,动画不会显现.  
  152.      *  
  153.      *  @see http://iphonedevwiki.net/index.php/CATransition  
  154.      */    
  155.          
  156.     animation.subtype = subType;    
  157.          
  158.     /**  
  159.      *  所有核心动画和特效都是基于CAAnimation,而CAAnimation是作用于CALayer的.所以把动画添加到layer上.  
  160.      *  forKey  可以是任意字符串.  
  161.      */    
  162.          
  163.     [theView.layer addAnimation:animation forKey:nil];    
  164. }    
  165.      
  166. #pragma mark - Preset Animation    
  167.      
  168.      
  169. + (void)animationRevealFromBottom:(UIView *)view    
  170. {    
  171.     CATransition *animation = [CATransition animation];    
  172.     [animation setDuration:0.35f];    
  173.     [animation setType:kCATransitionReveal];    
  174.     [animation setSubtype:kCATransitionFromBottom];    
  175.     [animation setFillMode:kCAFillModeForwards];    
  176.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];    
  177.          
  178.     [view.layer addAnimation:animation forKey:nil];    
  179. }    
  180.      
  181. + (void)animationRevealFromTop:(UIView *)view    
  182. {    
  183.     CATransition *animation = [CATransition animation];    
  184.     [animation setDuration:0.35f];    
  185.     [animation setType:kCATransitionReveal];    
  186.     [animation setSubtype:kCATransitionFromTop];    
  187.     [animation setFillMode:kCAFillModeForwards];    
  188.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  189.          
  190.     [view.layer addAnimation:animation forKey:nil];    
  191. }    
  192.      
  193. + (void)animationRevealFromLeft:(UIView *)view    
  194. {    
  195.     CATransition *animation = [CATransition animation];    
  196.     [animation setDuration:0.35f];    
  197.     [animation setType:kCATransitionReveal];    
  198.     [animation setSubtype:kCATransitionFromLeft];    
  199.     [animation setFillMode:kCAFillModeForwards];    
  200.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];    
  201.          
  202.     [view.layer addAnimation:animation forKey:nil];    
  203. }    
  204.      
  205. + (void)animationRevealFromRight:(UIView *)view    
  206. {    
  207.     CATransition *animation = [CATransition animation];    
  208.     [animation setDuration:0.35f];    
  209.     [animation setType:kCATransitionReveal];    
  210.     [animation setSubtype:kCATransitionFromRight];    
  211.     [animation setFillMode:kCAFillModeForwards];    
  212.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];    
  213.          
  214.     [view.layer addAnimation:animation forKey:nil];    
  215. }    
  216.      
  217.      
  218. + (void)animationEaseIn:(UIView *)view    
  219. {    
  220.     CATransition *animation = [CATransition animation];    
  221.     [animation setDuration:0.35f];    
  222.     [animation setType:kCATransitionFade];    
  223.     [animation setFillMode:kCAFillModeForwards];    
  224.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];    
  225.          
  226.     [view.layer addAnimation:animation forKey:nil];    
  227. }    
  228.      
  229. + (void)animationEaseOut:(UIView *)view    
  230. {    
  231.     CATransition *animation = [CATransition animation];    
  232.     [animation setDuration:0.35f];    
  233.     [animation setType:kCATransitionFade];    
  234.     [animation setFillMode:kCAFillModeForwards];    
  235.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  236.          
  237.     [view.layer addAnimation:animation forKey:nil];    
  238. }    
  239.      
  240.      
  241. /**  
  242.  *  UIViewAnimation  
  243.  *  
  244.  *  @see    http://www.cocoachina.com/bbs/read.php?tid=110168  
  245.  *  
  246.  *  @brief  UIView动画应该是最简单便捷创建动画的方式了,详解请猛戳URL.  
  247.  *    
  248.  *  @method beginAnimations:context 第一个参数用来作为动画的标识,第二个参数给代理代理传递消息.至于为什么一个使用  
  249.  *                                  nil而另外一个使用NULL,是因为第一个参数是一个对象指针,而第二个参数是基本数据类型.  
  250.  *  @method setAnimationCurve:      设置动画的加速或减速的方式(速度)  
  251.  *  @method setAnimationDuration:   动画持续时间  
  252.  *  @method setAnimationTransition:forView:cache:   第一个参数定义动画类型,第二个参数是当前视图对象,第三个参数是是否使用缓冲区  
  253.  *  @method commitAnimations        动画结束  
  254.  */    
  255.      
  256. + (void)animationFlipFromLeft:(UIView *)view    
  257. {    
  258.     [UIView beginAnimations:nil context:NULL];    
  259.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
  260.     [UIView setAnimationDuration:0.35f];    
  261.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO];    
  262.     [UIView commitAnimations];    
  263. }    
  264.      
  265. + (void)animationFlipFromRigh:(UIView *)view    
  266. {    
  267.     [UIView beginAnimations:nil context:NULL];    
  268.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
  269.     [UIView setAnimationDuration:0.35f];    
  270.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:NO];    
  271.     [UIView commitAnimations];    
  272. }    
  273.      
  274.      
  275. + (void)animationCurlUp:(UIView *)view    
  276. {    
  277.     [UIView beginAnimations:nil context:NULL];    
  278.     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];    
  279.     [UIView setAnimationDuration:0.35f];    
  280.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:NO];    
  281.     [UIView commitAnimations];    
  282. }    
  283.      
  284. + (void)animationCurlDown:(UIView *)view    
  285. {    
  286.     [UIView beginAnimations:nil context:NULL];    
  287.     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];    
  288.     [UIView setAnimationDuration:0.35f];    
  289.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:view cache:NO];    
  290.     [UIView commitAnimations];    
  291. }    
  292.      
  293. + (void)animationPushUp:(UIView *)view    
  294. {    
  295.     CATransition *animation = [CATransition animation];    
  296.     [animation setDuration:0.35f];    
  297.     [animation setFillMode:kCAFillModeForwards];    
  298.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  299.     [animation setType:kCATransitionPush];    
  300.     [animation setSubtype:kCATransitionFromTop];    
  301.          
  302.     [view.layer addAnimation:animation forKey:nil];    
  303. }    
  304.      
  305. + (void)animationPushDown:(UIView *)view    
  306. {    
  307.     CATransition *animation = [CATransition animation];    
  308.     [animation setDuration:0.35f];    
  309.     [animation setFillMode:kCAFillModeForwards];    
  310.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  311.     [animation setType:kCATransitionPush];    
  312.     [animation setSubtype:kCATransitionFromBottom];    
  313.          
  314.     [view.layer addAnimation:animation forKey:nil];    
  315. }    
  316.      
  317. + (void)animationPushLeft:(UIView *)view    
  318. {    
  319.     CATransition *animation = [CATransition animation];    
  320.     [animation setDuration:0.35f];    
  321.     [animation setFillMode:kCAFillModeForwards];    
  322.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  323.     [animation setType:kCATransitionPush];    
  324.     [animation setSubtype:kCATransitionFromLeft];    
  325.          
  326.     [view.layer addAnimation:animation forKey:nil];    
  327. }    
  328.      
  329. + (void)animationPushRight:(UIView *)view    
  330. {    
  331.     CATransition *animation = [CATransition animation];    
  332.     [animation setDuration:0.35f];    
  333.     [animation setFillMode:kCAFillModeForwards];    
  334.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  335.     [animation setType:kCATransitionPush];    
  336.     [animation setSubtype:kCATransitionFromRight];    
  337.          
  338.     [view.layer addAnimation:animation forKey:nil];    
  339. }    
  340.      
  341. // presentModalViewController    
  342. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration    
  343. {    
  344.     CATransition *animation = [CATransition animation];    
  345.     [animation setDuration:duration];    
  346.     [animation setFillMode:kCAFillModeForwards];    
  347.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];    
  348.     [animation setType:kCATransitionMoveIn];    
  349.     [animation setSubtype:kCATransitionFromTop];    
  350.          
  351.     [view.layer addAnimation:animation forKey:nil];    
  352. }    
  353.      
  354. // dissModalViewController    
  355. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration    
  356. {    
  357.     CATransition *transition = [CATransition animation];    
  358.     transition.duration =0.4;    
  359.     transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    
  360.     transition.type = kCATransitionReveal;    
  361.     transition.subtype = kCATransitionFromBottom;    
  362.     [view.layer addAnimation:transition forKey:nil];    
  363. }    
  364.      
  365. + (void)animationMoveLeft:(UIView *)view    
  366. {    
  367.     CATransition *animation = [CATransition animation];    
  368.     [animation setDuration:0.35f];    
  369.     [animation setFillMode:kCAFillModeForwards];    
  370.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  371.     [animation setType:kCATransitionMoveIn];    
  372.     [animation setSubtype:kCATransitionFromLeft];    
  373.          
  374.     [view.layer addAnimation:animation forKey:nil];    
  375. }    
  376.      
  377. + (void)animationMoveRight:(UIView *)view    
  378. {    
  379.     CATransition *animation = [CATransition animation];    
  380.     [animation setDuration:0.35f];    
  381.     [animation setFillMode:kCAFillModeForwards];    
  382.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  383.     [animation setType:kCATransitionMoveIn];    
  384.     [animation setSubtype:kCATransitionFromRight];    
  385.          
  386.     [view.layer addAnimation:animation forKey:nil];    
  387. }    
  388.      
  389. +(void)animationRotateAndScaleEffects:(UIView *)view    
  390. {    
  391.     [UIView animateWithDuration:0.35f animations:^    
  392.      {    
  393.          /**  
  394.           *  @see       http://donbe.blog.163.com/blog/static/138048021201061054243442/  
  395.           *  
  396.           *  @param     transform   形变属性(结构体),可以利用这个属性去对view做一些翻转或者缩放.详解请猛戳↑URL.  
  397.           *  
  398.           *  @method    valueWithCATransform3D: 此方法需要一个CATransform3D的结构体.一些非详细的讲解可以看下面的URL  
  399.           *  
  400.           *  @see       http://blog.csdn.net/liubo0_0/article/details/7452166  
  401.           *  
  402.           */    
  403.               
  404.          view.transform = CGAffineTransformMakeScale(0.001, 0.001);    
  405.               
  406.          CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];    
  407.               
  408.          // 向右旋转45°缩小到最小,然后再从小到大推出.    
  409.          animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)];    
  410.               
  411.          /**  
  412.           *     其他效果:  
  413.           *     从底部向上收缩一半后弹出  
  414.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)];  
  415.           *  
  416.           *     从底部向上完全收缩后弹出  
  417.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)];  
  418.           *  
  419.           *     左旋转45°缩小到最小,然后再从小到大推出.  
  420.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)];  
  421.           *  
  422.           *     旋转180°缩小到最小,然后再从小到大推出.  
  423.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)];  
  424.           */    
  425.               
  426.          animation.duration = 0.45;    
  427.          animation.repeatCount = 1;    
  428.          [view.layer addAnimation:animation forKey:nil];    
  429.               
  430.      }    
  431.                      completion:^(BOOL finished)    
  432.      {    
  433.          [UIView animateWithDuration:0.35f animations:^    
  434.           {    
  435.               view.transform = CGAffineTransformMakeScale(1.0, 1.0);    
  436.           }];    
  437.      }];    
  438. }    
  439.      
  440. /** CABasicAnimation  
  441.  *  
  442.  *  @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html  
  443.  *  
  444.  *  @brief                      便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个  
  445.  *                              键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果  
  446.  *                              具体可以填写什么请参考上面的URL,切勿乱填!  
  447.  *                              例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度.  
  448.  *                              这个动画的效果是把view旋转到最小,再旋转回来.  
  449.  *                              你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类.  
  450.  *  
  451.  *  @param toValue              动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为:  
  452.  *                              fromValue(开始值), toValue(结束值), byValue(偏移值),  
  453.  !                              这三个属性最多只能同时设置两个;  
  454.  *                              他们之间的关系如下:  
  455.  *                              如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue;  
  456.  *                              如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue;  
  457.  *                              如果同时设置了byValue  和toValue,那么动画就会从toValue - byValue过渡到toValue;  
  458.  *  
  459.  *                              如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value;  
  460.  *                              如果只设置了toValue  ,那么动画就会从当前的value过渡到toValue;  
  461.  *                              如果只设置了byValue  ,那么动画就会从从当前的value过渡到当前value + byValue.  
  462.  *  
  463.  *                              可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并  
  464.  *                              同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值.  
  465.  !                              而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去  
  466.  *                              完成这些显示效果而已.  
  467.  *                              在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度.  
  468.  *  
  469.  *  @param duration             动画持续时间  
  470.  *  
  471.  *  @param timingFunction       动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢...  
  472.  */    
  473.      
  474. /** CAAnimationGroup  
  475.  *  
  476.  *  @brief                      顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画,  
  477.  *                              把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性,  
  478.  *                              这时候就可以使用CAAnimationGroup.  
  479.  *  
  480.  *  @param duration             动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一  
  481.  *                              设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性  
  482.  *                              的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画.  
  483.  *  
  484.  *  @param autoreverses         动画完成后自动重新开始,默认为NO.  
  485.  *  
  486.  *  @param repeatCount          动画重复次数,默认为0.  
  487.  *  
  488.  *  @param animations           动画组(数组类型),把需要同时运行的动画加到这个数组里.  
  489.  *  
  490.  *  @note  addAnimation:forKey  这个方法的forKey参数是一个字符串,这个字符串可以随意设置.  
  491.  *  
  492.  *  @note                       如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把  
  493.  *                              removedOnCompletion 设置为NO;  
  494.  */    
  495.      
  496. + (void)animationRotateAndScaleDownUp:(UIView *)view    
  497. {    
  498.     CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];    
  499.  rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2];    
  500.  rotationAnimation.duration = 0.35f;    
  501.  rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    
  502.          
  503.  CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];    
  504.  scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];    
  505.  scaleAnimation.duration = 0.35f;    
  506.  scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    
  507.       
  508.  CAAnimationGroup *animationGroup = [CAAnimationGroup animation];    
  509.  animationGroup.duration = 0.35f;    
  510.  animationGroup.autoreverses = YES;    
  511.  animationGroup.repeatCount = 1;    
  512.  animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil];    
  513.  [view.layer addAnimation:animationGroup forKey:@"animationGroup"];    
  514. }    
  515.      
  516.      
  517.      
  518. #pragma mark - Private API    
  519.      
  520. + (void)animationFlipFromTop:(UIView *)view    
  521. {    
  522.     CATransition *animation = [CATransition animation];    
  523.     [animation setDuration:0.35f];    
  524.     [animation setFillMode:kCAFillModeForwards];    
  525.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  526.     [animation setType:@"oglFlip"];    
  527.     [animation setSubtype:@"fromTop"];    
  528.          
  529.     [view.layer addAnimation:animation forKey:nil];    
  530. }    
  531.      
  532. + (void)animationFlipFromBottom:(UIView *)view    
  533. {    
  534.     CATransition *animation = [CATransition animation];    
  535.     [animation setDuration:0.35f];    
  536.     [animation setFillMode:kCAFillModeForwards];    
  537.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  538.     [animation setType:@"oglFlip"];    
  539.     [animation setSubtype:@"fromBottom"];    
  540.          
  541.     [view.layer addAnimation:animation forKey:nil];    
  542. }    
  543.      
  544. + (void)animationCubeFromLeft:(UIView *)view    
  545. {    
  546.     CATransition *animation = [CATransition animation];    
  547.     [animation setDuration:0.35f];    
  548.     [animation setFillMode:kCAFillModeForwards];    
  549.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  550.     [animation setType:@"cube"];    
  551.     [animation setSubtype:@"fromLeft"];    
  552.          
  553.     [view.layer addAnimation:animation forKey:nil];    
  554. }    
  555.      
  556. + (void)animationCubeFromRight:(UIView *)view    
  557. {    
  558.     CATransition *animation = [CATransition animation];    
  559.     [animation setDuration:0.35f];    
  560.     [animation setFillMode:kCAFillModeForwards];    
  561.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  562.     [animation setType:@"cube"];    
  563.     [animation setSubtype:@"fromRight"];    
  564.          
  565.     [view.layer addAnimation:animation forKey:nil];    
  566. }    
  567.      
  568. + (void)animationCubeFromTop:(UIView *)view    
  569. {    
  570.     CATransition *animation = [CATransition animation];    
  571.     [animation setDuration:0.35f];    
  572.     [animation setFillMode:kCAFillModeForwards];    
  573.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  574.     [animation setType:@"cube"];    
  575.     [animation setSubtype:@"fromTop"];    
  576.          
  577.     [view.layer addAnimation:animation forKey:nil];    
  578. }    
  579.      
  580. + (void)animationCubeFromBottom:(UIView *)view    
  581. {    
  582.     CATransition *animation = [CATransition animation];    
  583.     [animation setDuration:0.35f];    
  584.     [animation setFillMode:kCAFillModeForwards];    
  585.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  586.     [animation setType:@"cube"];    
  587.     [animation setSubtype:@"fromBottom"];    
  588.          
  589.     [view.layer addAnimation:animation forKey:nil];    
  590. }    
  591.      
  592. + (void)animationSuckEffect:(UIView *)view    
  593. {    
  594.     CATransition *animation = [CATransition animation];    
  595.     [animation setDuration:0.35f];    
  596.     [animation setFillMode:kCAFillModeForwards];    
  597.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  598.     [animation setType:@"suckEffect"];    
  599.          
  600.     [view.layer addAnimation:animation forKey:nil];    
  601. }    
  602.      
  603. + (void)animationRippleEffect:(UIView *)view    
  604. {    
  605.     CATransition *animation = [CATransition animation];    
  606.     [animation setDuration:0.35f];    
  607.     [animation setFillMode:kCAFillModeForwards];    
  608.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  609.     [animation setType:@"rippleEffect"];    
  610.          
  611.     [view.layer addAnimation:animation forKey:nil];    
  612. }    
  613.      
  614. + (void)animationCameraOpen:(UIView *)view    
  615. {    
  616.     CATransition *animation = [CATransition animation];    
  617.     [animation setDuration:0.35f];    
  618.     [animation setFillMode:kCAFillModeForwards];    
  619.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  620.     [animation setType:@"cameraIrisHollowOpen"];    
  621.     [animation setSubtype:@"fromRight"];    
  622.          
  623.     [view.layer addAnimation:animation forKey:nil];    
  624. }    
  625.      
  626. + (void)animationCameraClose:(UIView *)view    
  627. {    
  628.     CATransition *animation = [CATransition animation];    
  629.     [animation setDuration:0.35f];    
  630.     [animation setFillMode:kCAFillModeForwards];    
  631.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    
  632.     [animation setType:@"cameraIrisHollowClose"];    
  633.     [animation setSubtype:@"fromRight"];    
  634.          
  635.     [view.layer addAnimation:animation forKey:nil];    
  636. }    
  637. @end    

转载于:http://blog.csdn.net/xwren362922604/article/details/9942979

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值