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中常用到的動畫效果,用以記錄方便以後使用
//永久閃爍的動畫
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 }
//有閃爍次數的動畫
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 }
//橫向移動
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 }
//縱向移動
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 }
//縮放
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 }
//組合動畫
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 }
//路徑動畫
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 }
//點移動
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 }
//旋轉
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 }