一.UIView動畫(首尾)
1.簡單說明
UIKit直接將動畫集成到UIView類中,當內部的一些屬性發生改變時,UIView將爲這些改變提供動畫支持。
執行動畫所須要的工做由UIView類自動完成,但仍要在但願執行動畫時通知視圖,爲此須要將改變屬性的代碼在[UIView beginAnimation:nil context:nil]和[UIView commitAnimations]之間。
緩存
常見方法解析:
(1)設置動畫代理對象,當動畫開始或者結束時會發消息給代理對象
+(void)setAnimationDelegate:(id)delegate
性能
(2)當動畫即將開始時,執行delegate對象的selector,而且把beginAnimation:context:中傳入的參數傳進selector
+(void)setAnimationWillStartSelector:(SEL)selector
動畫
(3)當動畫結束時,執行delegate對象的selector,而且把beginAnimation:context:中傳入的參數傳進selector
+(void)setAnimationDidStopSelector:(SEL)selector
atom
(4)動畫的持續時間,秒爲單位
+(void)setAnimationDuration:(NSTimeInterval)duration
spa
(5)動畫延遲delay秒後再開始
+(void)setAnimationDelay:(NSTimeInterval)delay
代理
(6)動畫的開始時間,默認爲now
+(void)setAnimationStartDate:(NSDate*)startDate
code
(7)動畫的節奏控制
+(void)setAnimationCurve:(UIViewAnimationCurve)curve
對象
(8)動畫的重複次數
+(void)setAnimationRepeatCount:(float)repeatCount
blog
(9)若是設置爲YES,表明動畫每次重複執行的效果會跟上一次相反
+(void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
事件
(10)設置視圖view的過分效果,transition指定過渡類型,cache設置YES表明使用視圖緩存,性能較好。
+(void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView*)view cache:(BOOL)cache
2.代碼示例
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong) UIView *customView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _customView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)]; _customView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_customView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //打印動畫塊的位置 NSLog(@"動畫執行以前的位置:%@",NSStringFromCGPoint(self.customView.center)); //首尾式動畫 [UIView beginAnimations:nil context:nil]; //執行動畫 //設置動畫執行時間 [UIView setAnimationDuration:2.0]; //設置代理 [UIView setAnimationDelegate:self]; //設置動畫執行完畢調用的事件 [UIView setAnimationDidStopSelector:@selector(didStopAnimation)]; _customView.center = CGPointMake(200, 300); [UIView commitAnimations]; } - (void)didStopAnimation{ NSLog(@"動畫執行完畢"); NSLog(@"動畫執行以後的位置:%@",NSStringFromCGPoint(self.customView.center)); } @end
3.UIView封裝的動畫與CALayer動畫的對比
使用UIView和CALayer都能實現動畫效果,可是在真實的開發中,通常仍是主要是用UIView封裝的動畫,而不多使用CALayer的動畫。
CALayer核心動畫與UIView動畫的區別:
UIView封裝的動畫執行完畢以後不會反彈。即若是是經過CALayer核心動畫改變layer的位置狀態,表面上看雖然已經改變了,可是實際上它的位置是沒有改變的。
二.block動畫
1.簡單說明 + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數解析
duration:動畫的持續時間
delay:動畫延遲delay秒後開始
options:動畫的節奏控制
animations:將改變視圖屬性的代碼放在這個block中
completion:動畫結束後,會自動調用這個block
轉場動畫 + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數解析
duration:動畫的持續時間
view:須要進行轉場動畫的視圖
options:轉場動畫的類型
animations:將改變視圖屬性的代碼放在這個block中
completion:動畫結束後,會自動調用這個block
+(void)transitionFromView:(UIView )fromView toView:(UIView )toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
方法調用完畢後,至關於執行了下面兩句代碼
//添加toView到父視圖
[fromView.superview addSubview:toView];
//把fromView從父視圖中移除
[fromView.superview removeFromSuperview];
參數解析
duration:動畫的持續時間
options:轉場動畫的類型
animations:將改變視圖屬性的代碼放在這個block中
completion:動畫結束後,會自動調用這個block
2.代碼示例
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong) UIView *customView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _customView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)]; _customView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_customView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //block代碼塊動畫 [UIView transitionWithView:_customView duration:3.0 options:UIViewAnimationOptionLayoutSubviews animations:^{ //執行的動畫 NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint(self.customView.center)); _customView.center = CGPointMake(200, 300); } completion:^(BOOL finished) { //動畫執行完畢後的首位操做 NSLog(@"動畫執行完畢"); NSLog(@"動畫執行完畢後的位置:%@",NSStringFromCGPoint( self.customView.center)); }]; } @end
提示:self.customView.layer.position和self.customView.center等價,由於position的默認值爲(0.5,0.5)。