隱式動畫 - CALayer - 找規律遊戲

      1.所謂隱式動畫,就是操作CALayer的一些屬性,而系統自帶有動畫效果,在CALayer裏面的屬性,凡是用

 Animatable.描述了的,都帶有隱式動畫效果

   2.要實現隱式動畫效果,出了屬性是Animatable描述之外,還要是自定義layer對象,系統的layer對象

 也是沒有隱式動畫效果的

   3.在下面代碼例子中,除了自定義layer之外,還加入隨機顏色/數字和  事務的概念;

#import "ViewController.h"


@interface ViewController ()


@property(nonatomic,strong)CALayer *layer;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    CALayer *layer = [CALayer layer];

    layer.frame = CGRectMake(60, 60, 100, 100);

    layer.backgroundColor = [UIColor redColor].CGColor;

    

    self.layer = layer;

    [self.view.layer addSublayer:layer];

    

}

//當點擊屏幕開啓隱式動畫效果

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

     //開啓事務

    [CATransaction begin];

    //設置動畫時長

    [CATransaction setAnimationDuration:1.5];

    _layer.backgroundColor = [self changeColor].CGColor;

    

    _layer.cornerRadius = arc4random_uniform(50);

    

    _layer.borderWidth = arc4random_uniform(10);

    

    _layer.borderColor = [self changeColor].CGColor;

    

    _layer.position = CGPointMake(arc4random_uniform(375), arc4random_uniform(667));

    //執行事務

    [CATransaction commit];

}

//返回隨機顏色

-(UIColor *)changeColor{

    

    CGFloat r = arc4random_uniform(256)/255.0;

    CGFloat g = arc4random_uniform(256)/255.0;

    CGFloat b = arc4random_uniform(256)/255.0;

    return [UIColor colorWithRed:r green:g blue:b alpha:1];

}

@end


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章