iOS經常使用動畫-b

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