CABasicAnimation学习Demo 包括了一些常用的动画效果

来源:互联网 发布:遥控软件哪个最好 编辑:程序博客网 时间:2024/06/01 08:44

http://blog.csdn.net/feixiang_song/article/details/30972701?utm_source=tuicool&utm_medium=referral

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  ViewController.m  
  3. //  CABasicAnimationDemo  
  4. //  
  5. //  Created by haotian on 14-6-13.  
  6. //  Copyright (c) 2014年 Baseus. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16. @synthesize ViewTest;  
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     // Do any additional setup after loading the view, typically from a nib.  
  21.       
  22.     //self.ViewTest.backgroundColor = [UIColor redColor];  
  23.     //组合动画调用  
  24.     //[self startAnimation2];  
  25.       
  26.     //永久闪烁动画  
  27.     //[self opacityForever_Animation:0.3];  
  28.       
  29.     ////有闪烁次数的动画  
  30.     //[self opacityTimes_Animation:10 durTimes:0.3];  
  31.       
  32.     //画一条线    路径  
  33.     [self drawACurvedLine];  
  34.       
  35.     //路径动画  
  36.     //[self animateCicleAlongPath];  
  37.       
  38. }  
  39.   
  40. -(void)startAnimation  
  41. {  
  42.     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];  
  43.     animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(5050)];  
  44.     animation.toValue = [NSValue valueWithCGPoint:CGPointMake(300300)];  
  45.     animation.duration = 3.0f;  
  46.     animation.repeatCount = 1;  
  47.     //animation.removedOnCompletion = NO; //完成后是否回到原来状态,如果为NO 就是停留在动画结束时的状态  
  48.     //animation.fillMode = kCAFillModeRemoved;//动画完成后返回到原来状态  
  49.     //animation.fillMode = kCAFillModeBackwards;  
  50.     animation.fillMode = kCAFillModeForwards;//当动画完成时,保留在动画结束的状态  
  51.       
  52.     [self.ViewTest.layer addAnimation:animation forKey:nil];  
  53. }  
  54.   
  55. -(void)startAnimation1  
  56. {  
  57.     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];  
  58.     animation.fromValue = [NSNumber numberWithFloat:0.0f];  
  59.     animation.toValue   = [NSNumber numberWithFloat:10.0f];  
  60.     //animation.duration = 0.5f;  
  61.     //animation.fillMode = kCAFillModeForwards;  
  62.     //animation.removedOnCompletion = NO;  
  63.     //animation.repeatCount = 2;  
  64.     //[self.ViewTest.layer addAnimation:animation forKey:nil];  
  65.       
  66.     CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];  
  67.     animation.fromValue = [NSNumber numberWithFloat:0.0f];  
  68.     animation.toValue   = [NSNumber numberWithFloat:10.0f];  
  69.     //animation.duration = 0.5f;  
  70.     //animation.fillMode = kCAFillModeForwards;  
  71.     //animation.removedOnCompletion = NO;  
  72.     //animation.repeatCount = 2;  
  73.     //[self.ViewTest.layer addAnimation:animation1 forKey:nil];  
  74.   
  75.     CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];  
  76.     groupAnimation.duration = 2.0f;  
  77.     groupAnimation.autoreverses  = YES;  
  78.     groupAnimation.repeatCount = 5;  
  79.     [groupAnimation setAnimations:[NSArray arrayWithObjects:animation,animation1, nil nil]];  
  80.       
  81.     [self.ViewTest.layer addAnimation:groupAnimation forKey:nil];  
  82. }  
  83.   
  84. //组合动画  
  85. -(void)startAnimation2  
  86. {  
  87.     //界限  
  88.     CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];  
  89.     boundsAnimation.fromValue = [NSValue valueWithCGRectself.ViewTest.bounds];  
  90.     boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectZero];  
  91.     //透明度变化  
  92.     CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];  
  93.     opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];  
  94.     opacityAnimation.toValue = [NSNumber numberWithFloat:0.5];  
  95.       
  96.     //位置移动  
  97.     CABasicAnimation *animation  = [CABasicAnimation animationWithKeyPath:@"position"];  
  98.     animation.fromValue =  [NSValue valueWithCGPointself.ViewTest.layer.position];  
  99.     CGPoint toPoint = self.ViewTest.layer.position;  
  100.     toPoint.x += 180;  
  101.     animation.toValue = [NSValue valueWithCGPoint:toPoint];  
  102.       
  103.     //旋转动画  
  104.     CABasicAnimation* rotationAnimation =  
  105.     [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//"z"还可以是“x”“y”,表示沿z轴旋转  
  106.       
  107.     rotationAnimation.toValue = [NSNumber numberWithFloat:(22 * M_PI) * 3];  
  108.     // 3 is the number of 360 degree rotations  
  109.       
  110.     // Make the rotation animation duration slightly less than the other animations to give it the feel  
  111.     // that it pauses at its largest scale value  
  112.     rotationAnimation.duration = 3.0f;  
  113.     rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //缓入缓出  
  114.     //rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];  
  115.       
  116.     //缩放动画  
  117.     CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];  
  118.     scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];  
  119.     scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];  
  120.     scaleAnimation.duration = 3.0f;  
  121.     scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
  122.     //组合动画  
  123.     CAAnimationGroup *animationGroup = [CAAnimationGroup animation];  
  124.     animationGroup.duration = 3.0f;  
  125.     animationGroup.autoreverses = YES;   //是否重播,原动画的倒播  
  126.     animationGroup.repeatCount = NSNotFound;//HUGE_VALF;     //HUGE_VALF,源自math.h  
  127.     [animationGroup setAnimations:[NSArray arrayWithObjects:rotationAnimation, scaleAnimation,boundsAnimation, nil nil]];  
  128.       
  129.     //将上述两个动画编组  
  130.       
  131.     [self.ViewTest.layer addAnimation:animationGroup forKey:@"animationGroup"];  
  132. }  
  133.   
  134. //永久闪烁的动画  
  135. -(void)opacityForever_Animation:(float)time  
  136. {  
  137.     CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];  
  138.     animation.fromValue=[NSNumber numberWithFloat:1.0];  
  139.     animation.toValue=[NSNumber numberWithFloat:0.0];  
  140.     animation.autoreverses=YES;  
  141.     animation.duration=time;  
  142.     animation.repeatCount=FLT_MAX;  
  143.     animation.removedOnCompletion=NO;  
  144.     animation.fillMode=kCAFillModeForwards;  
  145.       
  146.     [self.ViewTest.layer addAnimation:animation forKey:@"opacityForever"];  
  147. }  
  148. /**************************************************************************/  
  149.   
  150. //有闪烁次数的动画  
  151.   
  152. -(void)opacityTimes_Animation:(float)repeatTimes durTimes:(float)time;  
  153. {  
  154.     CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];  
  155.     animation.fromValue=[NSNumber numberWithFloat:1.0];  
  156.     animation.toValue=[NSNumber numberWithFloat:0.4];  
  157.     animation.repeatCount=repeatTimes;  
  158.     animation.duration=time;  
  159.     animation.removedOnCompletion=NO;  
  160.     animation.fillMode=kCAFillModeForwards;  
  161.     animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];  
  162.     animation.autoreverses=YES;  
  163.       
  164.     [self.ViewTest.layer addAnimation:animation forKey:@"opacityTimes"];  
  165.       
  166. }  
  167. /**************************************************************************/  
  168. //路径动画  
  169.   
  170. -(void)keyframeAniamtion:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes  
  171. {  
  172.       
  173. }  
  174.   
  175. //在视图中画一条线  
  176. -(void)drawACurvedLine  
  177. {  
  178.     UIGraphicsBeginImageContext(CGSizeMake(320460));  
  179.     CGContextRef context = UIGraphicsGetCurrentContext();  
  180.       
  181.     CGContextSetLineWidth(context, 3);  
  182.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  
  183.     //设置起点  
  184.     CGContextMoveToPoint(context, 1010);  
  185.       
  186.     CGContextAddQuadCurveToPoint(context, 10450310450);  
  187.     //CGContextAddQuadCurveToPoint(context, 310, 10, 10, 10);  
  188.       
  189.     //划线  
  190.     CGContextDrawPath(context, kCGPathStroke);  
  191.       
  192.     //得到一个image 从目前的矢量上下文  
  193.     UIImage *curve = UIGraphicsGetImageFromCurrentImageContext();  
  194.     UIGraphicsEndImageContext();  
  195.       
  196.     UIImageView *curveView = [[UIImageView alloc]initWithImage:curve];  
  197.     curveView.frame = CGRectMake(11320460);  
  198.     [curveView setBackgroundColor:[UIColor clearColor]];  
  199.     [self.view addSubview:curveView];  
  200. }  
  201.   
  202. -(void)animateCicleAlongPath  
  203. {  
  204.     //准备关键帧动画  
  205.     CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
  206.     pathAnimation.calculationMode = kCAAnimationPaced;  
  207.     pathAnimation.fillMode = kCAFillModeForwards;  
  208.     pathAnimation.removedOnCompletion = NO;  
  209.     pathAnimation.duration = 5.0f;  
  210.     pathAnimation.repeatCount = 200;  
  211.     //pathAnimation.autoreverses = YES; //原路返回,而不是从头再来  
  212.     //设置动画路径  
  213.     //CGPoint endPoint = CGPointMake(310, 450);  
  214.     CGMutablePathRef curvedPath = CGPathCreateMutable();  
  215.     CGPathMoveToPoint(curvedPath, NULL1010);  
  216.     CGPathAddQuadCurveToPoint(curvedPath, NULL10450310450);  
  217.     CGPathAddQuadCurveToPoint(curvedPath, NULL310101010);  
  218.     //已经有了路径,我们要告诉动画  路径  
  219.     pathAnimation.path = curvedPath;  
  220.     CGPathRelease(curvedPath);  //这里要手工释放  
  221.       
  222.     [self.ViewTest.layer addAnimation:pathAnimation forKey:nil];  
  223.       
  224. }  
  225. /**************************************************************************/  
  226.   
  227.   
  228.   
  229. - (void)didReceiveMemoryWarning  
  230. {  
  231.     [super didReceiveMemoryWarning];  
  232.     // Dispose of any resources that can be recreated.  
  233. }  
  234.   
  235. @end  

 

最后附上Demo下载地址:http://download.csdn.net/detail/u012951123/7500419


0 0
原创粉丝点击