標籤:
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *redView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //1.建立動畫對象(設定layer的屬性值.) CABasicAnimation *anim = [CABasicAnimation animation]; //2.設定屬性值 anim.keyPath = @"position.x"; anim.toValue = @300; //動畫完成時, 會自動刪除動畫 anim.removedOnCompletion = NO; anim.fillMode = @"forwards"; //3.添加動畫:key值是為了區分不同的動畫 [self.redView.layer addAnimation:anim forKey:nil]; }
核心動畫之作用在層上面.
動畫的本質是改圖層的某一個屬性.
CABasicAnimation *anim = [CABasicAnimation animation];
圖層有那些屬性,這裡才能寫那些屬性.
anim.keyPath = @"transform.scale";
anim.toValue = @0.5;
告訴動畫完成的時候不要移除
anim.removedOnCompletion = NO;
儲存動畫最前面的效果.也就是最後一個設定的效果
anim.fillMode = kCAFillModeForwards;
把動畫添加到層上面.
[_redView.layer addAnimation:anim forKey:nil];
二:心跳效果
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageV;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //建立動畫對象 CABasicAnimation *anim = [CABasicAnimation animation]; //設定屬性值 anim.keyPath = @"transform.scale"; anim.toValue = @0; //設定動畫執行次數 anim.repeatCount = MAXFLOAT; //設定動畫執行時間長度 anim.duration = 3; //自動反轉(怎麼樣去 怎麼樣回來) anim.autoreverses = YES; //添加動畫 [self.imageV.layer addAnimation:anim forKey:nil]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
思路:就是讓一張圖片做一個放大縮放小的動畫.
代碼實現:
CABasicAnimation *anim =[CABasicAnimation animation];
設定縮放屬性
anim.keyPath = @"transform.scale";
縮放到最小
anim.toValue = @0;
設定動畫執行的次數
anim.repeatCount = MAXFLOAT;
設定動畫執行的時間長度
anim.duration = 0.25;
設定動畫自動反轉(怎麼去, 怎麼回)
anim.autoreverses = YES;
添加動畫
[self.heartView.layer addAnimation:anim forKey:nil];
ios開發之核心動畫四:核心動畫-Core Animation--CABasicAnimation基礎核心動畫