1 // 2 // ViewController.m 3 // 05 基本动画 4 // 5 // Created by ZhuJiaCong on 16/4/18. 6 // Copyright © 2016年 wxhl. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 13 14 @end 15 16 @implementation ViewController 17 18 19 20 - (IBAction)start:(id)sender { 21 22 //1 初始化动画对象 23 //keyPath在动画过程中,需要改变的值 24 CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 25 //2设置属性改变的值 26 basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 0)]; 27 basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(150, 500)]; 28 29 //动画持续时间 30 basicAnimation.duration = 3; 31 32 //动画自动回播 33 //在动画结束后,回自动的倒序播放动画,返回动画起始状态 34 //单次播放时间不变,总时间为原来的两倍 35 basicAnimation.autoreverses = NO; 36 37 //循环次数 不会改变每一次动画持续的时间 38 basicAnimation.repeatCount = 5; 39 //设定动画的开始时间 40 // basicAnimation.beginTime = CACurrentMediaTime() + 3; 41 //动画的填充模式 42 /* 43 kCAFillModeForwards 在动画结束播放之后,将图层保留结束为止 44 kCAFillModeBackwards 在动画没有开始之前,将图层保留在起点位置 45 kCAFillModeBoth 上面两者的集合 46 kCAFillModeRemoved 当动画结束之后,移除动画效果,图层返回最初时状态 47 */ 48 basicAnimation.removedOnCompletion = NO; 49 50 basicAnimation.fillMode = kCAFillModeBoth; 51 52 53 //播放速度 不会影响等待的时间 54 // basicAnimation.speed = 2.0; 55 //时间的偏移量 56 basicAnimation.timeOffset = 1.5; 57 58 59 //将动画添加到图层上去 60 [_imageView.layer addAnimation:basicAnimation forKey:@"basic"]; 61 62 } 63 64 - (void)viewDidLoad { 65 [super viewDidLoad]; 66 // Do any additional setup after loading the view, typically from a nib. 67 } 68 69 - (void)didReceiveMemoryWarning { 70 [super didReceiveMemoryWarning]; 71 // Dispose of any resources that can be recreated. 72 } 73 74 @end
时间: 2024-11-04 20:48:04