IOS UIView動(dòng)畫

UIKit直接將動(dòng)畫集成到UIView類中,當(dāng)內(nèi)部的一些屬性發(fā)生改變時(shí),UIView將為這些改變提供動(dòng)畫支持。

主要分為首尾方式和block方式兩種。

1.UIView動(dòng)畫(首尾方式)

執(zhí)行動(dòng)畫所需要的工作由UIView類自動(dòng)完成,但仍要在希望執(zhí)行動(dòng)畫時(shí)通知視圖,為此需要將改變屬性的代碼放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之間。

一些設(shè)置UIView動(dòng)畫屬性的方法

+ (void)setAnimationDelegate:(id)delegate? ? 設(shè)置動(dòng)畫代理對象,當(dāng)動(dòng)畫開始或者結(jié)束時(shí)會發(fā)消息給代理對象

+ (void)setAnimationWillStartSelector:(SEL)selector? 當(dāng)動(dòng)畫即將開始時(shí),執(zhí)行delegate對象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector

+ (void)setAnimationDidStopSelector:(SEL)selector? 當(dāng)動(dòng)畫結(jié)束時(shí),執(zhí)行delegate對象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector

+ (void)setAnimationDuration:(NSTimeInterval)duration? 動(dòng)畫的持續(xù)時(shí)間,秒為單位

+ (void)setAnimationDelay:(NSTimeInterval)delay? 動(dòng)畫延遲delay秒后再開始

+ (void)setAnimationStartDate:(NSDate *)startDate? 動(dòng)畫的開始時(shí)間,默認(rèn)為now

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve? 動(dòng)畫的節(jié)奏控制

+ (void)setAnimationRepeatCount:(float)repeatCount? 動(dòng)畫的重復(fù)次數(shù)

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses? 如果設(shè)置為YES,代表動(dòng)畫每次重復(fù)執(zhí)行的效果會跟上一次相反

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache? 設(shè)置視圖view的過渡效果, transition指定過渡類型, cache設(shè)置YES代表使用視圖緩存,性能較好

通常實(shí)現(xiàn)代碼

//首尾式動(dòng)畫

[UIView beginAnimations:nil context:nil];

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

//設(shè)置動(dòng)畫執(zhí)行時(shí)間

[UIView setAnimationDuration:2.0];

//設(shè)置代理

[UIView setAnimationDelegate:self];

//設(shè)置動(dòng)畫執(zhí)行完畢調(diào)用的事件

[UIView setAnimationDidStopSelector:@selector(didStopAnimation)];

self.customView.center=CGPointMake(200, 300);

[UIView commitAnimations];

此代碼實(shí)現(xiàn)了UIView動(dòng)畫的delegate,所以可以監(jiān)聽動(dòng)畫的開始和結(jié)束,didStopAnimation這個(gè)方法是自定義的。

2.UIView動(dòng)畫(block方式)

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

參數(shù)解析:

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

delay:動(dòng)畫延遲delay秒后開始

options:動(dòng)畫的節(jié)奏控制

animations:將改變視圖屬性的代碼放在這個(gè)block中

completion:動(dòng)畫結(jié)束后,會自動(dòng)調(diào)用這個(gè)block

轉(zhuǎn)場動(dòng)畫

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

參數(shù)解析:

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

view:需要進(jìn)行轉(zhuǎn)場動(dòng)畫的視圖

options:轉(zhuǎn)場動(dòng)畫的類型

animations:將改變視圖屬性的代碼放在這個(gè)block中

completion:動(dòng)畫結(jié)束后,會自動(dòng)調(diào)用這個(gè)block

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

方法調(diào)用完畢后,相當(dāng)于執(zhí)行了下面兩句代碼:

// 添加toView到父視圖

[fromView.superview addSubview:toView];

// 把fromView從父視圖中移除

[fromView removeFromSuperview];

參數(shù)解析:

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

options:轉(zhuǎn)場動(dòng)畫的類型

animations:將改變視圖屬性的代碼放在這個(gè)block中

completion:動(dòng)畫結(jié)束后,會自動(dòng)調(diào)用這個(gè)block

補(bǔ)充:

block方式下的options

UIViewAnimationOptionLayoutSubviews //提交動(dòng)畫的時(shí)候布局子控件,表示子控件將和父控件一同動(dòng)畫。

UIViewAnimationOptionAllowUserInteraction //動(dòng)畫時(shí)允許用戶交流,比如觸摸

UIViewAnimationOptionBeginFromCurrentState //從當(dāng)前狀態(tài)開始動(dòng)畫

UIViewAnimationOptionRepeat //動(dòng)畫無限重復(fù)

UIViewAnimationOptionAutoreverse //執(zhí)行動(dòng)畫回路,前提是設(shè)置動(dòng)畫無限重復(fù)

UIViewAnimationOptionOverrideInheritedDuration //忽略外層動(dòng)畫嵌套的執(zhí)行時(shí)間

UIViewAnimationOptionOverrideInheritedCurve //忽略外層動(dòng)畫嵌套的時(shí)間變化曲線

UIViewAnimationOptionAllowAnimatedContent //通過改變屬性和重繪實(shí)現(xiàn)動(dòng)畫效果,如果key沒有提交動(dòng)畫將使用快照

UIViewAnimationOptionShowHideTransitionViews //用顯隱的方式替代添加移除圖層的動(dòng)畫效果

UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套繼承的?選項(xiàng)

時(shí)間函數(shù)曲線相關(guān)

UIViewAnimationOptionCurveEaseInOut //時(shí)間曲線函數(shù),由慢到快

UIViewAnimationOptionCurveEaseIn //時(shí)間曲線函數(shù),由慢到特別快

UIViewAnimationOptionCurveEaseOut //時(shí)間曲線函數(shù),由快到慢

UIViewAnimationOptionCurveLinear //時(shí)間曲線函數(shù),勻速

//轉(zhuǎn)場動(dòng)畫相關(guān)的

UIViewAnimationOptionTransitionNone //無轉(zhuǎn)場動(dòng)畫

UIViewAnimationOptionTransitionFlipFromLeft //轉(zhuǎn)場從左翻轉(zhuǎn)

UIViewAnimationOptionTransitionFlipFromRight //轉(zhuǎn)場從右翻轉(zhuǎn)

UIViewAnimationOptionTransitionCurlUp //上卷轉(zhuǎn)場

UIViewAnimationOptionTransitionCurlDown //下卷轉(zhuǎn)場

UIViewAnimationOptionTransitionCrossDissolve //轉(zhuǎn)場交叉消失

UIViewAnimationOptionTransitionFlipFromTop //轉(zhuǎn)場從上翻轉(zhuǎn)

UIViewAnimationOptionTransitionFlipFromBottom //轉(zhuǎn)場從下翻轉(zhuǎn)

轉(zhuǎn)自文頂頂?shù)牟┛?/a>

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

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