(1) layer:UIView之所以能显示到屏幕上,就是因为其内部有一个图层,在创建UIView对象时,UIView内部自动创建一个图层(CALayer对象),可以通过UIView的layer属性设置这个图层的相关东西。
(2)UIView本身不能显示内容,当UIView需要显示到屏幕上时,会调用drawRect方法。然后把所绘内容放在自己的图层上。然后通过图层显示到屏幕上。
(3)每个UIView内部都默认关联着一个CALaye对象(图层),称这个layer为Root Layer(根图层)。所有的非Root Layer都存在隐式动画,隐式动画时长为0.25秒。
(4)隐式动画,在非Root Layer的部分属性发生改变时,产生的动画效果
- (void)viewDidLoad {
[super viewDidLoad];
//创建图层
CALayer *subLayer = [[CALayer alloc]init];
//将图层添加view
[self.view.layer addSublayer:subLayer];
//- - - - - - 常用属性 - - - - - -//
//设置图层的大小
subLayer.bounds = CGRectMake(0, 0, 200, 200);
//设置图层的显示位置
subLayer.position = CGPointMake(100, 100);
subLayer.backgroundColor = [UIColor redColor].CGColor;
//设置锚点
subLayer.anchorPoint = CGPointMake(0.5, 0.5);
//设置显示的内容
UIImage *img = [UIImage imageNamed:@"1.jpg"];
subLayer.contents = (id)img.CGImage;
}
/*隐式动画*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
//关闭动画
// [CATransaction begin];
// [CATransaction setDisableActions:YES];
//1修改位置
_layer.position = point;
//2.改变layer 的大小
CGFloat num = arc4random_uniform(50)+30;
_layer.bounds = CGRectMake(0, 0, num, num);
//改变透明度
_layer.opacity = arc4random_uniform(10)*0.1;
//背景色
_layer.backgroundColor = [UIColor colorWithRed:arc4random_uniform(10)*0.1 green:arc4random_uniform(10)*0.1 blue:arc4random_uniform(10)*0.1 alpha:1].CGColor;
//设置边框的弧度
_layer.cornerRadius = arc4random_uniform(30);
//transform
//_layer.transform = CATransform3DMakeScale(2, 1, 1);
//提交关闭动画
// [CATransaction commit];
}