30多个iOS常用动画



  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. #import <foundation foundation.h="">

  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. @interface CoreAnimationEffect : NSObject

  24. #pragma mark - Custom Animation

  25. /**
  26. *   @brief 快速构建一个你自定义的动画,有以下参数供你设置.
  27. *
  28. *   @note  调用系统预置Type需要在调用类引入下句
  29. *
  30. *          #import <quartzcore quartzcore.h="">
  31. *
  32. *   @param type                动画过渡类型
  33. *   @param subType             动画过渡方向(子类型)
  34. *   @param duration            动画持续时间
  35. *   @param timingFunction      动画定时函数属性
  36. *   @param theView             需要添加动画的view.
  37. *
  38. *
  39. */

  40. + (void)showAnimationType:(NSString *)type
  41.               withSubType:(NSString *)subType
  42.                  duration:(CFTimeInterval)duration
  43.            timingFunction:(NSString *)timingFunction
  44.                      view:(UIView *)theView;

  45. #pragma mark - Preset Animation

  46. /**
  47. *  下面是一些常用的动画效果
  48. */

  49. // reveal
  50. + (void)animationRevealFromBottom:(UIView *)view;
  51. + (void)animationRevealFromTop:(UIView *)view;
  52. + (void)animationRevealFromLeft:(UIView *)view;
  53. + (void)animationRevealFromRight:(UIView *)view;

  54. // 渐隐渐消
  55. + (void)animationEaseIn:(UIView *)view;
  56. + (void)animationEaseOut:(UIView *)view;

  57. // 翻转
  58. + (void)animationFlipFromLeft:(UIView *)view;
  59. + (void)animationFlipFromRigh:(UIView *)view;

  60. // 翻页
  61. + (void)animationCurlUp:(UIView *)view;
  62. + (void)animationCurlDown:(UIView *)view;

  63. // push
  64. + (void)animationPushUp:(UIView *)view;
  65. + (void)animationPushDown:(UIView *)view;
  66. + (void)animationPushLeft:(UIView *)view;
  67. + (void)animationPushRight:(UIView *)view;

  68. // move
  69. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration;
  70. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration;
  71. + (void)animationMoveLeft:(UIView *)view;
  72. + (void)animationMoveRight:(UIView *)view;

  73. // 旋转缩放

  74. // 各种旋转缩放效果
  75. + (void)animationRotateAndScaleEffects:(UIView *)view;

  76. // 旋转同时缩小放大效果
  77. + (void)animationRotateAndScaleDownUp:(UIView *)view;



  78. #pragma mark - Private API

  79. /**
  80. *  下面动画里用到的某些属性在当前API里是不合法的,但是也可以用.
  81. */

  82. + (void)animationFlipFromTop:(UIView *)view;
  83. + (void)animationFlipFromBottom:(UIView *)view;

  84. + (void)animationCubeFromLeft:(UIView *)view;
  85. + (void)animationCubeFromRight:(UIView *)view;
  86. + (void)animationCubeFromTop:(UIView *)view;
  87. + (void)animationCubeFromBottom:(UIView *)view;

  88. + (void)animationSuckEffect:(UIView *)view;

  89. + (void)animationRippleEffect:(UIView *)view;

  90. + (void)animationCameraOpen:(UIView *)view;
  91. + (void)animationCameraClose:(UIView *)view;

  92. @end



  93. //
  94. //  CoreAnimationEffect.m
  95. //  CoreAnimationEffect
  96. //
  97. //  Created by VincentXue on 13-1-19.
  98. //  Copyright (c) 2013年 VincentXue. All rights reserved.
  99. //

  100. #import "CoreAnimationEffect.h"

  101. #import <quartzcore quartzcore.h="">

  102. @implementation CoreAnimationEffect

  103. /**
  104. *  首先推荐一个不错的网站.   http://www.raywenderlich.com
  105. */

  106. #pragma mark - Custom Animation

  107. + (void)showAnimationType:(NSString *)type
  108.               withSubType:(NSString *)subType
  109.                  duration:(CFTimeInterval)duration
  110.            timingFunction:(NSString *)timingFunction
  111.                      view:(UIView *)theView
  112. {
  113.     /** CATransition
  114.      *
  115.      *  @see http://www.dreamingwish.com/dream-2012/the-concept-of-coreanimation-programming-guide.html
  116.      *  @see http://geeklu.com/2012/09/animation-in-ios/
  117.      *
  118.      *  CATransition 常用设置及属性注解如下:
  119.      */

  120.     CATransition *animation = [CATransition animation];
  121.      
  122.     /** delegate
  123.      *
  124.      *  动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法.
  125.      *
  126.      *  @see CAAnimationDelegate    (按下command键点击)
  127.      */
  128.      
  129.     animation.delegate = self;
  130.      
  131.     /** duration
  132.      *
  133.      *  动画持续时间
  134.      */
  135.      
  136.     animation.duration = duration;
  137.      
  138.     /** timingFunction
  139.      *
  140.      *  用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是
  141.      *  先快后慢,先慢后快还是先慢再快再慢.
  142.      *
  143.      *  动画的开始与结束的快慢,有五个预置分别为(下同):
  144.      *  kCAMediaTimingFunctionLinear            线性,即匀速
  145.      *  kCAMediaTimingFunctionEaseIn            先慢后快
  146.      *  kCAMediaTimingFunctionEaseOut           先快后慢
  147.      *  kCAMediaTimingFunctionEaseInEaseOut     先慢后快再慢
  148.      *  kCAMediaTimingFunctionDefault           实际效果是动画中间比较快.
  149.      */
  150.      
  151.     /** timingFunction
  152.      *
  153.      *  当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction
  154.      *  具体参见下面的URL
  155.      *
  156.      *  @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html
  157.      *
  158.      *  + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
  159.      *
  160.      *  - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
  161.      */
  162.      
  163.     animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];
  164.      
  165.     /** fillMode
  166.      *
  167.      *  决定当前对象过了非active时间段的行为,比如动画开始之前,动画结束之后.
  168.      *  预置为:
  169.      *  kCAFillModeRemoved   默认,当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
  170.      *  kCAFillModeForwards  当动画结束后,layer会一直保持着动画最后的状态
  171.      *  kCAFillModeBackwards 和kCAFillModeForwards相对,具体参考上面的URL
  172.      *  kCAFillModeBoth      kCAFillModeForwards和kCAFillModeBackwards在一起的效果
  173.      */
  174.      
  175.     animation.fillMode = kCAFillModeForwards;
  176.      
  177.     /** removedOnCompletion
  178.      *
  179.      *  这个属性默认为YES.一般情况下,不需要设置这个属性.
  180.      *
  181.      *  但如果是CAAnimation动画,并且需要设置 fillMode 属性,那么需要将 removedOnCompletion 设置为NO,否则
  182.      *  fillMode无效
  183.      */
  184.      
  185. //    animation.removedOnCompletion = NO;
  186.      
  187.     /** type
  188.      *
  189.      *  各种动画效果  其中除了'fade', `moveIn', `push' , `reveal' ,其他属于似有的API(我是这么认为的,可以点进去看下注释).
  190.      *  ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
  191.      *  @"cube"                     立方体翻滚效果
  192.      *  @"moveIn"                   新视图移到旧视图上面
  193.      *  @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)
  194.      *  @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果)
  195.      *  @"pageCurl"                 向上翻一页
  196.      *  @"pageUnCurl"               向下翻一页
  197.      *  @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
  198.      *  @"rippleEffect"             滴水效果,(不支持过渡方向)
  199.      *  @"oglFlip"                  上下左右翻转效果
  200.      *  @"rotate"                   旋转效果
  201.      *  @"push"                     
  202.      *  @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)
  203.      *  @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)
  204.      */
  205.      
  206.     /** type
  207.      *
  208.      *  kCATransitionFade            交叉淡化过渡
  209.      *  kCATransitionMoveIn          新视图移到旧视图上面
  210.      *  kCATransitionPush            新视图把旧视图推出去
  211.      *  kCATransitionReveal          将旧视图移开,显示下面的新视图
  212.      */
  213.      
  214.     animation.type = type;
  215.      
  216.     /** subtype
  217.      *
  218.      *  各种动画方向
  219.      *
  220.      *  kCATransitionFromRight;      同字面意思(下同)
  221.      *  kCATransitionFromLeft;
  222.      *  kCATransitionFromTop;
  223.      *  kCATransitionFromBottom;
  224.      */
  225.      
  226.     /** subtype
  227.      *
  228.      *  当type为@"rotate"(旋转)的时候,它也有几个对应的subtype,分别为:
  229.      *  90cw    逆时针旋转90°
  230.      *  90ccw   顺时针旋转90°
  231.      *  180cw   逆时针旋转180°
  232.      *  180ccw  顺时针旋转180°
  233.      */
  234.      
  235.     /**
  236.      *  type与subtype的对应关系(必看),如果对应错误,动画不会显现.
  237.      *
  238.      *  @see http://iphonedevwiki.net/index.php/CATransition
  239.      */
  240.      
  241.     animation.subtype = subType;
  242.      
  243.     /**
  244.      *  所有核心动画和特效都是基于CAAnimation,而CAAnimation是作用于CALayer的.所以把动画添加到layer上.
  245.      *  forKey  可以是任意字符串.
  246.      */
  247.      
  248.     [theView.layer addAnimation:animation forKey:nil];
  249. }

  250. #pragma mark - Preset Animation


  251. + (void)animationRevealFromBottom:(UIView *)view
  252. {
  253.     CATransition *animation = [CATransition animation];
  254.     [animation setDuration:0.35f];
  255.     [animation setType:kCATransitionReveal];
  256.     [animation setSubtype:kCATransitionFromBottom];
  257.     [animation setFillMode:kCAFillModeForwards];
  258.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
  259.      
  260.     [view.layer addAnimation:animation forKey:nil];
  261. }

  262. + (void)animationRevealFromTop:(UIView *)view
  263. {
  264.     CATransition *animation = [CATransition animation];
  265.     [animation setDuration:0.35f];
  266.     [animation setType:kCATransitionReveal];
  267.     [animation setSubtype:kCATransitionFromTop];
  268.     [animation setFillMode:kCAFillModeForwards];
  269.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  270.      
  271.     [view.layer addAnimation:animation forKey:nil];
  272. }

  273. + (void)animationRevealFromLeft:(UIView *)view
  274. {
  275.     CATransition *animation = [CATransition animation];
  276.     [animation setDuration:0.35f];
  277.     [animation setType:kCATransitionReveal];
  278.     [animation setSubtype:kCATransitionFromLeft];
  279.     [animation setFillMode:kCAFillModeForwards];
  280.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  281.      
  282.     [view.layer addAnimation:animation forKey:nil];
  283. }

  284. + (void)animationRevealFromRight:(UIView *)view
  285. {
  286.     CATransition *animation = [CATransition animation];
  287.     [animation setDuration:0.35f];
  288.     [animation setType:kCATransitionReveal];
  289.     [animation setSubtype:kCATransitionFromRight];
  290.     [animation setFillMode:kCAFillModeForwards];
  291.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  292.      
  293.     [view.layer addAnimation:animation forKey:nil];
  294. }


  295. + (void)animationEaseIn:(UIView *)view
  296. {
  297.     CATransition *animation = [CATransition animation];
  298.     [animation setDuration:0.35f];
  299.     [animation setType:kCATransitionFade];
  300.     [animation setFillMode:kCAFillModeForwards];
  301.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
  302.      
  303.     [view.layer addAnimation:animation forKey:nil];
  304. }

  305. + (void)animationEaseOut:(UIView *)view
  306. {
  307.     CATransition *animation = [CATransition animation];
  308.     [animation setDuration:0.35f];
  309.     [animation setType:kCATransitionFade];
  310.     [animation setFillMode:kCAFillModeForwards];
  311.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  312.      
  313.     [view.layer addAnimation:animation forKey:nil];
  314. }


  315. /**
  316. *  UIViewAnimation
  317. *
  318. *  @see    http://www.cocoachina.com/bbs/read.php?tid=110168
  319. *
  320. *  @brief  UIView动画应该是最简单便捷创建动画的方式了,详解请猛戳URL.
  321. *  
  322. *  @method beginAnimations:context 第一个参数用来作为动画的标识,第二个参数给代理代理传递消息.至于为什么一个使用
  323. *                                  nil而另外一个使用NULL,是因为第一个参数是一个对象指针,而第二个参数是基本数据类型.
  324. *  @method setAnimationCurve:      设置动画的加速或减速的方式(速度)
  325. *  @method setAnimationDuration:   动画持续时间
  326. *  @method setAnimationTransition:forView:cache:   第一个参数定义动画类型,第二个参数是当前视图对象,第三个参数是是否使用缓冲区
  327. *  @method commitAnimations        动画结束
  328. */

  329. + (void)animationFlipFromLeft:(UIView *)view
  330. {
  331.     [UIView beginAnimations:nil context:NULL];
  332.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  333.     [UIView setAnimationDuration:0.35f];
  334.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO];
  335.     [UIView commitAnimations];
  336. }

  337. + (void)animationFlipFromRigh:(UIView *)view
  338. {
  339.     [UIView beginAnimations:nil context:NULL];
  340.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  341.     [UIView setAnimationDuration:0.35f];
  342.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:NO];
  343.     [UIView commitAnimations];
  344. }


  345. + (void)animationCurlUp:(UIView *)view
  346. {
  347.     [UIView beginAnimations:nil context:NULL];
  348.     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
  349.     [UIView setAnimationDuration:0.35f];
  350.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:NO];
  351.     [UIView commitAnimations];
  352. }

  353. + (void)animationCurlDown:(UIView *)view
  354. {
  355.     [UIView beginAnimations:nil context:NULL];
  356.     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
  357.     [UIView setAnimationDuration:0.35f];
  358.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:view cache:NO];
  359.     [UIView commitAnimations];
  360. }

  361. + (void)animationPushUp:(UIView *)view
  362. {
  363.     CATransition *animation = [CATransition animation];
  364.     [animation setDuration:0.35f];
  365.     [animation setFillMode:kCAFillModeForwards];
  366.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  367.     [animation setType:kCATransitionPush];
  368.     [animation setSubtype:kCATransitionFromTop];
  369.      
  370.     [view.layer addAnimation:animation forKey:nil];
  371. }

  372. + (void)animationPushDown:(UIView *)view
  373. {
  374.     CATransition *animation = [CATransition animation];
  375.     [animation setDuration:0.35f];
  376.     [animation setFillMode:kCAFillModeForwards];
  377.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  378.     [animation setType:kCATransitionPush];
  379.     [animation setSubtype:kCATransitionFromBottom];
  380.      
  381.     [view.layer addAnimation:animation forKey:nil];
  382. }

  383. + (void)animationPushLeft:(UIView *)view
  384. {
  385.     CATransition *animation = [CATransition animation];
  386.     [animation setDuration:0.35f];
  387.     [animation setFillMode:kCAFillModeForwards];
  388.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  389.     [animation setType:kCATransitionPush];
  390.     [animation setSubtype:kCATransitionFromLeft];
  391.      
  392.     [view.layer addAnimation:animation forKey:nil];
  393. }

  394. + (void)animationPushRight:(UIView *)view
  395. {
  396.     CATransition *animation = [CATransition animation];
  397.     [animation setDuration:0.35f];
  398.     [animation setFillMode:kCAFillModeForwards];
  399.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  400.     [animation setType:kCATransitionPush];
  401.     [animation setSubtype:kCATransitionFromRight];
  402.      
  403.     [view.layer addAnimation:animation forKey:nil];
  404. }

  405. // presentModalViewController
  406. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration
  407. {
  408.     CATransition *animation = [CATransition animation];
  409.     [animation setDuration:duration];
  410.     [animation setFillMode:kCAFillModeForwards];
  411.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  412.     [animation setType:kCATransitionMoveIn];
  413.     [animation setSubtype:kCATransitionFromTop];
  414.      
  415.     [view.layer addAnimation:animation forKey:nil];
  416. }

  417. // dissModalViewController
  418. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration
  419. {
  420.     CATransition *transition = [CATransition animation];
  421.     transition.duration =0.4;
  422.     transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  423.     transition.type = kCATransitionReveal;
  424.     transition.subtype = kCATransitionFromBottom;
  425.     [view.layer addAnimation:transition forKey:nil];
  426. }

  427. + (void)animationMoveLeft:(UIView *)view
  428. {
  429.     CATransition *animation = [CATransition animation];
  430.     [animation setDuration:0.35f];
  431.     [animation setFillMode:kCAFillModeForwards];
  432.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  433.     [animation setType:kCATransitionMoveIn];
  434.     [animation setSubtype:kCATransitionFromLeft];
  435.      
  436.     [view.layer addAnimation:animation forKey:nil];
  437. }

  438. + (void)animationMoveRight:(UIView *)view
  439. {
  440.     CATransition *animation = [CATransition animation];
  441.     [animation setDuration:0.35f];
  442.     [animation setFillMode:kCAFillModeForwards];
  443.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  444.     [animation setType:kCATransitionMoveIn];
  445.     [animation setSubtype:kCATransitionFromRight];
  446.      
  447.     [view.layer addAnimation:animation forKey:nil];
  448. }

  449. +(void)animationRotateAndScaleEffects:(UIView *)view
  450. {
  451.     [UIView animateWithDuration:0.35f animations:^
  452.      {
  453.          /**
  454.           *  @see       http://donbe.blog.163.com/blog/static/138048021201061054243442/
  455.           *
  456.           *  @param     transform   形变属性(结构体),可以利用这个属性去对view做一些翻转或者缩放.详解请猛戳↑URL.
  457.           *
  458.           *  @method    valueWithCATransform3D: 此方法需要一个CATransform3D的结构体.一些非详细的讲解可以看下面的URL
  459.           *
  460.           *  @see       http://blog.csdn.net/liubo0_0/article/details/7452166
  461.           *
  462.           */
  463.           
  464.          view.transform = CGAffineTransformMakeScale(0.001, 0.001);
  465.           
  466.          CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
  467.           
  468.          // 向右旋转45°缩小到最小,然后再从小到大推出.
  469.          animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)];
  470.           
  471.          /**
  472.           *     其他效果:
  473.           *     从底部向上收缩一半后弹出
  474.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)];
  475.           *
  476.           *     从底部向上完全收缩后弹出
  477.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)];
  478.           *
  479.           *     左旋转45°缩小到最小,然后再从小到大推出.
  480.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)];
  481.           *
  482.           *     旋转180°缩小到最小,然后再从小到大推出.
  483.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)];
  484.           */
  485.           
  486.          animation.duration = 0.45;
  487.          animation.repeatCount = 1;
  488.          [view.layer addAnimation:animation forKey:nil];
  489.           
  490.      }
  491.                      completion:^(BOOL finished)
  492.      {
  493.          [UIView animateWithDuration:0.35f animations:^
  494.           {
  495.               view.transform = CGAffineTransformMakeScale(1.0, 1.0);
  496.           }];
  497.      }];
  498. }

  499. /** CABasicAnimation
  500. *
  501. *  @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html
  502. *
  503. *  @brief                      便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个
  504. *                              键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果
  505. *                              具体可以填写什么请参考上面的URL,切勿乱填!
  506. *                              例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度.
  507. *                              这个动画的效果是把view旋转到最小,再旋转回来.
  508. *                              你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类.
  509. *
  510. *  @param toValue              动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为:
  511. *                              fromValue(开始值), toValue(结束值), byValue(偏移值),
  512. !                              这三个属性最多只能同时设置两个;
  513. *                              他们之间的关系如下:
  514. *                              如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue;
  515. *                              如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue;
  516. *                              如果同时设置了byValue  和toValue,那么动画就会从toValue - byValue过渡到toValue;
  517. *
  518. *                              如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value;
  519. *                              如果只设置了toValue  ,那么动画就会从当前的value过渡到toValue;
  520. *                              如果只设置了byValue  ,那么动画就会从从当前的value过渡到当前value + byValue.
  521. *
  522. *                              可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并
  523. *                              同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值.
  524. !                              而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去
  525. *                              完成这些显示效果而已.
  526. *                              在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度.
  527. *
  528. *  @param duration             动画持续时间
  529. *
  530. *  @param timingFunction       动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢...
  531. */

  532. /** CAAnimationGroup
  533. *
  534. *  @brief                      顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画,
  535. *                              把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性,
  536. *                              这时候就可以使用CAAnimationGroup.
  537. *
  538. *  @param duration             动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一
  539. *                              设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性
  540. *                              的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画.
  541. *
  542. *  @param autoreverses         动画完成后自动重新开始,默认为NO.
  543. *
  544. *  @param repeatCount          动画重复次数,默认为0.
  545. *
  546. *  @param animations           动画组(数组类型),把需要同时运行的动画加到这个数组里.
  547. *
  548. *  @note  addAnimation:forKey  这个方法的forKey参数是一个字符串,这个字符串可以随意设置.
  549. *
  550. *  @note                       如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把
  551. *                              removedOnCompletion 设置为NO;
  552. */

  553. + (void)animationRotateAndScaleDownUp:(UIView *)view
  554. {
  555.     CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  556. rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2];
  557. rotationAnimation.duration = 0.35f;
  558. rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  559.      
  560. CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  561. scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];
  562. scaleAnimation.duration = 0.35f;
  563. scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  564.   
  565. CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
  566. animationGroup.duration = 0.35f;
  567. animationGroup.autoreverses = YES;
  568. animationGroup.repeatCount = 1;
  569. animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil];
  570. [view.layer addAnimation:animationGroup forKey:@"animationGroup"];
  571. }



  572. #pragma mark - Private API

  573. + (void)animationFlipFromTop:(UIView *)view
  574. {
  575.     CATransition *animation = [CATransition animation];
  576.     [animation setDuration:0.35f];
  577.     [animation setFillMode:kCAFillModeForwards];
  578.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  579.     [animation setType:@"oglFlip"];
  580.     [animation setSubtype:@"fromTop"];
  581.      
  582.     [view.layer addAnimation:animation forKey:nil];
  583. }

  584. + (void)animationFlipFromBottom:(UIView *)view
  585. {
  586.     CATransition *animation = [CATransition animation];
  587.     [animation setDuration:0.35f];
  588.     [animation setFillMode:kCAFillModeForwards];
  589.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  590.     [animation setType:@"oglFlip"];
  591.     [animation setSubtype:@"fromBottom"];
  592.      
  593.     [view.layer addAnimation:animation forKey:nil];
  594. }

  595. + (void)animationCubeFromLeft:(UIView *)view
  596. {
  597.     CATransition *animation = [CATransition animation];
  598.     [animation setDuration:0.35f];
  599.     [animation setFillMode:kCAFillModeForwards];
  600.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  601.     [animation setType:@"cube"];
  602.     [animation setSubtype:@"fromLeft"];
  603.      
  604.     [view.layer addAnimation:animation forKey:nil];
  605. }

  606. + (void)animationCubeFromRight:(UIView *)view
  607. {
  608.     CATransition *animation = [CATransition animation];
  609.     [animation setDuration:0.35f];
  610.     [animation setFillMode:kCAFillModeForwards];
  611.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  612.     [animation setType:@"cube"];
  613.     [animation setSubtype:@"fromRight"];
  614.      
  615.     [view.layer addAnimation:animation forKey:nil];
  616. }

  617. + (void)animationCubeFromTop:(UIView *)view
  618. {
  619.     CATransition *animation = [CATransition animation];
  620.     [animation setDuration:0.35f];
  621.     [animation setFillMode:kCAFillModeForwards];
  622.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  623.     [animation setType:@"cube"];
  624.     [animation setSubtype:@"fromTop"];
  625.      
  626.     [view.layer addAnimation:animation forKey:nil];
  627. }

  628. + (void)animationCubeFromBottom:(UIView *)view
  629. {
  630.     CATransition *animation = [CATransition animation];
  631.     [animation setDuration:0.35f];
  632.     [animation setFillMode:kCAFillModeForwards];
  633.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  634.     [animation setType:@"cube"];
  635.     [animation setSubtype:@"fromBottom"];
  636.      
  637.     [view.layer addAnimation:animation forKey:nil];
  638. }

  639. + (void)animationSuckEffect:(UIView *)view
  640. {
  641.     CATransition *animation = [CATransition animation];
  642.     [animation setDuration:0.35f];
  643.     [animation setFillMode:kCAFillModeForwards];
  644.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  645.     [animation setType:@"suckEffect"];
  646.      
  647.     [view.layer addAnimation:animation forKey:nil];
  648. }

  649. + (void)animationRippleEffect:(UIView *)view
  650. {
  651.     CATransition *animation = [CATransition animation];
  652.     [animation setDuration:0.35f];
  653.     [animation setFillMode:kCAFillModeForwards];
  654.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  655.     [animation setType:@"rippleEffect"];
  656.      
  657.     [view.layer addAnimation:animation forKey:nil];
  658. }

  659. + (void)animationCameraOpen:(UIView *)view
  660. {
  661.     CATransition *animation = [CATransition animation];
  662.     [animation setDuration:0.35f];
  663.     [animation setFillMode:kCAFillModeForwards];
  664.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  665.     [animation setType:@"cameraIrisHollowOpen"];
  666.     [animation setSubtype:@"fromRight"];
  667.      
  668.     [view.layer addAnimation:animation forKey:nil];
  669. }

  670. + (void)animationCameraClose:(UIView *)view
  671. {
  672.     CATransition *animation = [CATransition animation];
  673.     [animation setDuration:0.35f];
  674.     [animation setFillMode:kCAFillModeForwards];
  675.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  676.     [animation setType:@"cameraIrisHollowClose"];
  677.     [animation setSubtype:@"fromRight"];
  678.      
  679.     [view.layer addAnimation:animation forKey:nil];
  680. }
  681. @end

  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. #import <foundation foundation.h="">

  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. @interface CoreAnimationEffect : NSObject

  24. #pragma mark - Custom Animation

  25. /**
  26. *   @brief 快速构建一个你自定义的动画,有以下参数供你设置.
  27. *
  28. *   @note  调用系统预置Type需要在调用类引入下句
  29. *
  30. *          #import <quartzcore quartzcore.h="">
  31. *
  32. *   @param type                动画过渡类型
  33. *   @param subType             动画过渡方向(子类型)
  34. *   @param duration            动画持续时间
  35. *   @param timingFunction      动画定时函数属性
  36. *   @param theView             需要添加动画的view.
  37. *
  38. *
  39. */

  40. + (void)showAnimationType:(NSString *)type
  41.               withSubType:(NSString *)subType
  42.                  duration:(CFTimeInterval)duration
  43.            timingFunction:(NSString *)timingFunction
  44.                      view:(UIView *)theView;

  45. #pragma mark - Preset Animation

  46. /**
  47. *  下面是一些常用的动画效果
  48. */

  49. // reveal
  50. + (void)animationRevealFromBottom:(UIView *)view;
  51. + (void)animationRevealFromTop:(UIView *)view;
  52. + (void)animationRevealFromLeft:(UIView *)view;
  53. + (void)animationRevealFromRight:(UIView *)view;

  54. // 渐隐渐消
  55. + (void)animationEaseIn:(UIView *)view;
  56. + (void)animationEaseOut:(UIView *)view;

  57. // 翻转
  58. + (void)animationFlipFromLeft:(UIView *)view;
  59. + (void)animationFlipFromRigh:(UIView *)view;

  60. // 翻页
  61. + (void)animationCurlUp:(UIView *)view;
  62. + (void)animationCurlDown:(UIView *)view;

  63. // push
  64. + (void)animationPushUp:(UIView *)view;
  65. + (void)animationPushDown:(UIView *)view;
  66. + (void)animationPushLeft:(UIView *)view;
  67. + (void)animationPushRight:(UIView *)view;

  68. // move
  69. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration;
  70. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration;
  71. + (void)animationMoveLeft:(UIView *)view;
  72. + (void)animationMoveRight:(UIView *)view;

  73. // 旋转缩放

  74. // 各种旋转缩放效果
  75. + (void)animationRotateAndScaleEffects:(UIView *)view;

  76. // 旋转同时缩小放大效果
  77. + (void)animationRotateAndScaleDownUp:(UIView *)view;



  78. #pragma mark - Private API

  79. /**
  80. *  下面动画里用到的某些属性在当前API里是不合法的,但是也可以用.
  81. */

  82. + (void)animationFlipFromTop:(UIView *)view;
  83. + (void)animationFlipFromBottom:(UIView *)view;

  84. + (void)animationCubeFromLeft:(UIView *)view;
  85. + (void)animationCubeFromRight:(UIView *)view;
  86. + (void)animationCubeFromTop:(UIView *)view;
  87. + (void)animationCubeFromBottom:(UIView *)view;

  88. + (void)animationSuckEffect:(UIView *)view;

  89. + (void)animationRippleEffect:(UIView *)view;

  90. + (void)animationCameraOpen:(UIView *)view;
  91. + (void)animationCameraClose:(UIView *)view;

  92. @end



  93. //
  94. //  CoreAnimationEffect.m
  95. //  CoreAnimationEffect
  96. //
  97. //  Created by VincentXue on 13-1-19.
  98. //  Copyright (c) 2013年 VincentXue. All rights reserved.
  99. //

  100. #import "CoreAnimationEffect.h"

  101. #import <quartzcore quartzcore.h="">

  102. @implementation CoreAnimationEffect

  103. /**
  104. *  首先推荐一个不错的网站.   http://www.raywenderlich.com
  105. */

  106. #pragma mark - Custom Animation

  107. + (void)showAnimationType:(NSString *)type
  108.               withSubType:(NSString *)subType
  109.                  duration:(CFTimeInterval)duration
  110.            timingFunction:(NSString *)timingFunction
  111.                      view:(UIView *)theView
  112. {
  113.     /** CATransition
  114.      *
  115.      *  @see http://www.dreamingwish.com/dream-2012/the-concept-of-coreanimation-programming-guide.html
  116.      *  @see http://geeklu.com/2012/09/animation-in-ios/
  117.      *
  118.      *  CATransition 常用设置及属性注解如下:
  119.      */

  120.     CATransition *animation = [CATransition animation];
  121.      
  122.     /** delegate
  123.      *
  124.      *  动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法.
  125.      *
  126.      *  @see CAAnimationDelegate    (按下command键点击)
  127.      */
  128.      
  129.     animation.delegate = self;
  130.      
  131.     /** duration
  132.      *
  133.      *  动画持续时间
  134.      */
  135.      
  136.     animation.duration = duration;
  137.      
  138.     /** timingFunction
  139.      *
  140.      *  用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是
  141.      *  先快后慢,先慢后快还是先慢再快再慢.
  142.      *
  143.      *  动画的开始与结束的快慢,有五个预置分别为(下同):
  144.      *  kCAMediaTimingFunctionLinear            线性,即匀速
  145.      *  kCAMediaTimingFunctionEaseIn            先慢后快
  146.      *  kCAMediaTimingFunctionEaseOut           先快后慢
  147.      *  kCAMediaTimingFunctionEaseInEaseOut     先慢后快再慢
  148.      *  kCAMediaTimingFunctionDefault           实际效果是动画中间比较快.
  149.      */
  150.      
  151.     /** timingFunction
  152.      *
  153.      *  当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction
  154.      *  具体参见下面的URL
  155.      *
  156.      *  @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html
  157.      *
  158.      *  + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
  159.      *
  160.      *  - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
  161.      */
  162.      
  163.     animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];
  164.      
  165.     /** fillMode
  166.      *
  167.      *  决定当前对象过了非active时间段的行为,比如动画开始之前,动画结束之后.
  168.      *  预置为:
  169.      *  kCAFillModeRemoved   默认,当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
  170.      *  kCAFillModeForwards  当动画结束后,layer会一直保持着动画最后的状态
  171.      *  kCAFillModeBackwards 和kCAFillModeForwards相对,具体参考上面的URL
  172.      *  kCAFillModeBoth      kCAFillModeForwards和kCAFillModeBackwards在一起的效果
  173.      */
  174.      
  175.     animation.fillMode = kCAFillModeForwards;
  176.      
  177.     /** removedOnCompletion
  178.      *
  179.      *  这个属性默认为YES.一般情况下,不需要设置这个属性.
  180.      *
  181.      *  但如果是CAAnimation动画,并且需要设置 fillMode 属性,那么需要将 removedOnCompletion 设置为NO,否则
  182.      *  fillMode无效
  183.      */
  184.      
  185. //    animation.removedOnCompletion = NO;
  186.      
  187.     /** type
  188.      *
  189.      *  各种动画效果  其中除了'fade', `moveIn', `push' , `reveal' ,其他属于似有的API(我是这么认为的,可以点进去看下注释).
  190.      *  ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
  191.      *  @"cube"                     立方体翻滚效果
  192.      *  @"moveIn"                   新视图移到旧视图上面
  193.      *  @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)
  194.      *  @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果)
  195.      *  @"pageCurl"                 向上翻一页
  196.      *  @"pageUnCurl"               向下翻一页
  197.      *  @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
  198.      *  @"rippleEffect"             滴水效果,(不支持过渡方向)
  199.      *  @"oglFlip"                  上下左右翻转效果
  200.      *  @"rotate"                   旋转效果
  201.      *  @"push"                     
  202.      *  @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)
  203.      *  @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)
  204.      */
  205.      
  206.     /** type
  207.      *
  208.      *  kCATransitionFade            交叉淡化过渡
  209.      *  kCATransitionMoveIn          新视图移到旧视图上面
  210.      *  kCATransitionPush            新视图把旧视图推出去
  211.      *  kCATransitionReveal          将旧视图移开,显示下面的新视图
  212.      */
  213.      
  214.     animation.type = type;
  215.      
  216.     /** subtype
  217.      *
  218.      *  各种动画方向
  219.      *
  220.      *  kCATransitionFromRight;      同字面意思(下同)
  221.      *  kCATransitionFromLeft;
  222.      *  kCATransitionFromTop;
  223.      *  kCATransitionFromBottom;
  224.      */
  225.      
  226.     /** subtype
  227.      *
  228.      *  当type为@"rotate"(旋转)的时候,它也有几个对应的subtype,分别为:
  229.      *  90cw    逆时针旋转90°
  230.      *  90ccw   顺时针旋转90°
  231.      *  180cw   逆时针旋转180°
  232.      *  180ccw  顺时针旋转180°
  233.      */
  234.      
  235.     /**
  236.      *  type与subtype的对应关系(必看),如果对应错误,动画不会显现.
  237.      *
  238.      *  @see http://iphonedevwiki.net/index.php/CATransition
  239.      */
  240.      
  241.     animation.subtype = subType;
  242.      
  243.     /**
  244.      *  所有核心动画和特效都是基于CAAnimation,而CAAnimation是作用于CALayer的.所以把动画添加到layer上.
  245.      *  forKey  可以是任意字符串.
  246.      */
  247.      
  248.     [theView.layer addAnimation:animation forKey:nil];
  249. }

  250. #pragma mark - Preset Animation


  251. + (void)animationRevealFromBottom:(UIView *)view
  252. {
  253.     CATransition *animation = [CATransition animation];
  254.     [animation setDuration:0.35f];
  255.     [animation setType:kCATransitionReveal];
  256.     [animation setSubtype:kCATransitionFromBottom];
  257.     [animation setFillMode:kCAFillModeForwards];
  258.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
  259.      
  260.     [view.layer addAnimation:animation forKey:nil];
  261. }

  262. + (void)animationRevealFromTop:(UIView *)view
  263. {
  264.     CATransition *animation = [CATransition animation];
  265.     [animation setDuration:0.35f];
  266.     [animation setType:kCATransitionReveal];
  267.     [animation setSubtype:kCATransitionFromTop];
  268.     [animation setFillMode:kCAFillModeForwards];
  269.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  270.      
  271.     [view.layer addAnimation:animation forKey:nil];
  272. }

  273. + (void)animationRevealFromLeft:(UIView *)view
  274. {
  275.     CATransition *animation = [CATransition animation];
  276.     [animation setDuration:0.35f];
  277.     [animation setType:kCATransitionReveal];
  278.     [animation setSubtype:kCATransitionFromLeft];
  279.     [animation setFillMode:kCAFillModeForwards];
  280.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  281.      
  282.     [view.layer addAnimation:animation forKey:nil];
  283. }

  284. + (void)animationRevealFromRight:(UIView *)view
  285. {
  286.     CATransition *animation = [CATransition animation];
  287.     [animation setDuration:0.35f];
  288.     [animation setType:kCATransitionReveal];
  289.     [animation setSubtype:kCATransitionFromRight];
  290.     [animation setFillMode:kCAFillModeForwards];
  291.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  292.      
  293.     [view.layer addAnimation:animation forKey:nil];
  294. }


  295. + (void)animationEaseIn:(UIView *)view
  296. {
  297.     CATransition *animation = [CATransition animation];
  298.     [animation setDuration:0.35f];
  299.     [animation setType:kCATransitionFade];
  300.     [animation setFillMode:kCAFillModeForwards];
  301.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
  302.      
  303.     [view.layer addAnimation:animation forKey:nil];
  304. }

  305. + (void)animationEaseOut:(UIView *)view
  306. {
  307.     CATransition *animation = [CATransition animation];
  308.     [animation setDuration:0.35f];
  309.     [animation setType:kCATransitionFade];
  310.     [animation setFillMode:kCAFillModeForwards];
  311.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  312.      
  313.     [view.layer addAnimation:animation forKey:nil];
  314. }


  315. /**
  316. *  UIViewAnimation
  317. *
  318. *  @see    http://www.cocoachina.com/bbs/read.php?tid=110168
  319. *
  320. *  @brief  UIView动画应该是最简单便捷创建动画的方式了,详解请猛戳URL.
  321. *  
  322. *  @method beginAnimations:context 第一个参数用来作为动画的标识,第二个参数给代理代理传递消息.至于为什么一个使用
  323. *                                  nil而另外一个使用NULL,是因为第一个参数是一个对象指针,而第二个参数是基本数据类型.
  324. *  @method setAnimationCurve:      设置动画的加速或减速的方式(速度)
  325. *  @method setAnimationDuration:   动画持续时间
  326. *  @method setAnimationTransition:forView:cache:   第一个参数定义动画类型,第二个参数是当前视图对象,第三个参数是是否使用缓冲区
  327. *  @method commitAnimations        动画结束
  328. */

  329. + (void)animationFlipFromLeft:(UIView *)view
  330. {
  331.     [UIView beginAnimations:nil context:NULL];
  332.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  333.     [UIView setAnimationDuration:0.35f];
  334.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO];
  335.     [UIView commitAnimations];
  336. }

  337. + (void)animationFlipFromRigh:(UIView *)view
  338. {
  339.     [UIView beginAnimations:nil context:NULL];
  340.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  341.     [UIView setAnimationDuration:0.35f];
  342.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:NO];
  343.     [UIView commitAnimations];
  344. }


  345. + (void)animationCurlUp:(UIView *)view
  346. {
  347.     [UIView beginAnimations:nil context:NULL];
  348.     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
  349.     [UIView setAnimationDuration:0.35f];
  350.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:NO];
  351.     [UIView commitAnimations];
  352. }

  353. + (void)animationCurlDown:(UIView *)view
  354. {
  355.     [UIView beginAnimations:nil context:NULL];
  356.     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
  357.     [UIView setAnimationDuration:0.35f];
  358.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:view cache:NO];
  359.     [UIView commitAnimations];
  360. }

  361. + (void)animationPushUp:(UIView *)view
  362. {
  363.     CATransition *animation = [CATransition animation];
  364.     [animation setDuration:0.35f];
  365.     [animation setFillMode:kCAFillModeForwards];
  366.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  367.     [animation setType:kCATransitionPush];
  368.     [animation setSubtype:kCATransitionFromTop];
  369.      
  370.     [view.layer addAnimation:animation forKey:nil];
  371. }

  372. + (void)animationPushDown:(UIView *)view
  373. {
  374.     CATransition *animation = [CATransition animation];
  375.     [animation setDuration:0.35f];
  376.     [animation setFillMode:kCAFillModeForwards];
  377.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  378.     [animation setType:kCATransitionPush];
  379.     [animation setSubtype:kCATransitionFromBottom];
  380.      
  381.     [view.layer addAnimation:animation forKey:nil];
  382. }

  383. + (void)animationPushLeft:(UIView *)view
  384. {
  385.     CATransition *animation = [CATransition animation];
  386.     [animation setDuration:0.35f];
  387.     [animation setFillMode:kCAFillModeForwards];
  388.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  389.     [animation setType:kCATransitionPush];
  390.     [animation setSubtype:kCATransitionFromLeft];
  391.      
  392.     [view.layer addAnimation:animation forKey:nil];
  393. }

  394. + (void)animationPushRight:(UIView *)view
  395. {
  396.     CATransition *animation = [CATransition animation];
  397.     [animation setDuration:0.35f];
  398.     [animation setFillMode:kCAFillModeForwards];
  399.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  400.     [animation setType:kCATransitionPush];
  401.     [animation setSubtype:kCATransitionFromRight];
  402.      
  403.     [view.layer addAnimation:animation forKey:nil];
  404. }

  405. // presentModalViewController
  406. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration
  407. {
  408.     CATransition *animation = [CATransition animation];
  409.     [animation setDuration:duration];
  410.     [animation setFillMode:kCAFillModeForwards];
  411.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  412.     [animation setType:kCATransitionMoveIn];
  413.     [animation setSubtype:kCATransitionFromTop];
  414.      
  415.     [view.layer addAnimation:animation forKey:nil];
  416. }

  417. // dissModalViewController
  418. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration
  419. {
  420.     CATransition *transition = [CATransition animation];
  421.     transition.duration =0.4;
  422.     transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  423.     transition.type = kCATransitionReveal;
  424.     transition.subtype = kCATransitionFromBottom;
  425.     [view.layer addAnimation:transition forKey:nil];
  426. }

  427. + (void)animationMoveLeft:(UIView *)view
  428. {
  429.     CATransition *animation = [CATransition animation];
  430.     [animation setDuration:0.35f];
  431.     [animation setFillMode:kCAFillModeForwards];
  432.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  433.     [animation setType:kCATransitionMoveIn];
  434.     [animation setSubtype:kCATransitionFromLeft];
  435.      
  436.     [view.layer addAnimation:animation forKey:nil];
  437. }

  438. + (void)animationMoveRight:(UIView *)view
  439. {
  440.     CATransition *animation = [CATransition animation];
  441.     [animation setDuration:0.35f];
  442.     [animation setFillMode:kCAFillModeForwards];
  443.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  444.     [animation setType:kCATransitionMoveIn];
  445.     [animation setSubtype:kCATransitionFromRight];
  446.      
  447.     [view.layer addAnimation:animation forKey:nil];
  448. }

  449. +(void)animationRotateAndScaleEffects:(UIView *)view
  450. {
  451.     [UIView animateWithDuration:0.35f animations:^
  452.      {
  453.          /**
  454.           *  @see       http://donbe.blog.163.com/blog/static/138048021201061054243442/
  455.           *
  456.           *  @param     transform   形变属性(结构体),可以利用这个属性去对view做一些翻转或者缩放.详解请猛戳↑URL.
  457.           *
  458.           *  @method    valueWithCATransform3D: 此方法需要一个CATransform3D的结构体.一些非详细的讲解可以看下面的URL
  459.           *
  460.           *  @see       http://blog.csdn.net/liubo0_0/article/details/7452166
  461.           *
  462.           */
  463.           
  464.          view.transform = CGAffineTransformMakeScale(0.001, 0.001);
  465.           
  466.          CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
  467.           
  468.          // 向右旋转45°缩小到最小,然后再从小到大推出.
  469.          animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)];
  470.           
  471.          /**
  472.           *     其他效果:
  473.           *     从底部向上收缩一半后弹出
  474.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)];
  475.           *
  476.           *     从底部向上完全收缩后弹出
  477.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)];
  478.           *
  479.           *     左旋转45°缩小到最小,然后再从小到大推出.
  480.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)];
  481.           *
  482.           *     旋转180°缩小到最小,然后再从小到大推出.
  483.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)];
  484.           */
  485.           
  486.          animation.duration = 0.45;
  487.          animation.repeatCount = 1;
  488.          [view.layer addAnimation:animation forKey:nil];
  489.           
  490.      }
  491.                      completion:^(BOOL finished)
  492.      {
  493.          [UIView animateWithDuration:0.35f animations:^
  494.           {
  495.               view.transform = CGAffineTransformMakeScale(1.0, 1.0);
  496.           }];
  497.      }];
  498. }

  499. /** CABasicAnimation
  500. *
  501. *  @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html
  502. *
  503. *  @brief                      便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个
  504. *                              键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果
  505. *                              具体可以填写什么请参考上面的URL,切勿乱填!
  506. *                              例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度.
  507. *                              这个动画的效果是把view旋转到最小,再旋转回来.
  508. *                              你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类.
  509. *
  510. *  @param toValue              动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为:
  511. *                              fromValue(开始值), toValue(结束值), byValue(偏移值),
  512. !                              这三个属性最多只能同时设置两个;
  513. *                              他们之间的关系如下:
  514. *                              如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue;
  515. *                              如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue;
  516. *                              如果同时设置了byValue  和toValue,那么动画就会从toValue - byValue过渡到toValue;
  517. *
  518. *                              如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value;
  519. *                              如果只设置了toValue  ,那么动画就会从当前的value过渡到toValue;
  520. *                              如果只设置了byValue  ,那么动画就会从从当前的value过渡到当前value + byValue.
  521. *
  522. *                              可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并
  523. *                              同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值.
  524. !                              而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去
  525. *                              完成这些显示效果而已.
  526. *                              在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度.
  527. *
  528. *  @param duration             动画持续时间
  529. *
  530. *  @param timingFunction       动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢...
  531. */

  532. /** CAAnimationGroup
  533. *
  534. *  @brief                      顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画,
  535. *                              把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性,
  536. *                              这时候就可以使用CAAnimationGroup.
  537. *
  538. *  @param duration             动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一
  539. *                              设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性
  540. *                              的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画.
  541. *
  542. *  @param autoreverses         动画完成后自动重新开始,默认为NO.
  543. *
  544. *  @param repeatCount          动画重复次数,默认为0.
  545. *
  546. *  @param animations           动画组(数组类型),把需要同时运行的动画加到这个数组里.
  547. *
  548. *  @note  addAnimation:forKey  这个方法的forKey参数是一个字符串,这个字符串可以随意设置.
  549. *
  550. *  @note                       如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把
  551. *                              removedOnCompletion 设置为NO;
  552. */

  553. + (void)animationRotateAndScaleDownUp:(UIView *)view
  554. {
  555.     CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  556. rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2];
  557. rotationAnimation.duration = 0.35f;
  558. rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  559.      
  560. CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  561. scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];
  562. scaleAnimation.duration = 0.35f;
  563. scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  564.   
  565. CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
  566. animationGroup.duration = 0.35f;
  567. animationGroup.autoreverses = YES;
  568. animationGroup.repeatCount = 1;
  569. animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil];
  570. [view.layer addAnimation:animationGroup forKey:@"animationGroup"];
  571. }



  572. #pragma mark - Private API

  573. + (void)animationFlipFromTop:(UIView *)view
  574. {
  575.     CATransition *animation = [CATransition animation];
  576.     [animation setDuration:0.35f];
  577.     [animation setFillMode:kCAFillModeForwards];
  578.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  579.     [animation setType:@"oglFlip"];
  580.     [animation setSubtype:@"fromTop"];
  581.      
  582.     [view.layer addAnimation:animation forKey:nil];
  583. }

  584. + (void)animationFlipFromBottom:(UIView *)view
  585. {
  586.     CATransition *animation = [CATransition animation];
  587.     [animation setDuration:0.35f];
  588.     [animation setFillMode:kCAFillModeForwards];
  589.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  590.     [animation setType:@"oglFlip"];
  591.     [animation setSubtype:@"fromBottom"];
  592.      
  593.     [view.layer addAnimation:animation forKey:nil];
  594. }

  595. + (void)animationCubeFromLeft:(UIView *)view
  596. {
  597.     CATransition *animation = [CATransition animation];
  598.     [animation setDuration:0.35f];
  599.     [animation setFillMode:kCAFillModeForwards];
  600.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  601.     [animation setType:@"cube"];
  602.     [animation setSubtype:@"fromLeft"];
  603.      
  604.     [view.layer addAnimation:animation forKey:nil];
  605. }

  606. + (void)animationCubeFromRight:(UIView *)view
  607. {
  608.     CATransition *animation = [CATransition animation];
  609.     [animation setDuration:0.35f];
  610.     [animation setFillMode:kCAFillModeForwards];
  611.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  612.     [animation setType:@"cube"];
  613.     [animation setSubtype:@"fromRight"];
  614.      
  615.     [view.layer addAnimation:animation forKey:nil];
  616. }

  617. + (void)animationCubeFromTop:(UIView *)view
  618. {
  619.     CATransition *animation = [CATransition animation];
  620.     [animation setDuration:0.35f];
  621.     [animation setFillMode:kCAFillModeForwards];
  622.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  623.     [animation setType:@"cube"];
  624.     [animation setSubtype:@"fromTop"];
  625.      
  626.     [view.layer addAnimation:animation forKey:nil];
  627. }

  628. + (void)animationCubeFromBottom:(UIView *)view
  629. {
  630.     CATransition *animation = [CATransition animation];
  631.     [animation setDuration:0.35f];
  632.     [animation setFillMode:kCAFillModeForwards];
  633.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  634.     [animation setType:@"cube"];
  635.     [animation setSubtype:@"fromBottom"];
  636.      
  637.     [view.layer addAnimation:animation forKey:nil];
  638. }

  639. + (void)animationSuckEffect:(UIView *)view
  640. {
  641.     CATransition *animation = [CATransition animation];
  642.     [animation setDuration:0.35f];
  643.     [animation setFillMode:kCAFillModeForwards];
  644.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  645.     [animation setType:@"suckEffect"];
  646.      
  647.     [view.layer addAnimation:animation forKey:nil];
  648. }

  649. + (void)animationRippleEffect:(UIView *)view
  650. {
  651.     CATransition *animation = [CATransition animation];
  652.     [animation setDuration:0.35f];
  653.     [animation setFillMode:kCAFillModeForwards];
  654.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  655.     [animation setType:@"rippleEffect"];
  656.      
  657.     [view.layer addAnimation:animation forKey:nil];
  658. }

  659. + (void)animationCameraOpen:(UIView *)view
  660. {
  661.     CATransition *animation = [CATransition animation];
  662.     [animation setDuration:0.35f];
  663.     [animation setFillMode:kCAFillModeForwards];
  664.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  665.     [animation setType:@"cameraIrisHollowOpen"];
  666.     [animation setSubtype:@"fromRight"];
  667.      
  668.     [view.layer addAnimation:animation forKey:nil];
  669. }

  670. + (void)animationCameraClose:(UIView *)view
  671. {
  672.     CATransition *animation = [CATransition animation];
  673.     [animation setDuration:0.35f];
  674.     [animation setFillMode:kCAFillModeForwards];
  675.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  676.     [animation setType:@"cameraIrisHollowClose"];
  677.     [animation setSubtype:@"fromRight"];
  678.      
  679.     [view.layer addAnimation:animation forKey:nil];
  680. }
  681. @end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值