1 //创建CABasicAnimation对象 2 CABasicAnimation *animation = [CABasicAnimation animation]; 3 //对其keypath赋值,大小(旋转,平移,缩放) 4 animation.keyPath = @"transform"; 5 //动画大小从多打开始 6 animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.greenView.layer.transform, M_PI_4, 0, 0, 1)]; 7 //动画大小到多大开始结束 8 animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.greenView.layer.transform, -M_PI_4, 0, 0, 1)]; 9 10 //如果希望保持动画最后状态 11 animation.removedOnCompletion = NO; 12 13 //设置fillMode 14 animation.fillMode = kCAFillModeForwards; 15 /* 16 CA_EXTERN NSString * const kCAFillModeForwards 17 CA_EXTERN NSString * const kCAFillModeBackwards 18 CA_EXTERN NSString * const kCAFillModeBoth 19 CA_EXTERN NSString * const kCAFillModeRemoved 20 */ 21 /* 22 fillMode的作用就是决定layer对象执行完动画后的行为。该属性只有在removedOnCompletion设置为NO才会有效。 23 > kCAFillModeForwards:当动画结束后,layer会一直保持着动画最后的状态 24 > kCAFillModeBackwards:只要动画被加入了layer,layer便立刻处于动画初始状态。将一个动画加入一个layer的时候延迟5秒执行即可测试出来。设置核心动画 延时anim.beginTime = CACurrentMediaTime() + 2;其中CACurrentMediaTime获得当前动画时间。这里表示延时两秒。 25 > kCAFillModeBoth:动画加入layer后,在开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态. 26 > kCAFillModeRemoved: 默认值,动画结束后,layer会恢复到之前的状态。 27 */ 28 29 30 //设置持续时间 31 animation.duration = 2; 32 //添加到view 33 [self.greenView.layer addAnimation:animation forKey:nil];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//创建CABasicAnimation对象 CABasicAnimation *animation = [CABasicAnimation animation]; //对其keypath赋值, animation.keyPath = @"position"; //动画从什么地方开始 animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)]; //动画从什么地方结束 animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)]; //设置延长时间 animation.duration = 2; //添加到view [self.greenView.layer addAnimation:animation forKey:nil]; }
//创建CABasicAnimation对象 CABasicAnimation *animation = [CABasicAnimation animation]; //对其keypath赋值,大小 animation.keyPath = @"bounds"; //动画大小从多打开始 animation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)]; //动画大小到多大开始结束 animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; //设置持续时间 animation.duration = 2; //添加到view [self.greenView.layer addAnimation:animation forKey:nil];