iOS簡單動(dòng)畫實(shí)現(xiàn) (一)

iOS動(dòng)畫主要是指Core Animation框架,Core Animation可以作用與動(dòng)畫視圖或者其他可視元素,為你完成了動(dòng)畫所需的大部分繪幀工作。

先查看 Core Animation類

常用屬性

duration : 動(dòng)畫的持續(xù)時(shí)間

beginTime : 動(dòng)畫的開始時(shí)間

repeatCount : 動(dòng)畫的重復(fù)次數(shù)

autoreverses : 執(zhí)行的動(dòng)畫按照原動(dòng)畫返回執(zhí)行

type:過渡動(dòng)畫的動(dòng)畫類型,系統(tǒng)提供了四種過渡動(dòng)畫。

kCATransitionFade 漸變效果

kCATransitionMoveIn 進(jìn)入覆蓋效果

kCATransitionPush 推出效果

kCATransitionReveal 揭露離開效果

subtype : 過渡動(dòng)畫的動(dòng)畫方向

kCATransitionFromRight 從右側(cè)進(jìn)入

kCATransitionFromLeft 從左側(cè)進(jìn)入

kCATransitionFromTop 從頂部進(jìn)入

kCATransitionFromBottom 從底部進(jìn)入


在oc中調(diào)用動(dòng)畫一般有三種方式

? ? 1 ? UIView直接調(diào)用

基本寫法,代碼必須放在Begin和Commit之間:

[UIView beginAnimations:nil context:nil];

//開始動(dòng)畫//Code..

.[UIView commitAnimations];//提交動(dòng)畫

例如如下執(zhí)行多個(gè)效果

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:3.0];

[_imageView setAlpha:0.0];

[UIView commitAnimations];


[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:3.0];

CGPoint point=_imageView.center;

point.y+=150;

[_imageView setCenter:point];

[UIView commitAnimations];

另外簡單介紹下UIView 的部分屬性和方法

//開始動(dòng)畫 ? ?

+ (void)beginAnimations:(NSString *)animationID context:(void*)context;

//提交動(dòng)畫+ (void)commitAnimations;

//設(shè)置動(dòng)畫曲線,默認(rèn)是勻速進(jìn)行:

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; ?

//設(shè)置動(dòng)畫時(shí)長:

+ (void)setAnimationDuration:(NSTimeInterval)duration; ?

//默認(rèn)為YES。為NO時(shí)跳過動(dòng)畫效果,直接跳到執(zhí)行后的狀態(tài)

+(void)setAnimationsEnabled:(BOOL)enabled; ?

//設(shè)置動(dòng)畫延遲執(zhí)行(delay:秒為單位):

+ (void)setAnimationDelay:(NSTimeInterval)delay;

//動(dòng)畫的重復(fù)播放次數(shù)

+ (void)setAnimationRepeatCount:(float)repeatCount

//如果為YES,逆向(相反)動(dòng)畫效果,結(jié)束后返回動(dòng)畫逆向前的狀態(tài); 默認(rèn)為NO:

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;

//設(shè)置動(dòng)畫代理:

+ (void)setAnimationDelegate:(id)delegate;

//動(dòng)畫將要開始時(shí)執(zhí)行方法××(必須要先設(shè)置動(dòng)畫代理):

+ (void)setAnimationWillStartSelector:(SEL)selector;

//動(dòng)畫已結(jié)束時(shí)執(zhí)行方法××(必須要先設(shè)置動(dòng)畫代理):

+ (void)setAnimationDidStopSelector:(SEL)selector;/*

*? 設(shè)置動(dòng)畫過渡效果

*

*? @param transition 動(dòng)畫的過渡效果

*? @param view 過渡效果作用視圖

*? @param cache 如果為YES,開始和結(jié)束視圖分別渲染一次并在動(dòng)畫中創(chuàng)建幀;否則,視圖將會(huì)渲染每一幀。例如,你不需要在視圖轉(zhuǎn)變中不停的更新,你只需要等到轉(zhuǎn)換完成再去更新視圖。*/

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;

//刪除所有動(dòng)畫層

- (void)removeAllAnimations;

? ?2 使用 UIView 代碼塊

// ?方法一 ?簡單動(dòng)畫執(zhí)行

[UIView animateWithDuration:4.0 ? ?//動(dòng)畫時(shí)長

animations:^{

//code ?動(dòng)畫執(zhí)行代碼

}];

//方法二 ?動(dòng)畫執(zhí)行完后調(diào)用

[UIView animateWithDuration:4.0

//動(dòng)畫時(shí)長

animations:^{

//code... 動(dòng)畫執(zhí)行代碼

}

completion:^(BOOL finished) {

//動(dòng)畫完成后執(zhí)行//code...

}];

//方法三 ? 帶有延時(shí)效果的

[UIView ?animateWithDuration:4.0 ?//動(dòng)畫時(shí)長 ?

delay:2.0 ?//動(dòng)畫延遲 ?

options:UIViewAnimationOptionCurveEaseIn //動(dòng)畫過渡效果 ?

animations:^{

//code... 動(dòng)畫執(zhí)行代碼

completion:^(BOOL finished) {

//動(dòng)畫完成后執(zhí)行//code...

}];

方法四,Spring Animationring Animation

[UIView animateWithDuration:4.0 ?//動(dòng)畫時(shí)長?

delay:0.0 ?//動(dòng)畫延遲

usingSpringWithDamping:1.0 ?//類似彈簧振動(dòng)效果 0~1?

initialSpringVelocity:5.0//初始速度

options:UIViewAnimationOptionCurveEaseInOut ?//動(dòng)畫過渡效果

animations:^{

//動(dòng)畫執(zhí)行代碼

} completion:^(BOOL finished) {

//動(dòng)畫完成后執(zhí)行//code...[_imageView setAlpha:1];

}];

iOS中動(dòng)畫是做出炫酷效果的基礎(chǔ) 本文僅僅簡單介紹 后期會(huì)抽空繼續(xù)深入挖掘研究

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容