iOS中View和layer的区别:
1、View负责处理事务,layer用于显示.
2、layer是CALayer的类型,其创建方式和UIView创建方式类似。不过通过此方式添加的图层在模拟器的层次结构中只能看到一个view。其相当于在view上染了一个宽和高分别为80的湖蓝色,而不是在view上添加了一个宽和高为80的view。
layer用法:
1、CALayer *layer = [[CALayer alloc] init];
2、layer.bounds = CGRectMake(100,100,80,80); //设置其大小
3、layer.backgroundColor = [UIColor cyanColor].CGColor; //设置其背景色
4、layer.position = CGPointMake(200,200); //设置其位置
5、[self.view.layer addSublayer:layer]; //将其添加到根图层上
iOS中View和layer的区别以及layer用法
View负责处理事务,layer用于显示.
layer是CALayer的类型,其创建方式和UIView创建方式类似。不过通过此方式添加的图层在模拟器的层次结构中只能看到一个view。其相当于在view上染了一个宽和高分别为80的湖蓝色,而不是在view上添加了一个宽和高为80的view。
CALayer *layer = [[CALayer alloc] init];
//设置其大小
layer.bounds = CGRectMake(100,100,80,80);
//设置其背景色
layer.backgroundColor = [UIColor cyanColor].CGColor;
//设置其位置
layer.position = CGPointMake(200,200);
//将其添加到根图层上
[self.view.layer addSublayer:layer];
另外有一段苹果的官方文档:
/* Unlike NSView, each Layer in the hierarchy has an implicit frame
* rectangle, a function of the `position', `bounds', `anchorPoint',
* and `transform' properties. When setting the frame the `position'
* and `bounds.size' are changed to match the given frame. */
由此文档可知frame是由position', `bounds', `anchorPoint',`transform'而计算出来的。
所以当我们要更改一个图层的位置或者大小时只需要通过position和bounds来更改即可。不需要用frame设置。