博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS编程(双语版)-视图-Frame/Bounds/Center
阅读量:7105 次
发布时间:2019-06-28

本文共 3890 字,大约阅读时间需要 12 分钟。

1. Frame

每个视图都有一个frame属性,它是CGRect结构,它描述了视图所在的矩形在其父视图中的位置

(屏幕坐标系默认的原点在左上角,x轴向右伸展,y轴向下伸展)

设置frame通常通过视图的指定初始化器initWithFrame

下面来看个例子,该例子初始化了3个相互叠加的矩形区域

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];UIView* v2 = [[UIView alloc] initWithFrame:CGRectMake(41, 56, 132, 194)];v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];UIView* v3 = [[UIView alloc] initWithFrame:CGRectMake(43, 197, 160, 230)];v3.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];[mainview addSubview: v1];[v1 addSubview: v2];[mainview addSubview: v3];

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)let v2 = UIView(frame:CGRectMake(41, 56, 132, 194))v2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1)let v3 = UIView(frame:CGRectMake(43, 197, 160, 230))v3.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)mainview.addSubview(v1)v1.addSubview(v2)mainview.addSubview(v3)

 

运行结果:

 

2. Bounds

Bounds也是CGRect结构,和Frame不同,它描述的是视图自身的矩形区域,是相对于自身的坐标系而言的。

下面的例子创建了2个叠加的矩形视图,子视图为绿色较小的那个

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];// 在一个视图内部画图时,通常需要使用该视图的boundsUIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, 10, 10)];v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];[mainview addSubview: v1];[v1 addSubview: v2];

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)// 在一个视图内部画图时,通常需要使用该视图的boundslet v2 = UIView(frame:v1.bounds.insetBy(dx: 10, dy: 10))v2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1)mainview.addSubview(v1)v1.addSubview(v2)

 运行结果:

 

下面的例子通过改变绿色子视图的bounds将父视图完全覆盖

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];UIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, 10, 10)];v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];[mainview addSubview: v1];[v1 addSubview: v2];// 重定义子视图的boundsCGRect r = v2.bounds;r.size.height += 20;r.size.width += 20;v2.bounds = r;

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)let v2 = UIView(frame:v1.bounds.insetBy(dx: 10, dy: 10))v2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1)mainview.addSubview(v1)v1.addSubview(v2)v2.bounds.size.height += 20v2.bounds.size.width += 20

 运行结果:

 

下面的例子,紫色父视图的原点进行少量偏移

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];UIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, 10, 10)];v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];[mainview addSubview: v1];[v1 addSubview: v2];// 改变父视图的原点坐标CGRect r = v1.bounds;r.origin.x += 10;r.origin.y += 10;v1.bounds = r;

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)let v2 = UIView(frame:v1.bounds.insetBy(dx: 10, dy: 10))v2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1)mainview.addSubview(v1)v1.addSubview(v2)// 改变父视图的原点坐标v1.bounds.origin.x += 10v1.bounds.origin.y += 10

 

运行结果:

 

3. Center

Center即视图的中心点位置坐标

 

4. 关于主窗口和设备屏幕

设备屏幕(UIScreen.mainScreen())没有frame, 但它有bounds。

主窗口没有父视图,但是它的frame可以设为屏幕的bounds。

let w = UIWindow(frame: UIScreen.mainScreen().bounds)

 

5. 关于frame和bounds的区别

一言以蔽之,就是前者相对于父视图,而后者相对于自身。

我们还是用图片来看一下吧,这样更直观

在视图未旋转的情况下,它们差不多,坐标稍有区别,如下图:

 

而在视图作了类似旋转的transform之后,它们的坐标则有很大的差别了,见下图:

 

转载地址:http://ivjhl.baihongyu.com/

你可能感兴趣的文章
如何在你的项目中集成 CAP【手把手视频教程】
查看>>
python学习之老男孩python全栈第九期_day028知识点总结——面向对象进阶、hashlib...
查看>>
小凯的数字 数论
查看>>
在 Android Studio 上调试数据库 ( SQLite )
查看>>
UItableview全部属性、方法以及代理方法执行顺序
查看>>
图片文字css小知识点
查看>>
安卓 消息队列 优先级 顺序
查看>>
微软 Share Point “.NET研究”2010 企业应用解决方案
查看>>
微软下周将发布10个安全补丁 严重漏洞超过半数
查看>>
Microsoft NLayerApp“.NET研究”案例理论与实践 - 项目简介与环境搭建
查看>>
信息周刊:微软视窗系统为何倍受病毒欢迎?
查看>>
Infinispan's GridFileSystem--基于内存的网格文件系统,互联网营销
查看>>
一起谈.NET技术,NET下RabbitMQ实践 [示例篇]
查看>>
一起谈.NET技术,Silverlight 游戏开发小技巧:传说中的透视跑马灯
查看>>
浅析react中间件机制
查看>>
layui--入门(helloWorld)
查看>>
SQLI LABS Basic Part(1-22) WriteUp
查看>>
Windows环境下CGAL的安装
查看>>
基本命令
查看>>
JBOSS集群和安装
查看>>