iOS中的CABasicAnimation的相关学习示例
CABasicAnimation 只有三个常用到的属性:fromValue toValue ByValue;
下面是他的一些参数说明:
(1).Autoreverses
当你设定这个属性为 YES 时,在它到达目的地之后,动画的返回到开始的值,代替了直接跳转到 开始的值。
(1).Duration
Duration 这个参数你已经相当熟悉了。它设定开始值到结束值花费的时间。期间会被速度的属性所影响。 RemovedOnCompletion
这个属性默认为 YES,那意味着,在指定的时间段完成后,动画就自动的从层上移除了。这个一般不用。
假如你想要再次用这个动画时,你需要设定这个属性为 NO。这样的话,下次你在通过-set 方法设定动画的属 性时,它将再次使用你的动画,而非默认的动画。
(2).Speed
默认的值为 1.0.这意味着动画播放按照默认的速度。如果你改变这个值为 2.0,动画会用 2 倍的速度播放。 这样的影响就是使持续时间减半。如果你指定的持续时间为 6 秒,速度为 2.0,动画就会播放 3 秒钟---一半的 持续时间。
(3).BeginTime
这个属性在组动画中很有用。它根据父动画组的持续时间,指定了开始播放动画的时间。默认的是 0.0.组 动画在下个段落中讨论“Animation Grouping”。
(4).TimeOffset
如果一个时间偏移量是被设定,动画不会真正的可见,直到根据父动画组中的执行时间得到的时间都流逝 了。
(5).RepeatCount
默认的是 0,意味着动画只会播放一次。如果指定一个无限大的重复次数,使用 1e100f。这个不应该和 repeatDration 属性一块使用。
(6).RepeatDuration
这个属性指定了动画应该被重复多久。动画会一直重复,直到设定的时间流逝完。它不应该和 repeatCount 一起使用。
我们可以通过animationWithKeyPath键值对的方式来改变动画,具体参数如下:
transform.scale = 比例轉換
transform.scale.x = 闊的比例轉換
transform.scale.y = 高的比例轉換
transform.rotation.z = 平面圖的旋轉
opacity = 透明度
margin
zPosition
backgroundColor 背景颜色
cornerRadius 圆角
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
下面是一些app中常用到的动画效果,用以记录方便以后使用
//永久闪烁的动画
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画 2 3 { 4 5 CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"]; 6 7 animation.fromValue=[NSNumber numberWithFloat:1.0]; 8 9 animation.toValue=[NSNumber numberWithFloat:0.0]; 10 11 animation.autoreverses=YES; 12 13 animation.duration=time; 14 15 animation.repeatCount=FLT_MAX; 16 17 animation.removedOnCompletion=NO; 18 19 animation.fillMode=kCAFillModeForwards; 20 21 return animation; 22 23 }
//有闪烁次数的动画
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CABasicAnimation *)opacityTimes_Animation:(float)repeatTimes durTimes:(float)time; 2 3 { 4 5 CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"]; 6 7 animation.fromValue=[NSNumber numberWithFloat:1.0]; 8 9 animation.toValue=[NSNumber numberWithFloat:0.4]; 10 11 animation.repeatCount=repeatTimes; 12 13 animation.duration=time; 14 15 animation.removedOnCompletion=NO; 16 17 animation.fillMode=kCAFillModeForwards; 18 19 animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 20 21 animation.autoreverses=YES; 22 23 return animation; 24 25 }
//横向移动
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x 2 3 { 4 5 CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; 6 7 animation.toValue=x; 8 9 animation.duration=time; 10 11 animation.removedOnCompletion=NO; 12 13 animation.fillMode=kCAFillModeForwards; 14 15 return animation; 16 17 }
//纵向移动
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CABasicAnimation *)moveY:(float)time Y:(NSNumber *)y 2 3 { 4 5 CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"]; 6 7 animation.toValue=y; 8 9 animation.duration=time; 10 11 animation.removedOnCompletion=NO; 12 13 animation.fillMode=kCAFillModeForwards; 14 15 return animation; 16 17 }
//缩放
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repeatTimes 2 { 3 4 CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"]; 5 6 animation.fromValue=orginMultiple; 7 8 animation.toValue=Multiple; 9 10 animation.duration=time; 11 12 animation.autoreverses=YES; 13 14 animation.repeatCount=repeatTimes; 15 16 animation.removedOnCompletion=NO; 17 18 animation.fillMode=kCAFillModeForwards; 19 20 return animation; 21 22 }
//组合动画
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.repeatCount=repeatTimes; 12 13 animation.removedOnCompletion=NO; 14 15 animation.fillMode=kCAFillModeForwards; 16 17 return animation; 18 19 }
//路径动画
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CAKeyframeAnimation *)keyframeAniamtion:(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 }
//点移动
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CABasicAnimation *)movepoint:(CGPoint )point 2 { 3 4 CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation"]; 5 6 animation.toValue=[NSValue valueWithCGPoint:point]; 7 8 animation.removedOnCompletion=NO; 9 10 animation.fillMode=kCAFillModeForwards; 11 12 return animation; 13 14 }
//旋转
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 +(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount 2 3 { 4 5 CATransform3D rotationTransform = CATransform3DMakeRotation(degree, 0, 0,direction); 6 7 CABasicAnimation* animation; 8 9 animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 10 11 12 13 animation.toValue= [NSValue valueWithCATransform3D:rotationTransform]; 14 15 animation.duration= dur; 16 17 animation.autoreverses= NO; 18 19 animation.cumulative= YES; 20 21 animation.removedOnCompletion=NO; 22 23 animation.fillMode=kCAFillModeForwards; 24 25 animation.repeatCount= repeatCount; 26 27 animation.delegate= self; 28 29 30 31 return animation; 32 33 }