30多个iOS常用动画,带详细注释

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


m


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