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

简介: <div class="bar" style="padding-left:45px; font-family:Consolas,'Courier New',Courier,mono,serif; line-height:26px; background-color:rgb(231,229,220)"> <div class="tools" style="padding:3px 8px 1
[cpp]  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 
  385. forView:view cache:NO];  
  386.     [UIView commitAnimations];  
  387. }  
  388.    
  389. + (void)animationFlipFromRigh:(UIView *)view  
  390. {  
  391.     [UIView beginAnimations:nil context:NULL];  
  392.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  393.     [UIView setAnimationDuration:0.35f];  
  394.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight 
  395. forView:view cache:NO];  
  396.     [UIView commitAnimations];  
  397. }  
  398.    
  399.    
  400. + (void)animationCurlUp:(UIView *)view  
  401. {  
  402.     [UIView beginAnimations:nil context:NULL];  
  403.     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];  
  404.     [UIView setAnimationDuration:0.35f];  
  405.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp 
  406. forView:view cache:NO];  
  407.     [UIView commitAnimations];  
  408. }  
  409.    
  410. + (void)animationCurlDown:(UIView *)view  
  411. {  
  412.     [UIView beginAnimations:nil context:NULL];  
  413.     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];  
  414.     [UIView setAnimationDuration:0.35f];  
  415.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 
  416. forView:view cache:NO];  
  417.     [UIView commitAnimations];  
  418. }  
  419.    
  420. + (void)animationPushUp:(UIView *)view  
  421. {  
  422.     CATransition *animation = [CATransition animation];  
  423.     [animation setDuration:0.35f];  
  424.     [animation setFillMode:kCAFillModeForwards];  
  425.     [animation setTimingFunction:[CAMediaTimingFunction 
  426. functionWithName:kCAMediaTimingFunctionEaseOut]];  
  427.     [animation setType:kCATransitionPush];  
  428.     [animation setSubtype:kCATransitionFromTop];  
  429.        
  430.     [view.layer addAnimation:animation forKey:nil];  
  431. }  
  432.    
  433. + (void)animationPushDown:(UIView *)view  
  434. {  
  435.     CATransition *animation = [CATransition animation];  
  436.     [animation setDuration:0.35f];  
  437.     [animation setFillMode:kCAFillModeForwards];  
  438.     [animation setTimingFunction:[CAMediaTimingFunction 
  439. functionWithName:kCAMediaTimingFunctionEaseOut]];  
  440.     [animation setType:kCATransitionPush];  
  441.     [animation setSubtype:kCATransitionFromBottom];  
  442.        
  443.     [view.layer addAnimation:animation forKey:nil];  
  444. }  
  445.    
  446. + (void)animationPushLeft:(UIView *)view  
  447. {  
  448.     CATransition *animation = [CATransition animation];  
  449.     [animation setDuration:0.35f];  
  450.     [animation setFillMode:kCAFillModeForwards];  
  451.     [animation setTimingFunction:[CAMediaTimingFunction
  452.  functionWithName:kCAMediaTimingFunctionEaseOut]];  
  453.     [animation setType:kCATransitionPush];  
  454.     [animation setSubtype:kCATransitionFromLeft];  
  455.        
  456.     [view.layer addAnimation:animation forKey:nil];  
  457. }  
  458.    
  459. + (void)animationPushRight:(UIView *)view  
  460. {  
  461.     CATransition *animation = [CATransition animation];  
  462.     [animation setDuration:0.35f];  
  463.     [animation setFillMode:kCAFillModeForwards];  
  464.     [animation setTimingFunction:[CAMediaTimingFunction 
  465. functionWithName:kCAMediaTimingFunctionEaseOut]];  
  466.     [animation setType:kCATransitionPush];  
  467.     [animation setSubtype:kCATransitionFromRight];  
  468.        
  469.     [view.layer addAnimation:animation forKey:nil];  
  470. }  
  471.    
  472. // presentModalViewController  
  473. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration  
  474. {  
  475.     CATransition *animation = [CATransition animation];  
  476.     [animation setDuration:duration];  
  477.     [animation setFillMode:kCAFillModeForwards];  
  478.     [animation setTimingFunction:[CAMediaTimingFunction 
  479. functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];  
  480.     [animation setType:kCATransitionMoveIn];  
  481.     [animation setSubtype:kCATransitionFromTop];  
  482.        
  483.     [view.layer addAnimation:animation forKey:nil];  
  484. }  
  485.    
  486. // dissModalViewController  
  487. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration  
  488. {  
  489.     CATransition *transition = [CATransition animation];  
  490.     transition.duration =0.4;  
  491.     transition.timingFunction = [CAMediaTimingFunction 
  492. functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
  493.     transition.type = kCATransitionReveal;  
  494.     transition.subtype = kCATransitionFromBottom;  
  495.     [view.layer addAnimation:transition forKey:nil];  
  496. }  
  497.    
  498. + (void)animationMoveLeft:(UIView *)view  
  499. {  
  500.     CATransition *animation = [CATransition animation];  
  501.     [animation setDuration:0.35f];  
  502.     [animation setFillMode:kCAFillModeForwards];  
  503.     [animation setTimingFunction:[CAMediaTimingFunction 
  504. functionWithName:kCAMediaTimingFunctionEaseOut]];  
  505.     [animation setType:kCATransitionMoveIn];  
  506.     [animation setSubtype:kCATransitionFromLeft];  
  507.        
  508.     [view.layer addAnimation:animation forKey:nil];  
  509. }  
  510.    
  511. + (void)animationMoveRight:(UIView *)view  
  512. {  
  513.     CATransition *animation = [CATransition animation];  
  514.     [animation setDuration:0.35f];  
  515.     [animation setFillMode:kCAFillModeForwards];  
  516.     [animation setTimingFunction:[CAMediaTimingFunction 
  517. functionWithName:kCAMediaTimingFunctionEaseOut]];  
  518.     [animation setType:kCATransitionMoveIn];  
  519.     [animation setSubtype:kCATransitionFromRight];  
  520.        
  521.     [view.layer addAnimation:animation forKey:nil];  
  522. }  
  523.    
  524. +(void)animationRotateAndScaleEffects:(UIView *)view  
  525. {  
  526.     [UIView animateWithDuration:0.35f animations:^  
  527.      {  
  528.          /** 
  529.           *  @see       http://donbe.blog.163.com/blog/static/138048021201061054243442/ 
  530.           * 
  531.           *  @param     transform   形变属性(结构体),可以利用这个属性去对view做一些翻转或者缩放.详解请猛戳↑URL. 
  532.           * 
  533.           *  @method    valueWithCATransform3D: 此方法需要一个CATransform3D的结构体.一些非详细的讲解可以看下面的URL 
  534.           * 
  535.           *  @see       http://blog.csdn.net/liubo0_0/article/details/7452166 
  536.           * 
  537.           */  
  538.             
  539.          view.transform = CGAffineTransformMakeScale(0.001, 0.001);  
  540.             
  541.          CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];  
  542.             
  543.          // 向右旋转45°缩小到最小,然后再从小到大推出.  
  544.          animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)];  
  545.             
  546.          /** 
  547.           *     其他效果: 
  548.           *     从底部向上收缩一半后弹出 
  549.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)]; 
  550.           * 
  551.           *     从底部向上完全收缩后弹出 
  552.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)]; 
  553.           * 
  554.           *     左旋转45°缩小到最小,然后再从小到大推出. 
  555.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)]; 
  556.           * 
  557.           *     旋转180°缩小到最小,然后再从小到大推出. 
  558.           *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)]; 
  559.           */  
  560.             
  561.          animation.duration = 0.45;  
  562.          animation.repeatCount = 1;  
  563.          [view.layer addAnimation:animation forKey:nil];  
  564.             
  565.      }  
  566.                      completion:^(BOOL finished)  
  567.      {  
  568.          [UIView animateWithDuration:0.35f animations:^  
  569.           {  
  570.               view.transform = CGAffineTransformMakeScale(1.0, 1.0);  
  571.           }];  
  572.      }];  
  573. }  
  574.    
  575. /** CABasicAnimation 
  576.  * 
  577.  *  @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html 
  578.  * 
  579.  *  @brief                      便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个 
  580.  *                              键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果 
  581.  *                              具体可以填写什么请参考上面的URL,切勿乱填! 
  582.  *                              例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度. 
  583.  *                              这个动画的效果是把view旋转到最小,再旋转回来. 
  584.  *                              你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类. 
  585.  * 
  586.  *  @param toValue              动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为: 
  587.  *                              fromValue(开始值), toValue(结束值), byValue(偏移值), 
  588.  !                              这三个属性最多只能同时设置两个; 
  589.  *                              他们之间的关系如下: 
  590.  *                              如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue; 
  591.  *                              如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue; 
  592.  *                              如果同时设置了byValue  和toValue,那么动画就会从toValue - byValue过渡到toValue; 
  593.  * 
  594.  *                              如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value; 
  595.  *                              如果只设置了toValue  ,那么动画就会从当前的value过渡到toValue; 
  596.  *                              如果只设置了byValue  ,那么动画就会从从当前的value过渡到当前value + byValue. 
  597.  * 
  598.  *                              可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并 
  599.  *                              同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值. 
  600.  !                              而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去 
  601.  *                              完成这些显示效果而已. 
  602.  *                              在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度. 
  603.  * 
  604.  *  @param duration             动画持续时间 
  605.  * 
  606.  *  @param timingFunction       动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢... 
  607.  */  
  608.    
  609. /** CAAnimationGroup 
  610.  * 
  611.  *  @brief                      顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画, 
  612.  *                              把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性, 
  613.  *                              这时候就可以使用CAAnimationGroup. 
  614.  * 
  615.  *  @param duration             动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一 
  616.  *                              设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性 
  617.  *                              的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画. 
  618.  * 
  619.  *  @param autoreverses         动画完成后自动重新开始,默认为NO. 
  620.  * 
  621.  *  @param repeatCount          动画重复次数,默认为0. 
  622.  * 
  623.  *  @param animations           动画组(数组类型),把需要同时运行的动画加到这个数组里. 
  624.  * 
  625.  *  @note  addAnimation:forKey  这个方法的forKey参数是一个字符串,这个字符串可以随意设置. 
  626.  * 
  627.  *  @note                       如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把 
  628.  *                              removedOnCompletion 设置为NO; 
  629.  */  
  630.    
  631. + (void)animationRotateAndScaleDownUp:(UIView *)view  
  632. {  
  633.     CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  
  634.  rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2];  
  635.  rotationAnimation.duration = 0.35f;  
  636.  rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
  637.        
  638.  CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];  
  639.  scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];  
  640.  scaleAnimation.duration = 0.35f;  
  641.  scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
  642.     
  643.  CAAnimationGroup *animationGroup = [CAAnimationGroup animation];  
  644.  animationGroup.duration = 0.35f;  
  645.  animationGroup.autoreverses = YES;  
  646.  animationGroup.repeatCount = 1;  
  647.  animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil];  
  648.  [view.layer addAnimation:animationGroup forKey:@"animationGroup"];  
  649. }  
  650.    
  651.    
  652.    
  653. #pragma mark - Private API  
  654.    
  655. + (void)animationFlipFromTop:(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:@"fromTop"];  
  663.        
  664.     [view.layer addAnimation:animation forKey:nil];  
  665. }  
  666.    
  667. + (void)animationFlipFromBottom:(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:@"oglFlip"];  
  674.     [animation setSubtype:@"fromBottom"];  
  675.        
  676.     [view.layer addAnimation:animation forKey:nil];  
  677. }  
  678.    
  679. + (void)animationCubeFromLeft:(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:@"fromLeft"];  
  687.        
  688.     [view.layer addAnimation:animation forKey:nil];  
  689. }  
  690.    
  691. + (void)animationCubeFromRight:(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:@"fromRight"];  
  699.        
  700.     [view.layer addAnimation:animation forKey:nil];  
  701. }  
  702.    
  703. + (void)animationCubeFromTop:(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:@"fromTop"];  
  711.        
  712.     [view.layer addAnimation:animation forKey:nil];  
  713. }  
  714.    
  715. + (void)animationCubeFromBottom:(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:@"cube"];  
  722.     [animation setSubtype:@"fromBottom"];  
  723.        
  724.     [view.layer addAnimation:animation forKey:nil];  
  725. }  
  726.    
  727. + (void)animationSuckEffect:(UIView *)view  
  728. {  
  729.     CATransition *animation = [CATransition animation];  
  730.     [animation setDuration:0.35f];  
  731.     [animation setFillMode:kCAFillModeForwards];  
  732.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];  
  733.     [animation setType:@"suckEffect"];  
  734.        
  735.     [view.layer addAnimation:animation forKey:nil];  
  736. }  
  737.    
  738. + (void)animationRippleEffect:(UIView *)view  
  739. {  
  740.     CATransition *animation = [CATransition animation];  
  741.     [animation setDuration:0.35f];  
  742.     [animation setFillMode:kCAFillModeForwards];  
  743.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];  
  744.     [animation setType:@"rippleEffect"];  
  745.        
  746.     [view.layer addAnimation:animation forKey:nil];  
  747. }  
  748.    
  749. + (void)animationCameraOpen:(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:@"cameraIrisHollowOpen"];  
  756.     [animation setSubtype:@"fromRight"];  
  757.        
  758.     [view.layer addAnimation:animation forKey:nil];  
  759. }  
  760.    
  761. + (void)animationCameraClose:(UIView *)view  
  762. {  
  763.     CATransition *animation = [CATransition animation];  
  764.     [animation setDuration:0.35f];  
  765.     [animation setFillMode:kCAFillModeForwards];  
  766.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];  
  767.     [animation setType:@"cameraIrisHollowClose"];  
  768.     [animation setSubtype:@"fromRight"];  
  769.        
  770.     [view.layer addAnimation:animation forKey:nil];  
  771. }  
  772. @end  
目录
相关文章
|
7月前
|
iOS开发
iOS 动画绘制圆形
iOS 动画绘制圆形
42 1
|
JSON 缓存 Android开发
iOS高质量的动画实现解决方案——Lottie
iOS高质量的动画实现解决方案——Lottie
987 0
|
iOS开发
iOS开发-导航栏标题动画
iOS开发-导航栏标题动画
159 0
iOS开发-导航栏标题动画
|
iOS开发
iOS - 个人中心果冻弹性下拉动画
iOS - 个人中心果冻弹性下拉动画
212 0
iOS - 个人中心果冻弹性下拉动画
|
iOS开发
iOS开发 - 柱状图动态展现动画
iOS开发 - 柱状图动态展现动画
127 0
iOS开发 - 柱状图动态展现动画
|
JSON iOS开发 数据格式
iOS开发 - 关于启动页动画的杂谈
iOS开发 - 关于启动页动画的杂谈
211 0
iOS开发 - 关于启动页动画的杂谈
|
iOS开发
iOS开发- 分屏动画
iOS开发- 分屏动画
102 0
iOS开发- 分屏动画
|
iOS开发
IOS动画
IOS动画
59 0
iPhone7、7P iOS10.2及以下系统转场动画出现白屏bug的解决办法
iPhone7、7P iOS10.2及以下系统转场动画出现白屏bug的解决办法
233 0
|
iOS开发 UED
iOS以动画的形式更新Masonry约束
iOS以动画的形式更新Masonry约束
521 0
iOS以动画的形式更新Masonry约束