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