您好,登錄后才能下訂單哦!
1.簡單介紹
UIKit直接將動(dòng)畫集成到UIView類中,當(dāng)內(nèi)部的一些屬性發(fā)生改變時(shí),UIView將為這些改變提供動(dòng)畫支持。動(dòng)畫的執(zhí)行過程全部由UIView類自動(dòng)完成,我們只需要通過調(diào)用[UIView beginAnimations: nil context:nil]和[UIView commitAnimations]這2個(gè)方法來通知視圖即可。
常用的方法說明:
+ (void)setAnimationDelegate:(id)delegate -------- 設(shè)置動(dòng)畫代理對(duì)象,當(dāng)動(dòng)畫開始或者結(jié)束時(shí)會(huì)發(fā)消息給代理對(duì)象
+ (void)setAnimationWillStartSelector:(SEL)selector -------- 當(dāng)動(dòng)畫即將開始時(shí),執(zhí)行delegate對(duì)象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector
+ (void)setAnimationDidStopSelector:(SEL)selector --------- 當(dāng)動(dòng)畫結(jié)束時(shí),執(zhí)行delegate對(duì)象的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í)行的效果會(huì)跟上一次相反
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache -------- 設(shè)置視圖view的過渡效果, transition指定過渡類型, cache設(shè)置YES代表使用視圖緩存,性能較好
代碼示例:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//首尾式動(dòng)畫
[UIView beginAnimations:nil context:nil];
//設(shè)置代理
[UIView setAnimationDelegate:self];
//設(shè)置執(zhí)行需要的時(shí)間
[UIView setAnimationDuration:1.0f];
//設(shè)置延遲多久后執(zhí)行
[UIView setAnimationDelay:1.0f];
//設(shè)置動(dòng)畫重復(fù)次數(shù)
[UIView setAnimationRepeatCount:MAXFLOAT];
//設(shè)置動(dòng)畫執(zhí)行的節(jié)奏
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//設(shè)置重復(fù)執(zhí)行時(shí)是否跟之前相反(YES代表相反)
[UIView setAnimationRepeatAutoreverses:YES];
//設(shè)置動(dòng)畫將要執(zhí)行時(shí)的回調(diào)
[UIView setAnimationWillStartSelector:@selector(willStartAnimation)];
//設(shè)置動(dòng)畫完成后的回調(diào)
[UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
_customView.center = CGPointMake(Width/2+ViewWidth, Height/2);
[UIView commitAnimations];
}
-(void)willStartAnimation
{
NSLog(@"動(dòng)畫將要執(zhí)行時(shí)動(dòng)畫塊的位置:%@",NSStringFromCGPoint(_customView.center));
}
-(void)didStopAnimation
{
NSLog(@"動(dòng)畫已經(jīng)結(jié)束后動(dòng)畫塊的位置:%@",NSStringFromCGPoint(_customView.center));
}
提示:
UIView和CALayer都可以實(shí)現(xiàn)視圖動(dòng)畫,但一般在實(shí)際項(xiàng)目中還是使用UIView來實(shí)現(xiàn)動(dòng)畫,因?yàn)樗趫?zhí)行完動(dòng)畫后不回反彈。CALayer在表面上改變了layer的位置狀態(tài),實(shí)際上是沒有做更改。不信可以用之前博文里的代碼測試一下就知道了~
使用block回調(diào)的動(dòng)畫:
1.常用動(dòng)畫
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數(shù)解析:
duration: 動(dòng)畫執(zhí)行的時(shí)間
delay: 動(dòng)畫延遲多久后開始
options: 動(dòng)畫的節(jié)奏類型
animations: 將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
2.轉(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é)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
轉(zhuǎn)場動(dòng)畫(二)
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
參數(shù)解析:
duration:動(dòng)畫的持續(xù)時(shí)間
options:轉(zhuǎn)場動(dòng)畫的類型
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
調(diào)用這個(gè)方法相當(dāng)于執(zhí)行了下面兩句代碼:
// 添加toView到父視圖
[fromView.superview addSubview:toView];
// 把fromView從父視圖中移除
[fromView removeFromSuperview];
代碼示例:
[UIView transitionWithView:self.customView duration:3.0 options:0 animations:^{ //執(zhí)行的動(dòng)畫
NSLog(@"動(dòng)畫開始執(zhí)行前的位置:%@",NSStringFromCGPoint(self.customView.center));
self.customView.center=CGPointMake(200, 300);
} completion:^(BOOL finished) {
//動(dòng)畫執(zhí)行完畢后的首位操作
NSLog(@"動(dòng)畫執(zhí)行完畢");
NSLog(@"動(dòng)畫執(zhí)行完畢后的位置:%@",NSStringFromCGPoint( self.customView.center));
}];
注意:self.customView.layer.position和self.customView.center等價(jià),因?yàn)閜osition的默認(rèn)值為(0.5,0.5)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。