#define kRadianToDegrees (radian) (radian * 180.0) / (M_PI)
- //闪烁
[self.testView.layer addAnimation:[self opacityForever_Animation:0.5] forKey:nil];
- //移动
[self.testView.layer addAnimation:[self duration:3 move:[NSNumber numberWithInteger:200]] forKey:nil];
- //缩放
[self.testView.layer addAnimation:[self scale:[NSNumber numberWithInteger:1] orgin:[NSNumber numberWithInteger:3] durTimes:1 Rep:MAXFLOAT] forKey:nil] ;
- //组合
NSArray *myArray = [NSArray arrayWithObjects:[self opacityForever_Animation:0.5], [self duration:1.0f move:[NSNumber numberWithFloat:200.0f]], [self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:3.0f] durTimes:2.0f Rep:MAXFLOAT], nil];
[self.testView.layer addAnimation:[self groupAnimation:myArray durTimes:3.0f Rep:MAXFLOAT] forKey:nil];
- //路径
CGMutablePathRef myPah = CGPathCreateMutable();
CGPathMoveToPoint(myPah, nil,30, 77);
CGPathAddCurveToPoint(myPah, nil, 50, 50, 60, 200, 200, 200);//这里的是控制点。
[self.testView.layer addAnimation:[self keyframeAnimation:myPah durTimes:5 Rep:MAXFLOAT] forKey:nil];
- //旋转
[self.testView.layer addAnimation:[self rotation:2 degree:kRadianToDegrees(90) direction:1 repeatCount:MAXFLOAT] forKey:nil];
- //移除
[self.testView.layer removeAllAnimations];
/********************************方法的实现**************************************/
- #pragma mark === 闪烁 ======
1 - (CABasicAnimation *)opacityForever_Animation:(float)time 2 3 { 4 5 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。 6 7 animation.fromValue = [NSNumber numberWithFloat:1.0f]; 8 9 animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。 10 11 animation.autoreverses = YES; 12 13 animation.duration = time; 14 15 animation.repeatCount = MAXFLOAT; 16 17 animation.removedOnCompletion = NO; 18 19 animation.fillMode = kCAFillModeForwards; 20 21 animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。 22 23 return animation; 24 25 }
- #pragma mark =====横向、纵向移动===========
1 -(CABasicAnimation *)duration:(float)time move:(NSNumber *)x 2 3 { 4 5 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];///.y的话就向下移动。 6 7 animation.toValue = x; 8 9 animation.duration = time; 10 11 animation.removedOnCompletion = NO;//yes的话,又返回原位置了。 12 13 animation.repeatCount = MAXFLOAT; 14 15 animation.fillMode = kCAFillModeForwards; 16 17 return animation; 18 19 }
- #pragma mark =====缩放-=============
1 -(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repertTimes 2 3 { 4 5 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 6 7 animation.fromValue = Multiple; 8 9 animation.toValue = orginMultiple; 10 11 animation.autoreverses = YES; 12 13 animation.repeatCount = repertTimes; 14 15 animation.duration = time;//不设置时候的话,有一个默认的缩放时间. 16 17 animation.removedOnCompletion = NO; 18 19 animation.fillMode = kCAFillModeForwards; 20 21 return animation; 22 23 }
- #pragma mark =====组合动画-=============
1 -(CAAnimationGroup *)groupAnimation:(NSArray *)animationAry durTimes:(float)time Rep:(float)repeatTimes 2 3 { 4 5 CAAnimationGroup *animation = [CAAnimationGroup animation]; 6 7 animation.animations = animationAry; 8 9 animation.duration = time; 10 11 animation.removedOnCompletion = NO; 12 13 animation.repeatCount = repeatTimes; 14 15 animation.fillMode = kCAFillModeForwards; 16 17 return animation; 18 19 }
- #pragma mark =====路径动画-=============
1 -(CAKeyframeAnimation *)keyframeAnimation:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes 2 3 { 4 5 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 6 7 animation.path = path; 8 9 animation.removedOnCompletion = NO; 10 11 animation.fillMode = kCAFillModeForwards; 12 13 animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 14 15 animation.autoreverses = NO; 16 17 animation.duration = time; 18 19 animation.repeatCount = repeatTimes; 20 21 return animation; 22 23 } 24 25 #pragma mark ====旋转动画====== 26 -(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount 27 28 { 29 30 CATransform3D rotationTransform = CATransform3DMakeRotation(degree, 0, 0, direction); 31 32 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 33 34 animation.toValue = [NSValue valueWithCATransform3D:rotationTransform]; 35 36 animation.duration = dur; 37 38 animation.autoreverses = NO; 39 40 animation.cumulative = NO; 41 42 animation.fillMode = kCAFillModeForwards; 43 44 animation.repeatCount = repeatCount; 45 46 animation.delegate = self; 47 48 return animation; 49 50 }