码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-08-07 21:49:51      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://www.cnblogs.com/A--G/p/4711668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!