CABasicAnimation是放在layer上面的入录,首先我們先了解一下層的概念.
CALayer的介紹。
CALayer是個(gè)與UIView很類似的概念晒夹,同樣有l(wèi)ayer,sublayer裆馒、backgroundColor姊氓、frame等屬性,所以說(shuō)只在渲染的層次上考慮喷好,他們兩者是相同的翔横,唯獨(dú)一點(diǎn):UIView可以添加通過(guò)方法選擇器等添加響應(yīng)事件。
因?yàn)槊總€(gè)UIView都有自己的layer屬性梗搅,而這個(gè)屬性的用途有兩個(gè):1棕孙、設(shè)置View的圓角、陰影些膨、邊框等蟀俊,具體屬性如下:
1. shadowPath : 設(shè)置 CALayer 背景(shodow)的位置
2. shadowOffset : shadow 在 X 和 Y 軸 上延伸的方向,即 shadow 的大小
3. shadowOpacity : shadow 的透明效果
4. shadowRadius : shadow 的漸變距離订雾,從外圍開始肢预,往里漸變 shadowRadius 距離
5. masksToBounds : 很重要的屬性,可以用此屬性來(lái)防止子元素大小溢出父元素洼哎,如若防止溢出烫映,請(qǐng)?jiān)O(shè)為 true
6. borderWidth 和 boarderColor : 邊框顏色和寬度,很常用
7. bounds : 對(duì)于我來(lái)說(shuō)比較難的一個(gè)屬性噩峦,測(cè)了半天也沒(méi)完全了解锭沟,只知道可以用來(lái)控制 UIView 的大小,但是不能控制 位置
8. opacity : UIView 的透明效果
9. cornerRadius : UIView 的圓角
2识补、設(shè)置View的動(dòng)畫族淮。因此對(duì)一個(gè)UIView進(jìn)行animation動(dòng)畫,實(shí)際上就是對(duì)View的layer進(jìn)行操控的凭涂。
而小編下面說(shuō)的就是如何對(duì)其動(dòng)畫進(jìn)行操控——CAAnimation祝辣。
CAAnimation的介紹
CAAnimation可分為四種:
1、CABasicAnimation:通過(guò)設(shè)定起始點(diǎn)切油,終點(diǎn)蝙斜,時(shí)間,動(dòng)畫會(huì)沿著你這設(shè)定點(diǎn)進(jìn)行移動(dòng)澎胡≡熊可以看做特殊的CAKeyFrameAnimation。
2攻谁、CAKeyframeAnimation:Keyframe顧名思義就是關(guān)鍵點(diǎn)的frame稚伍,你可以通過(guò)設(shè)定CALayer的始點(diǎn)、中間關(guān)鍵點(diǎn)巢株、終點(diǎn)的frame槐瑞,時(shí)間熙涤,只不過(guò)你可以定義它的移動(dòng)軌跡阁苞。
3困檩、CAAnimationGroup:Group也就是組合的意思,就是把對(duì)這個(gè)Layer的所有動(dòng)畫都組合起來(lái)那槽。PS:一個(gè)layer設(shè)定了很多動(dòng)畫悼沿,他們都會(huì)同時(shí)執(zhí)行,如何按順序執(zhí)行我到時(shí)候再講骚灸。
4糟趾、CATransition:這個(gè)就是蘋果幫開發(fā)者封裝好的一些動(dòng)畫
這里我們拿一個(gè)Demo來(lái)詳細(xì)說(shuō)明,在列舉例子之前,我們先看一組key值:就是在初始化CAAnimation的時(shí)候用到animationWithKeyPath:(NSString *)key中的key都有哪些.在這里我列舉一些我們?nèi)粘i_發(fā)常用的key.
transform.rotation.x :延X軸坐標(biāo)旋轉(zhuǎn) 類型:CGFloat或float * M_PI。
transform.rotation.y:延Y軸坐標(biāo)旋轉(zhuǎn) ?類型:CGFloat或float * M_PI甚牲。
transform.rotation.z:延Z軸坐標(biāo)旋轉(zhuǎn) ?類型:CGFloat或float * M_PI义郑。。
transform.rotation:這個(gè)與rotation.z是相同的 ?類型:CGFloat或float * M_PI丈钙。
transform.scale:整個(gè)layer的比例 ? 類型:CGFloat非驮。
transform.scale.x:x軸坐標(biāo)比例變化 類型:CGFloat。
transform.scale.y:y軸坐標(biāo)比例變化 ?類型:CGFloat雏赦。
transform.scale.z:z軸坐標(biāo)比例變化 ?類型:CGFloat劫笙。
opacity : 透明度,閃爍等動(dòng)畫用 ? ? ? ?類型:CGFloat 1.0或0.0。
backgroundColor ? ?背景顏色 ? ? ?類型:CGColor星岗。
cornerRadius ? ?圓角 ? ? ? ? ? ? ? ? ? ?類型:CGFloat填大。
transform.translation.y:縱向移動(dòng) ?類型:CGMutablePathRef ?可自定義
transform.translation.x:橫向移動(dòng) ?類型:CGMutablePathRef ?可自定義
在設(shè)置Animation的屬性的時(shí)候會(huì)有一個(gè)屬性為fillMode,首先聲明若使用此屬性,必須先寫這寫:animation.removedOnCompletion=NO;下面介紹fillMode的各個(gè)值:
kCAFillModeRemoved :這個(gè)是默認(rèn)值,也就是說(shuō)當(dāng)動(dòng)畫開始前和動(dòng)畫結(jié)束后,動(dòng)畫對(duì)layer都沒(méi)有影響,動(dòng)畫結(jié)束后,layer會(huì)恢復(fù)到之前的狀態(tài)
kCAFillModeForwards :當(dāng)動(dòng)畫結(jié)束后,layer會(huì)一直保持著動(dòng)畫最后的狀態(tài)
kCAFillModeBackwards :這個(gè)和kCAFillModeForwards是相對(duì)的,就是在動(dòng)畫開始前,你只要將動(dòng)畫加入了一個(gè)layer,layer便立即進(jìn)入動(dòng)畫的初始狀態(tài)并等待動(dòng)畫開始.你可以這樣設(shè)定測(cè)試代碼,將一個(gè)動(dòng)畫加入一個(gè)layer的時(shí)候延遲5秒執(zhí)行.然后就會(huì)發(fā)現(xiàn)在動(dòng)畫沒(méi)有開始的時(shí)候,只要?jiǎng)赢嫳患尤肓薼ayer,layer便處于動(dòng)畫初始狀態(tài)
kCAFillModeBoth :理解了上面兩個(gè),這個(gè)就很好理解了,這個(gè)其實(shí)就是上面兩個(gè)的合成.動(dòng)畫加入后開始之前,layer便處于動(dòng)畫初始狀態(tài),動(dòng)畫結(jié)束后layer保持動(dòng)畫最后的狀態(tài).
除此之外timingFunction這個(gè)屬性也是很重要的,有關(guān)他的屬性如下
kCAMediaTimingFunctionLinear(線性):勻速,給你一個(gè)相對(duì)靜態(tài)的感覺
kCAMediaTimingFunctionEaseIn(漸進(jìn)):動(dòng)畫緩慢進(jìn)入俏橘,然后加速離開
kCAMediaTimingFunctionEaseOut(漸出):動(dòng)畫全速進(jìn)入允华,然后減速的到達(dá)目的地
kCAMediaTimingFunctionEaseInEaseOut(漸進(jìn)漸出):動(dòng)畫緩慢的進(jìn)入,中間加速寥掐,然后減速的到達(dá)目的地例获。 ? ? ? ?這個(gè)是默認(rèn)的動(dòng)畫行為。
各種動(dòng)畫的demo
1 ?永久閃爍的動(dòng)畫.
+(CABasicAnimation *)opacityForever_Animation:(float)time
{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];//key 為opacity
animation.fromValue=[NSNumber numberWithFloat:1.0];
animation.toValue=[NSNumber numberWithFloat:0.0];
animation.autoreverses=YES;
animation.duration=time;
animation.repeatCount=FLT_MAX;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
return animation;
}
2 ? ?有閃爍次數(shù)的動(dòng)畫
+(CABasicAnimation *)opacityTimes_Animation:(float)repeatTimes durTimes:(float)time
{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue=[NSNumber numberWithFloat:1.0];
animation.toValue=[NSNumber numberWithFloat:0.4];
animation.repeatCount=repeatTimes;
animation.duration=time;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
animation.autoreverses=YES;
return ?animation;
}
3 橫向和縱向移動(dòng)
+(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x //橫向移動(dòng)
{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
animation.toValue=x;
animation.duration=time;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
return animation;
}
+(CABasicAnimation *)moveY:(float)time Y:(NSNumber *)y //縱向移動(dòng)
{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
animation.toValue=y;
animation.duration=time;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
return animation;
}
4 縮放
+(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repeatTimes //縮放
{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.fromValue=orginMultiple;
animation.toValue=Multiple;
animation.duration=time;
animation.autoreverses=YES;
animation.repeatCount=repeatTimes;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
return animation;
}
5 group組合動(dòng)畫
+(CAAnimationGroup *)groupAnimation:(NSArray *)animationAry durTimes:(float)time Rep:(float)repeatTimes //組合動(dòng)畫
{
CAAnimationGroup *animation=[CAAnimationGroup animation];
animation.animations=animationAry;
animation.duration=time;
animation.repeatCount=repeatTimes;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
return animation;
}
6 旋轉(zhuǎn)
+(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount //旋轉(zhuǎn)
{
CATransform3D rotationTransform ?= CATransform3DMakeRotation(degree, 0, 0,direction);
CABasicAnimation* animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.toValue= [NSValue valueWithCATransform3D:rotationTransform];
animation.duration= dur;
animation.autoreverses= NO;
animation.cumulative= YES;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
animation.repeatCount= repeatCount;
animation.delegate= self;
return animation;
}