一、簡單介紹
CAPropertyAnimation的子類
屬性解析:
fromValue:keyPath相應屬性的初始值
toValue:keyPath相應屬性的結束值
随着動畫的進行,在長度為duration的持續時間内,keyPath相應屬性的值從fromValue漸漸地變為toValue
如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那麼在動畫執行完畢後,圖層會保持顯示動畫執行後的狀态。但在實質上,圖層的屬性值還是動畫執行前的初始值,并沒有真正被改變。
比如,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10),toValue為(100,100),雖然動畫執行完畢後圖層保持在(100,100)這個位置,實質上圖層的position還是為(0,0)
二、平移動畫
代碼示例:
//
// YYViewController.m
// 07-核心動畫(基礎動畫)
//
#import "YYViewController.h"
@interface YYViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立layer
CALayer *myLayer=[CALayer layer];
//設定layer的屬性
myLayer.bounds=CGRectMake(, , , );
myLayer.backgroundColor=[UIColor yellowColor].CGColor;
myLayer.position=CGPointMake(, );
myLayer.anchorPoint=CGPointMake(, );
myLayer.cornerRadius=;
//添加layer
[self.view.layer addSublayer:myLayer];
self.myLayer=myLayer;
}
//設定動畫(基礎動畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.建立核心動畫
// CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
CABasicAnimation *anima=[CABasicAnimation animation];
//1.1告訴系統要執行什麼樣的動畫
anima.keyPath=@"position";
//設定通過動畫,将layer從哪兒移動到哪兒
anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(, )];
anima.toValue=[NSValue valueWithCGPoint:CGPointMake(, )];
//1.2設定動畫執行完畢之後不删除動畫
anima.removedOnCompletion=NO;
//1.3設定儲存動畫的最新狀态
anima.fillMode=kCAFillModeForwards;
//2.添加核心動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
@end
代碼說明:
第42行設定的keyPath是@”position”,說明要修改的是CALayer的position屬性,也就是會執行平移動畫
第44,45行,這裡的屬性接收的時id類型的參數,是以并不能直接使用CGPoint這種結構體類型,而是要先包裝成NSValue對象後再使用。
預設情況下,動畫執行完畢後,動畫會自動從CALayer上移除,CALayer又會回到原來的狀态。為了保持動畫執行後的狀态,可以加入第48,50行代碼
byValue和toValue的差別,前者是在目前的位置上增加多少,後者是到指定的位置。
執行效果:

設定代理:設定動畫的代理,可以監聽動畫的執行過程,這裡設定控制器為代理。
代碼示例:
#import "YYViewController.h"
@interface YYViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立layer
CALayer *myLayer=[CALayer layer];
//設定layer的屬性
myLayer.bounds=CGRectMake(, , , );
myLayer.backgroundColor=[UIColor yellowColor].CGColor;
myLayer.position=CGPointMake(, );
myLayer.anchorPoint=CGPointMake(, );
myLayer.cornerRadius=;
//添加layer
[self.view.layer addSublayer:myLayer];
self.myLayer=myLayer;
}
//設定動畫(基礎動畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.建立核心動畫
// CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
CABasicAnimation *anima=[CABasicAnimation animation];
//1.1告訴系統要執行什麼樣的動畫
anima.keyPath=@"position";
//設定通過動畫,将layer從哪兒移動到哪兒
anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(, )];
anima.toValue=[NSValue valueWithCGPoint:CGPointMake(, )];
//1.2設定動畫執行完畢之後不删除動畫
anima.removedOnCompletion=NO;
//1.3設定儲存動畫的最新狀态
anima.fillMode=kCAFillModeForwards;
anima.delegate=self;
//列印
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執行前:%@",str);
//2.添加核心動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
-(void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"開始執行動畫");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//動畫執行完畢,列印執行完畢後的position值
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執行後:%@",str);
}
@end
列印position的屬性值,驗證圖層的屬性值還是動畫執行前的初始值{50,50},并沒有真正被改變為{200,300}。
三、縮放動畫
實作縮放動畫的代碼示例:

@property(nonatomic,strong)CALayer *myLayer;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立layer
CALayer *myLayer=[CALayer layer];
//設定layer的屬性
myLayer.bounds=CGRectMake(, , , );
myLayer.backgroundColor=[UIColor yellowColor].CGColor;
myLayer.position=CGPointMake(, );
myLayer.anchorPoint=CGPointMake(, );
myLayer.cornerRadius=;
//添加layer
[self.view.layer addSublayer:myLayer];
self.myLayer=myLayer;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.建立動畫
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"];
//1.1設定動畫執行時間
anima.duration=;
//1.2設定動畫執行完畢後不删除動畫
anima.removedOnCompletion=NO;
//1.3設定儲存動畫的最新狀态
anima.fillMode=kCAFillModeForwards;
//1.4修改屬性,執行動畫
anima.toValue=[NSValue valueWithCGRect:CGRectMake(, , , )];
//2.添加動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
@end)
實作效果:
四、旋轉動畫
代碼示例:
//
// YYViewController.m
// 09-核心動畫旋轉
//
#import "YYViewController.h"
@interface YYViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立layer
CALayer *myLayer=[CALayer layer];
//設定layer的屬性
myLayer.bounds=CGRectMake(, , , );
myLayer.backgroundColor=[UIColor yellowColor].CGColor;
myLayer.position=CGPointMake(, );
myLayer.anchorPoint=CGPointMake(, );
myLayer.cornerRadius=;
//添加layer
[self.view.layer addSublayer:myLayer];
self.myLayer=myLayer;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.建立動畫
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"];
//1.1設定動畫執行時間
anima.duration=;
//1.2修改屬性,執行動畫
anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, , , )];
//1.3設定動畫執行完畢後不删除動畫
anima.removedOnCompletion=NO;
//1.4設定儲存動畫的最新狀态
anima.fillMode=kCAFillModeForwards;
//2.添加動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
@end
實作效果:
提示:如果要讓圖形以2D的方式旋轉,隻需要把CATransform3DMakeRotation在z方向上的值改為1即可。
五、補充
可以通過transform(KVC)的方式來進行設定。
代碼示例(平移):
#import "YYViewController.h"
@interface YYViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立layer
CALayer *myLayer=[CALayer layer];
//設定layer的屬性
myLayer.bounds=CGRectMake(, , , );
myLayer.backgroundColor=[UIColor yellowColor].CGColor;
myLayer.position=CGPointMake(, );
myLayer.anchorPoint=CGPointMake(, );
myLayer.cornerRadius=;
//添加layer
[self.view.layer addSublayer:myLayer];
self.myLayer=myLayer;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.建立動畫
CABasicAnimation *anima=[CABasicAnimation animation];
anima.keyPath=@"transform";
//1.1設定動畫執行時間
anima.duration=;
//1.2修改屬性,執行動畫
anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(, , )];
//1.3設定動畫執行完畢後不删除動畫
anima.removedOnCompletion=NO;
//1.4設定儲存動畫的最新狀态
anima.fillMode=kCAFillModeForwards;
//2.添加動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
實作效果:
繪制的圖形在y的方向上移動100個機關。