iOS开发UI篇—核心动画(基础动画)
一、簡(jiǎn)單介紹
CAPropertyAnimation的子類
屬性解析:
fromValue:keyPath相應(yīng)屬性的初始值
toValue:keyPath相應(yīng)屬性的結(jié)束值
隨著動(dòng)畫的進(jìn)行,在長(zhǎng)度為duration的持續(xù)時(shí)間內(nèi),keyPath相應(yīng)屬性的值從fromValue漸漸地變?yōu)閠oValue
如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在動(dòng)畫執(zhí)行完畢后,圖層會(huì)保持顯示動(dòng)畫執(zhí)行后的狀態(tài)。但在實(shí)質(zhì)上,圖層的屬性值還是動(dòng)畫執(zhí)行前的初始值,并沒有真正被改變。
比如,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10),toValue為(100,100),雖然動(dòng)畫執(zhí)行完畢后圖層保持在(100,100)這個(gè)位置,實(shí)質(zhì)上圖層的position還是為(0,0)
二、平移動(dòng)畫
代碼示例:
1 //2 // YYViewController.m3 // 07-核心動(dòng)畫(基礎(chǔ)動(dòng)畫)4 //5 // Created by apple on 14-6-21.6 // Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,strong)CALayer *myLayer; 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 //創(chuàng)建layer 22 CALayer *myLayer=[CALayer layer]; 23 //設(shè)置layer的屬性 24 myLayer.bounds=CGRectMake(0, 0, 50, 80); 25 myLayer.backgroundColor=[UIColor yellowColor].CGColor; 26 myLayer.position=CGPointMake(50, 50); 27 myLayer.anchorPoint=CGPointMake(0, 0); 28 myLayer.cornerRadius=20; 29 //添加layer 30 [self.view.layer addSublayer:myLayer]; 31 self.myLayer=myLayer; 32 } 33 34 //設(shè)置動(dòng)畫(基礎(chǔ)動(dòng)畫) 35 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 36 { 37 //1.創(chuàng)建核心動(dòng)畫 38 // CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>] 39 CABasicAnimation *anima=[CABasicAnimation animation]; 40 41 //1.1告訴系統(tǒng)要執(zhí)行什么樣的動(dòng)畫 42 anima.keyPath=@"position"; 43 //設(shè)置通過動(dòng)畫,將layer從哪兒移動(dòng)到哪兒 44 anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)]; 45 anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)]; 46 47 //1.2設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫 48 anima.removedOnCompletion=NO; 49 //1.3設(shè)置保存動(dòng)畫的最新狀態(tài) 50 anima.fillMode=kCAFillModeForwards; 51 52 //2.添加核心動(dòng)畫到layer 53 [self.myLayer addAnimation:anima forKey:nil]; 54 55 }@end
代碼說明:
?第42行設(shè)置的keyPath是@"position",說明要修改的是CALayer的position屬性,也就是會(huì)執(zhí)行平移動(dòng)畫
?第44,45行,這里的屬性接收的時(shí)id類型的參數(shù),因此并不能直接使用CGPoint這種結(jié)構(gòu)體類型,而是要先包裝成NSValue對(duì)象后再使用。
?默認(rèn)情況下,動(dòng)畫執(zhí)行完畢后,動(dòng)畫會(huì)自動(dòng)從CALayer上移除,CALayer又會(huì)回到原來的狀態(tài)。為了保持動(dòng)畫執(zhí)行后的狀態(tài),可以加入第48,50行代碼
byValue和toValue的區(qū)別,前者是在當(dāng)前的位置上增加多少,后者是到指定的位置。
執(zhí)行效果:
??
設(shè)置代理:設(shè)置動(dòng)畫的代理,可以監(jiān)聽動(dòng)畫的執(zhí)行過程,這里設(shè)置控制器為代理。
代碼示例:
1 #import "YYViewController.h"2 3 @interface YYViewController ()4 @property(nonatomic,strong)CALayer *myLayer;5 @end6 7 @implementation YYViewController8 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 13 //創(chuàng)建layer 14 CALayer *myLayer=[CALayer layer]; 15 //設(shè)置layer的屬性 16 myLayer.bounds=CGRectMake(0, 0, 50, 80); 17 myLayer.backgroundColor=[UIColor yellowColor].CGColor; 18 myLayer.position=CGPointMake(50, 50); 19 myLayer.anchorPoint=CGPointMake(0, 0); 20 myLayer.cornerRadius=20; 21 //添加layer 22 [self.view.layer addSublayer:myLayer]; 23 self.myLayer=myLayer; 24 } 25 26 //設(shè)置動(dòng)畫(基礎(chǔ)動(dòng)畫) 27 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 28 { 29 //1.創(chuàng)建核心動(dòng)畫 30 // CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>] 31 CABasicAnimation *anima=[CABasicAnimation animation]; 32 33 //1.1告訴系統(tǒng)要執(zhí)行什么樣的動(dòng)畫 34 anima.keyPath=@"position"; 35 //設(shè)置通過動(dòng)畫,將layer從哪兒移動(dòng)到哪兒 36 anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)]; 37 anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)]; 38 39 //1.2設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫 40 anima.removedOnCompletion=NO; 41 //1.3設(shè)置保存動(dòng)畫的最新狀態(tài) 42 anima.fillMode=kCAFillModeForwards; 43 anima.delegate=self; 44 //打印 45 NSString *str=NSStringFromCGPoint(self.myLayer.position); 46 NSLog(@"執(zhí)行前:%@",str); 47 48 //2.添加核心動(dòng)畫到layer 49 [self.myLayer addAnimation:anima forKey:nil]; 50 51 } 52 53 -(void)animationDidStart:(CAAnimation *)anim 54 { 55 NSLog(@"開始執(zhí)行動(dòng)畫"); 56 } 57 58 -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 59 { 60 //動(dòng)畫執(zhí)行完畢,打印執(zhí)行完畢后的position值 61 NSString *str=NSStringFromCGPoint(self.myLayer.position); 62 NSLog(@"執(zhí)行后:%@",str); 63 } 64 65 @end打印position的屬性值,驗(yàn)證圖層的屬性值還是動(dòng)畫執(zhí)行前的初始值{50,50},并沒有真正被改變?yōu)閧200,300}。
三、縮放動(dòng)畫
實(shí)現(xiàn)縮放動(dòng)畫的代碼示例:
1 //2 // YYViewController.m3 // 08-核心動(dòng)畫平移4 //5 // Created by apple on 14-6-21.6 // Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,strong)CALayer *myLayer; 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 //創(chuàng)建layer 22 CALayer *myLayer=[CALayer layer]; 23 //設(shè)置layer的屬性 24 myLayer.bounds=CGRectMake(0, 0, 150, 60); 25 myLayer.backgroundColor=[UIColor yellowColor].CGColor; 26 myLayer.position=CGPointMake(50, 50); 27 myLayer.anchorPoint=CGPointMake(0, 0); 28 myLayer.cornerRadius=40; 29 //添加layer 30 [self.view.layer addSublayer:myLayer]; 31 self.myLayer=myLayer; 32 } 33 34 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 35 { 36 //1.創(chuàng)建動(dòng)畫 37 CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"]; 38 //1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間 39 anima.duration=2.0; 40 //1.2設(shè)置動(dòng)畫執(zhí)行完畢后不刪除動(dòng)畫 41 anima.removedOnCompletion=NO; 42 //1.3設(shè)置保存動(dòng)畫的最新狀態(tài) 43 anima.fillMode=kCAFillModeForwards; 44 //1.4修改屬性,執(zhí)行動(dòng)畫 45 anima.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; 46 //2.添加動(dòng)畫到layer 47 [self.myLayer addAnimation:anima forKey:nil]; 48 } 49 50 @end實(shí)現(xiàn)效果:
??
四、旋轉(zhuǎn)動(dòng)畫
代碼示例:
1 //2 // YYViewController.m3 // 09-核心動(dòng)畫旋轉(zhuǎn)4 //5 // Created by apple on 14-6-21.6 // Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,strong)CALayer *myLayer; 13 @end 14 15 @implementation YYViewController 16 - (void)viewDidLoad 17 { 18 [super viewDidLoad]; 19 20 //創(chuàng)建layer 21 CALayer *myLayer=[CALayer layer]; 22 //設(shè)置layer的屬性 23 myLayer.bounds=CGRectMake(0, 0, 150, 60); 24 myLayer.backgroundColor=[UIColor yellowColor].CGColor; 25 myLayer.position=CGPointMake(50, 50); 26 myLayer.anchorPoint=CGPointMake(0, 0); 27 myLayer.cornerRadius=40; 28 //添加layer 29 [self.view.layer addSublayer:myLayer]; 30 self.myLayer=myLayer; 31 } 32 33 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 34 { 35 //1.創(chuàng)建動(dòng)畫 36 CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"]; 37 //1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間 38 anima.duration=2.0; 39 //1.2修改屬性,執(zhí)行動(dòng)畫 40 anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)]; 41 //1.3設(shè)置動(dòng)畫執(zhí)行完畢后不刪除動(dòng)畫 42 anima.removedOnCompletion=NO; 43 //1.4設(shè)置保存動(dòng)畫的最新狀態(tài) 44 anima.fillMode=kCAFillModeForwards; 45 46 //2.添加動(dòng)畫到layer 47 [self.myLayer addAnimation:anima forKey:nil]; 48 } 49 @end實(shí)現(xiàn)效果:
? ?
提示:如果要讓圖形以2D的方式旋轉(zhuǎn),只需要把CATransform3DMakeRotation在z方向上的值改為1即可。
anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
四、補(bǔ)充
可以通過transform(KVC)的方式來進(jìn)行設(shè)置。
代碼示例(平移):
1 #import "YYViewController.h"2 3 @interface YYViewController ()4 @property(nonatomic,strong)CALayer *myLayer;5 @end6 7 @implementation YYViewController8 - (void)viewDidLoad9 { 10 [super viewDidLoad]; 11 12 //創(chuàng)建layer 13 CALayer *myLayer=[CALayer layer]; 14 //設(shè)置layer的屬性 15 myLayer.bounds=CGRectMake(0, 0, 150, 60); 16 myLayer.backgroundColor=[UIColor yellowColor].CGColor; 17 myLayer.position=CGPointMake(50, 50); 18 myLayer.anchorPoint=CGPointMake(0, 0); 19 myLayer.cornerRadius=40; 20 //添加layer 21 [self.view.layer addSublayer:myLayer]; 22 self.myLayer=myLayer; 23 } 24 25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 26 { 27 //1.創(chuàng)建動(dòng)畫 28 CABasicAnimation *anima=[CABasicAnimation animation]; 29 anima.keyPath=@"transform"; 30 //1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間 31 anima.duration=2.0; 32 //1.2修改屬性,執(zhí)行動(dòng)畫 33 34 anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)]; 35 //1.3設(shè)置動(dòng)畫執(zhí)行完畢后不刪除動(dòng)畫 36 anima.removedOnCompletion=NO; 37 //1.4設(shè)置保存動(dòng)畫的最新狀態(tài) 38 anima.fillMode=kCAFillModeForwards; 39 40 //2.添加動(dòng)畫到layer 41 [self.myLayer addAnimation:anima forKey:nil]; 42 }實(shí)現(xiàn)效果:
繪制的圖形在y的方向上移動(dòng)100個(gè)單位。
? ??
轉(zhuǎn)載于:https://www.cnblogs.com/LifeTechnologySupporter/p/10166751.html
總結(jié)
以上是生活随笔為你收集整理的iOS开发UI篇—核心动画(基础动画)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于lfslivecd-x86-6.3-
- 下一篇: 学习react的心路历程(一)