圖層的核心動畫(CABaseAnimation)續

Main.storyboard測試

ViewController.m動畫

//atom

//  ViewController.mspa

//  8A01.核心動畫orm

//對象

//  Created by huan on 16/2/4.blog

//  Copyright © 2016 huanxi. All rights reserved.rem

//animation

 

#import "ViewController.h"it

 

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

 

@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{

//    [self testPositionAnimation];

//    [self testTransformAnimation];//形變屬性:平移

//    [self testTransformRotationAnimation];

    [self testTransformScaleAnimation];

}

 

#pragma mark 測試位置的動畫

-(void)testPositionAnimation{

    //核心動畫使用步驟

    //1.建立一個動畫對象

    CABasicAnimation *animation = [CABasicAnimation animation];

    //設置動畫類型

    animation.keyPath = @"position";

    //動畫執行的初始狀態

//    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

    //動畫執行的"最終狀態"

//    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 250)];

    

    //每次動畫執行的增長值

    animation.byValue = [NSValue valueWithCGPoint:CGPointMake(10, 10)];

    

    //保存動畫執行狀態

    //解決方案2:使動畫保存執行以後的狀態,只要設置動畫的兩個屬性

    animation.removedOnCompletion = NO;// 動畫對象不要移除

    animation.fillMode = kCAFillModeForwards;//保存當前的狀態

    //2.往控件的圖層添加動畫

    [self.imageView.layer addAnimation:animation forKey:nil];

    

}

#pragma mark 測試形變的平移動畫

-(void)testTransformAnimation{

    //1.建立一個動畫對象

    CABasicAnimation *animation = [CABasicAnimation animation];

    //設置動畫類型

//    animation.keyPath = @"transform.translation";

    animation.keyPath = @"transform.translation.x";

    

    //每次動畫執行的增長值

//    animation.byValue = [NSValue valueWithCGPoint:CGPointMake(10, 10)];

    //byValue的數據類型是 keypath 決定的

    animation.byValue = @10;

    

    //保存動畫執行狀態

    //解決方案2:使動畫保存執行以後的狀態,只要設置動畫的兩個屬性

    animation.removedOnCompletion = NO;// 動畫對象不要移除

    animation.fillMode = kCAFillModeForwards;//保存當前的狀態

    //2.往控件的圖層添加動畫

    [self.imageView.layer addAnimation:animation forKey:nil];

 

}

 

#pragma mark 測試形變的旋轉的動畫

-(void)testTransformRotationAnimation{

    //1.建立一個動畫對象

    CABasicAnimation *animation = [CABasicAnimation animation];

    //設置動畫類型

    

    animation.keyPath = @"transform.rotation.x";

    

   

    //byValue的數據類型是 keypath 決定的

    animation.byValue = @(M_PI_4);

    

    //保存動畫執行狀態

    //解決方案2:使動畫保存執行以後的狀態,只要設置動畫的兩個屬性

    animation.removedOnCompletion = NO;// 動畫對象不要移除

    animation.fillMode = kCAFillModeForwards;//保存當前的狀態

    //2.往控件的圖層添加動畫

    [self.imageView.layer addAnimation:animation forKey:nil];

 

}

 

#pragma mark 測試形變的縮放的動畫

-(void)testTransformScaleAnimation{

    //1.建立一個動畫對象

    CABasicAnimation *animation = [CABasicAnimation animation];

    //設置動畫類型 ==>>keyPath 設置圖層的屬性 bounds/position/transform

    //    animation.keyPath = @"transform.translation";

//    animation.keyPath = @"transform.scale";

    animation.keyPath = @"transform.scale.x";

    

    //設置動畫的時間

    animation.duration = 3;

    //byValue的數據類型是 keypath 決定的

    animation.byValue = @1.5;

    

    //保存動畫執行狀態

    //解決方案2:使動畫保存執行以後的狀態,只要設置動畫的兩個屬性

    animation.removedOnCompletion = NO;// 動畫對象不要移除

    animation.fillMode = kCAFillModeForwards;//保存當前的狀態

    //2.往控件的圖層添加動畫

    [self.imageView.layer addAnimation:animation forKey:nil];

 

}

 

@end

結果

相關文章
相關標籤/搜索