隐式动画
- 什么是图层的隐式动画?
- 当我们修改CALayer的一些属性,我们可以发现它会有类似动画效果一样的变化,这是图层独有的特性。
- 这样的特性UIView是没有的,当修改UIView的图层属性,会是很生硬的变化,达不到动画的效果。
示例演示
示例图
- 核心代码 ViewController.m
#import "ViewController.h"
//转换角度
#define angle2radion(angle) ((angle) / 180 * M_PI)
@interface ViewController ()
@property(nonatomic,weak)CALayer * layer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CALayer * layer = [CALayer layer];
//layer的位置
layer.position = CGPointMake(200, 150);
//大小
layer.bounds = CGRectMake(0, 0, 80, 80);
//背景颜色
layer.backgroundColor = [UIColor redColor].CGColor;
// layer.contents = (id)[UIImage imageNamed:@"bbb"].CGImage;
[self.view.layer addSublayer:layer];
_layer = layer;
//scheduled... 这个方法自动将定时器加入runLoop
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(change) userInfo:nil repeats:YES];
}
-(void)change
{
NSLog(@"%s",__func__);
//旋转角度的变化,以z轴为轴
_layer.transform = CATransform3DMakeRotation(angle2radion(arc4random_uniform(360)+1), 0, 0, 1);
//位置变化
_layer.position = CGPointMake(arc4random_uniform(200)+20, arc4random_uniform(400)+50);
//圆角变化
_layer.cornerRadius = arc4random_uniform(40);
//背景颜色变化
_layer.backgroundColor = [self arcColor].CGColor;
//边框变化
_layer.borderColor =[self arcColor].CGColor;
_layer.borderWidth = 10;
}
/**
颜色随机
*/
-(UIColor*)arcColor
{
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