iOS常用动画


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值