您的位置:首页 > 移动开发 > IOS开发

iOS中View和layer的区别以及layer用法

2016-07-19 09:48 330 查看
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设置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: