核心動畫(基礎動畫)-轉
阿新 • • 發佈:2018-12-11
value orm mic 提示 行動 nis top mod 執行時間
一.簡單介紹 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)。
二.平移動畫
#import "ViewController.h"
@interface ViewController () @property(nonatomic,strong) CALayer *myLayer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //創建layer CALayer *layer = [CALayer layer]; //設置layer屬性 layer.bounds = CGRectMake(0, 0, 50, 80); layer.backgroundColor = [UIColor yellowColor].CGColor; layer.position = CGPointMake(50, 50); layer.anchorPoint = CGPointMake(0, 0); layer.cornerRadius = 20; //添加layer [self.view.layer addSublayer:layer]; _myLayer = layer; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. } //設置動畫(基礎動畫) - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //創建核心動畫 CABasicAnimation *animation = [CABasicAnimation animation]; //告訴系統要執行什麽的動畫 animation.keyPath = @"position"; //設置通過動畫,將layer從哪兒移動到哪兒 animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; //設置動畫執行完畢之後不刪除動畫 animation.removedOnCompletion = NO; //設置保持動畫的最新狀態 animation.fillMode = kCAFillModeForwards; //添加核心動畫到layer [self.myLayer addAnimation:animation forKey:nil]; } @end
說明: byValue和toValue的區別,前者是在當前的位置上增加多少,後者是到指定的位置。 設置代理:設置動畫的代理,可以監聽動畫的執行過程,這裏設置控制器為代理。
#import <QuartzCore/QuartzCore.h> @interface ViewController ()<CAAnimationDelegate> @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //創建layer CALayer *layer = [CALayer layer]; //設置layer的屬性 layer.bounds = CGRectMake(0, 0, 50, 80); layer.backgroundColor = [UIColor yellowColor].CGColor; layer.position = CGPointMake(50, 50); layer.anchorPoint = CGPointMake(0, 0); layer.cornerRadius = 20; //添加layer [self.view.layer addSublayer:layer]; self.myLayer = layer; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //設置動畫(基礎動畫) - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //創建核心動畫 CABasicAnimation *animation = [CABasicAnimation animation]; //告訴系統要執行什麽樣的動畫 animation.keyPath = @"position"; //設置通過動畫,將layer從哪兒移動到哪兒 animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; //設置動畫執行完畢之後不刪除動畫 animation.removedOnCompletion = NO; //設置保持動畫的最新狀態 animation.fillMode = kCAFillModeForwards; animation.delegate = self; //打印 NSString *str = NSStringFromCGPoint(self.myLayer.position); NSLog(@"執行前:%@",str); //添加核心動畫到layer [_myLayer addAnimation:animation forKey:nil]; } #pragma mark -Core Animation Delegate - (void)animationDidStart:(CAAnimation *)anim{ NSLog(@"開始執行動畫"); } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ //動畫執行完畢,打印執行完畢後的position值 NSString *str = NSStringFromCGPoint(_myLayer.position); NSLog(@"執行後:%@",str); } @end
通過打印結構可以看出,屬性值還是動畫執行前的初始值{50,50},並沒有真正被改變為{200,300}。
三.縮放動畫
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong) CALayer *myLayer; @end
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //創建layer CALayer *layer = [CALayer layer]; //設置layer的屬性 layer.bounds = CGRectMake(0, 0, 50, 80); layer.backgroundColor = [UIColor yellowColor].CGColor; layer.position = CGPointMake(50, 50); layer.anchorPoint = CGPointMake(0, 0); layer.cornerRadius = 20; //添加layer [self.view.layer addSublayer:layer]; self.myLayer = layer; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //設置動畫(基礎動畫) - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //創建動畫 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"]; //設置動畫執行時間 animation.duration = 2.0; //設置動畫執行完畢後不刪除動畫 animation.removedOnCompletion = NO; //設置保持動畫的最新狀態 animation.fillMode = kCAFillModeForwards; //修改屬性,執行動畫 animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; //添加動畫到layer [_myLayer addAnimation:animation forKey:nil]; } #pragma mark -Core Animation Delegate - (void)animationDidStart:(CAAnimation *)anim{ } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ } @end
四.旋轉動畫
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong) CALayer *myLayer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //創建layer CALayer *layer = [CALayer layer]; //設置layer的屬性 layer.bounds = CGRectMake(0, 0, 50, 80); layer.backgroundColor = [UIColor yellowColor].CGColor; layer.position = CGPointMake(50, 50); layer.anchorPoint = CGPointMake(0, 0); layer.cornerRadius = 20; //添加layer [self.view.layer addSublayer:layer]; self.myLayer = layer; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //設置動畫(基礎動畫) - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //創建動畫 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; //設置動畫執行時間 animation.duration = 2.0; //設置動畫執行完畢後不刪除動畫 animation.removedOnCompletion = NO; //設置保持動畫的最新狀態 animation.fillMode = kCAFillModeForwards; //修改屬性,執行動畫 animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)]; //添加動畫到layer [_myLayer addAnimation:animation forKey:nil]; } #pragma mark -Core Animation Delegate - (void)animationDidStart:(CAAnimation *)anim{} - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{} @end
提示:如果要讓圖形以2D的方式旋轉,只需要把CATransform3DMakeRotation在z方向上的值改為1即可。
五.補充
可以通過transform (KVC)的方式來進行設置
@interface ViewController () @property(nonatomic,strong) CALayer *myLayer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //創建layer CALayer *layer = [CALayer layer]; //設置layer的屬性 layer.bounds = CGRectMake(0, 0, 50, 80); layer.backgroundColor = [UIColor yellowColor].CGColor; layer.position = CGPointMake(50, 50); layer.anchorPoint = CGPointMake(0, 0); layer.cornerRadius = 20; //添加layer [self.view.layer addSublayer:layer]; self.myLayer = layer; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //設置動畫(基礎動畫) - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //創建動畫 CABasicAnimation *animation = [CABasicAnimation animation]; animation.keyPath = @"transform"; //設置動畫執行時間 animation.duration = 2.0; //設置動畫執行完畢後不刪除動畫 animation.removedOnCompletion = NO; //設置保持動畫的最新狀態 animation.fillMode = kCAFillModeForwards; //修改屬性,執行動畫 animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)]; //添加動畫到layer [_myLayer addAnimation:animation forKey:nil]; } #pragma mark -Core Animation Delegate - (void)animationDidStart:(CAAnimation *)anim{} - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{} @end
核心動畫(基礎動畫)-轉