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