iOS之UIview动画
一、UIView動(dòng)畫(huà)(首尾)
1.簡(jiǎn)單說(shuō)明
UIKit直接將動(dòng)畫(huà)集成到UIView類(lèi)中,當(dāng)內(nèi)部的一些屬性發(fā)生改變時(shí),UIView將為這些改變提供動(dòng)畫(huà)支持
執(zhí)行動(dòng)畫(huà)所需要的工作由UIView類(lèi)自動(dòng)完成,但仍要在希望執(zhí)行動(dòng)畫(huà)時(shí)通知視圖,為此需要將改變屬性的代碼放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之間
常見(jiàn)方法解析:
+ (void)setAnimationDelegate:(id)delegate ? ??設(shè)置動(dòng)畫(huà)代理對(duì)象,當(dāng)動(dòng)畫(huà)開(kāi)始或者結(jié)束時(shí)會(huì)發(fā)消息給代理對(duì)象
+ (void)setAnimationWillStartSelector:(SEL)selector ??當(dāng)動(dòng)畫(huà)即將開(kāi)始時(shí),執(zhí)行delegate對(duì)象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector
+ (void)setAnimationDidStopSelector:(SEL)selector ?當(dāng)動(dòng)畫(huà)結(jié)束時(shí),執(zhí)行delegate對(duì)象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector
+ (void)setAnimationDuration:(NSTimeInterval)duration ??動(dòng)畫(huà)的持續(xù)時(shí)間,秒為單位
+ (void)setAnimationDelay:(NSTimeInterval)delay ?動(dòng)畫(huà)延遲delay秒后再開(kāi)始
+ (void)setAnimationStartDate:(NSDate *)startDate ??動(dòng)畫(huà)的開(kāi)始時(shí)間,默認(rèn)為now
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve ?動(dòng)畫(huà)的節(jié)奏控制
+ (void)setAnimationRepeatCount:(float)repeatCount ?動(dòng)畫(huà)的重復(fù)次數(shù)
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses ?如果設(shè)置為YES,代表動(dòng)畫(huà)每次重復(fù)執(zhí)行的效果會(huì)跟上一次相反
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache ?設(shè)置視圖view的過(guò)渡效果, transition指定過(guò)渡類(lèi)型, cache設(shè)置YES代表使用視圖緩存,性能較好
?
2.代碼示例
1 // 2 // YYViewController.m 3 // 01-uiview封裝動(dòng)畫(huà) 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property (weak, nonatomic) IBOutlet UIView *customView; 13 14 15 @end 16 17 @implementation YYViewController 18 19 - (void)viewDidLoad 20 { 21 [super viewDidLoad]; 22 23 } 24 25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 26 { 27 //打印動(dòng)畫(huà)塊的位置 28 NSLog(@"動(dòng)畫(huà)執(zhí)行之前的位置:%@",NSStringFromCGPoint(self.customView.center)); 29 30 //首尾式動(dòng)畫(huà) 31 [UIView beginAnimations:nil context:nil]; 32 //執(zhí)行動(dòng)畫(huà) 33 //設(shè)置動(dòng)畫(huà)執(zhí)行時(shí)間 34 [UIView setAnimationDuration:2.0]; 35 //設(shè)置代理 36 [UIView setAnimationDelegate:self]; 37 //設(shè)置動(dòng)畫(huà)執(zhí)行完畢調(diào)用的事件 38 [UIView setAnimationDidStopSelector:@selector(didStopAnimation)]; 39 self.customView.center=CGPointMake(200, 300); 40 [UIView commitAnimations]; 41 42 } 43 44 -(void)didStopAnimation 45 { 46 NSLog(@"動(dòng)畫(huà)執(zhí)行完畢"); 47 //打印動(dòng)畫(huà)塊的位置 48 NSLog(@"動(dòng)畫(huà)執(zhí)行之后的位置:%@",NSStringFromCGPoint(self.customView.center)); 49 } 50 @end執(zhí)行結(jié)果:
? ??
打印動(dòng)畫(huà)塊的位置:
3.UIView封裝的動(dòng)畫(huà)與CALayer動(dòng)畫(huà)的對(duì)比
使用UIView和CALayer都能實(shí)現(xiàn)動(dòng)畫(huà)效果,但是在真實(shí)的開(kāi)發(fā)中,一般還是主要使用UIView封裝的動(dòng)畫(huà),而很少使用CALayer的動(dòng)畫(huà)。
CALayer核心動(dòng)畫(huà)與UIView動(dòng)畫(huà)的區(qū)別:
UIView封裝的動(dòng)畫(huà)執(zhí)行完畢之后不會(huì)反彈。即如果是通過(guò)CALayer核心動(dòng)畫(huà)改變layer的位置狀態(tài),表面上看雖然已經(jīng)改變了,但是實(shí)際上它的位置是沒(méi)有改變的。
代碼示例:
1 // 2 // YYViewController.m 3 // 01-uiview封裝動(dòng)畫(huà) 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property (weak, nonatomic) IBOutlet UIView *customView; 13 14 15 @end 16 17 @implementation YYViewController 18 19 20 21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 22 { 23 //1.創(chuàng)建核心動(dòng)畫(huà) 24 CABasicAnimation *anima=[CABasicAnimation animation]; 25 //平移 26 anima.keyPath=@"position"; 27 //設(shè)置執(zhí)行的動(dòng)畫(huà) 28 anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)]; 29 30 //設(shè)置執(zhí)行動(dòng)畫(huà)的時(shí)間 31 anima.duration=2.0; 32 //設(shè)置動(dòng)畫(huà)執(zhí)行完畢之后不刪除動(dòng)畫(huà) 33 anima.removedOnCompletion=NO; 34 //設(shè)置保存動(dòng)畫(huà)的最新?tīng)顟B(tài) 35 anima.fillMode=kCAFillModeForwards; 36 // anima.fillMode=kCAFillModeBackwards; 37 38 //設(shè)置動(dòng)畫(huà)的代理 39 anima.delegate=self; 40 41 //2.添加核心動(dòng)畫(huà) 42 [self.customView.layer addAnimation:anima forKey:nil]; 43 } 44 45 -(void)animationDidStart:(CAAnimation *)anim 46 { 47 //打印動(dòng)畫(huà)塊的位置 48 // NSLog(@"動(dòng)畫(huà)開(kāi)始執(zhí)行前的位置:%@",NSStringFromCGPoint(self.customView.center)); 49 NSLog(@"動(dòng)畫(huà)開(kāi)始執(zhí)行前的位置:%@",NSStringFromCGPoint( self.customView.layer.position)); 50 } 51 -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 52 { 53 //打印動(dòng)畫(huà)塊的位置 54 NSLog(@"動(dòng)畫(huà)執(zhí)行完畢后的位置:%@",NSStringFromCGPoint( self.customView.layer.position)); 55 } 56 57 @end打印結(jié)果:
?
二、block動(dòng)畫(huà)
1.簡(jiǎn)單說(shuō)明
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數(shù)解析:
duration:動(dòng)畫(huà)的持續(xù)時(shí)間
delay:動(dòng)畫(huà)延遲delay秒后開(kāi)始
options:動(dòng)畫(huà)的節(jié)奏控制
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫(huà)結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數(shù)解析:
duration:動(dòng)畫(huà)的持續(xù)時(shí)間
view:需要進(jìn)行轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的視圖
options:轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的類(lèi)型
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫(huà)結(jié)束后,會(huì)自動(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.superview removeFromSuperview];
參數(shù)解析:
duration:動(dòng)畫(huà)的持續(xù)時(shí)間
options:轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的類(lèi)型
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫(huà)結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
?
2.代碼示例
1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 @property (weak, nonatomic) IBOutlet UIView *customView; 5 6 @end 7 8 @implementation YYViewController 9 10 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 11 { 12 //block代碼塊動(dòng)畫(huà) 13 [UIView transitionWithView:self.customView duration:3.0 options:0 animations:^{ 14 //執(zhí)行的動(dòng)畫(huà) 15 NSLog(@"動(dòng)畫(huà)開(kāi)始執(zhí)行前的位置:%@",NSStringFromCGPoint(self.customView.center)); 16 self.customView.center=CGPointMake(200, 300); 17 } completion:^(BOOL finished) { 18 //動(dòng)畫(huà)執(zhí)行完畢后的首位操作 19 NSLog(@"動(dòng)畫(huà)執(zhí)行完畢"); 20 NSLog(@"動(dòng)畫(huà)執(zhí)行完畢后的位置:%@",NSStringFromCGPoint( self.customView.center)); 21 }]; 22 } 23 @end打印結(jié)果:
提示:self.customView.layer.position和self.customView.center等價(jià),因?yàn)閜osition的默認(rèn)值為(0.5,0.5)。
?
三、補(bǔ)充
1.UIImageView的幀動(dòng)畫(huà)
UIImageView可以讓一系列的圖片在特定的時(shí)間內(nèi)按順序顯示
相關(guān)屬性解析:
animationImages:要顯示的圖片(一個(gè)裝著UIImage的NSArray)
animationDuration:完整地顯示一次animationImages中的所有圖片所需的時(shí)間
animationRepeatCount:動(dòng)畫(huà)的執(zhí)行次數(shù)(默認(rèn)為0,代表無(wú)限循環(huán))
相關(guān)方法解析:
- (void)startAnimating; 開(kāi)始動(dòng)畫(huà)
- (void)stopAnimating;? 停止動(dòng)畫(huà)
- (BOOL)isAnimating;? 是否正在運(yùn)行動(dòng)畫(huà)
?
2.UIActivityIndicatorView
是一個(gè)旋轉(zhuǎn)進(jìn)度輪,可以用來(lái)告知用戶有一個(gè)操作正在進(jìn)行中,一般用initWithActivityIndicatorStyle初始化
方法解析:
- (void)startAnimating; 開(kāi)始動(dòng)畫(huà)
- (void)stopAnimating;? 停止動(dòng)畫(huà)
- (BOOL)isAnimating;? 是否正在運(yùn)行動(dòng)畫(huà)
UIActivityIndicatorViewStyle有3個(gè)值可供選擇:
UIActivityIndicatorViewStyleWhiteLarge???//大型白色指示器????
UIActivityIndicatorViewStyleWhite??????//標(biāo)準(zhǔn)尺寸白色指示器????
UIActivityIndicatorViewStyleGray????//灰色指示器,用于白色背景
轉(zhuǎn)載于:https://www.cnblogs.com/zhun/p/5535978.html
總結(jié)
以上是生活随笔為你收集整理的iOS之UIview动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: DIV+CSS笔记(一)
- 下一篇: 《构建之法》8、9、10章