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设置。
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设置。
相关文章推荐
- iOS 相关证书申请
- IOS 错误个人总结
- iOS开发Bug汇总(持续更新)
- 【代码笔记】iOS-由身份证号码返回性别
- 【代码笔记】iOS-由身份证号码返回性别
- iOS 常用的第三方库
- 模拟“发送短信”按钮
- iOS页面间传值的一些方式总结
- 欲先攻其事必先利其器 (第三方资源篇)
- XGen 苹果IOS神器一键新机改串清理超级全息备份支持IOS789超IGV8使用分享(企鹅290093670)
- iOS页面间传值的一些方式总结
- 关于iOS 录音并且转码上传的相关问题
- 设计模式-外观模式
- iOS RTMP 视频直播
- iOS应用搜索
- iOS开发之Masonry框架源码深度解析
- iOS开发之NSURLProtocol的那些坑
- iOS Core Graphics封装虚线
- iOS socket小结01
- 自定义nagios监控脚本