ios開發之核心動畫四:核心動畫-Core Animation--CABasicAnimation基礎核心動畫

來源:互聯網
上載者:User

標籤:

 

#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基礎核心動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.