今天在学习有关UIView时,关于Bounds和Frame的问题困扰多时,今日研究了一翻,有所收获,遂记之。

一、问题来源

网上有关bounds和frames的比较的文章主要就是一篇:http://blog.csdn.net/mad1989/article/details/8711697,核心思想是bounds的坐标系是相对于自己而言,而frames的坐标系是相对于父视图,主要的演示代码如下:

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
[view1 setBounds:CGRectMake(-20, -20, 200, 200)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];//添加到self.view
NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds)); UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.backgroundColor = [UIColor yellowColor];
[view1 addSubview:view2];//添加到view1上,[此时view1坐标系左上角起点为(-20,-20)]
NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));

如果按以上代码运行可以得到期望的结果:

2017-03-12 21:32:04.474 BoundsAndFrame[5910:450792] view1's frame {{20, 20}, {200, 200}}=========view1 bounds:{{-20, -20}, {200, 200}}

2017-03-12 21:32:04.475 BoundsAndFrame[5910:450792] view2 frame:{{0, 0}, {100, 100}}=========view2 bounds:{{0, 0}, {100, 100}}

我们想将这个图形结果进行一些更改:

1、将红色区域变成正方形,并将其宽度加长一倍,变成400。

2、将黄色图形向下移动,使其上边缘距红色图形内部边缘底部为10。如下图所示:

将其代码更改为:

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
[view1 setBounds:CGRectMake(-20, -190, 400, 200)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];//添加到self.view
NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds)); UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.backgroundColor = [UIColor yellowColor];
[view1 addSubview:view2];//添加到view1上,[此时view1坐标系左上角起点为(-20,-20)]
NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));

但得到的结果如下所示,并不是我们想要的。

2017-03-12 21:40:57.170 BoundsAndFrame[5950:457772] view1's frame {{-80, 20}, {400, 200}}=========view1 bounds:{{-20, -190}, {400, 200}}

2017-03-12 21:40:57.170 BoundsAndFrame[5950:457772] view2 frame:{{0, 0}, {100, 100}}=========view2 bounds:{{0, 0}, {100, 100}}

二、问题分析

按上面比较bounds和frames区别的那篇文章来讲,

[view1 setBounds:CGRectMake(-20, -190, 400, 200)];

这个语句中的setBounds方法的功能是将本view的bounds坐标原点强制设置成(-20,-190),并将CGRect的宽和高设置成(400,200)。

按计算,被转换过后的view1的frame坐标应该还是(20,20),宽高(400,200)。

三、问题解决

那篇比较bounds和frames的文章中有一个重要的问题没有提到,最后在Apple的API Reference中找到答案:

Changing the bounds size grows or shrinks the view relative to its center point.(对形状边界进行扩大或缩小,是相对于其中心点进行的。)

我概括使用bounds属性进行坐标和大小变换的原则:以中心为原点进行缩放,先宽高后坐标。

是要先进行宽和高的变换,比如这上面那句

[view1 setBounds:CGRectMake(-20, -190, 400, 200)];

宽度的变换是以中心为原点,向两边各延长100,高为200不变。

大小变换完成之后,再将得到的图形的原点(左上角顶点)的bounds坐标设置为(-20,-190)。

这样,再进行计算,便得到了上述结果。

Objective-C日记-Bounds和Frame的更多相关文章

  1. iOS开发——项目篇—高仿百思不得姐 05——发布界面、发表文字界面、重识 bounds、frame、scrollView

    加号界面(发布模块) 一.点击加号modal出发布模块,创建控件,布局控件1)使用xib加载view,如果在viewDidLoad创建控件并设置frame 那么self.view 的宽高 拿到的是xi ...

  2. IOS中bounds和frame

    * 用bounds和frame来修改尺寸是有一些小区别的 三.isEqual:方法 1> 系统会根据对象isEqual方法的返回值来决定两个对象是否相同 * 比如判断对象a和b是否相同,就会查看 ...

  3. bounds 和 frame

    使用环境: 一个UIView 添加xib View 时需要注意使用 frame and bounds 代码: frame 和 bounds 对比 样式对比 其实说白了就是 frame: 该view在父 ...

  4. UIView 中bounds和frame的差别

    搞iOS开发的童鞋基本都会用过UIView,那他的bounds和frame两个属性也不会陌生,那这两个有什么实质性的区别呢? 先看到下面的代码你肯定就明白了一些: -(CGRect)frame{    ...

  5. bounds与frame的区别及setBounds的使用

    转自http://www.cocoachina.com/ios/20140925/9755.html 在iOS开发中经常遇到两个词Frame和bounds,本文主要阐述Frame和bound的区别,尤 ...

  6. iOS bounds、frame之间的关系

    这几个都是在ios程序中,经常会注意到的一些小细节,能否真正了解这些,对写ios程序也有很大的好处. frame 是UIView中表示此view的一个矩形面积,包括了view在它的superview中 ...

  7. iOS界面篇 - bounds和frame的相同和区别

    相同点: 他们都是CGRect类型,且拥有属性origin(x, y),  size(weight, height) 不同点: bounds是你画的视图的边界,和父视图没有半毛钱关系 frames则一 ...

  8. bounds 和frame区别

    仔细看下这个图就知道了

  9. iOS bounds vs frame

    斯坦福iOS开发课程的白胡子大叔的PPT解释得淋漓尽致!

随机推荐

  1. JQuery的$.each(list,function(i,a){} 中 i和a 未定义,a取不到值

    $.each(list,function(i,a){ var status = ""; if(a.status==0) status="未审核"; else i ...

  2. python3 入门基础

    1.查看python版本 python -V 2.指定python文件的编码格式 # -*- coding: utf-8 -*- 3.变量命名以字母和下划线(_)开始 _num num 4.注释 # ...

  3. Ionic在线打包IOS平台应用

    参见:http://docs.ionic.io/services/profiles/#ios-app-certificate--provisioning-profile Ionic云编译,需要注册.地 ...

  4. HDU-4371-Alice and Bob

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4371 这题在比赛的时候看错了题意,卡了很久都没做出来,也没有再回头仔细去看题,所以没做出来,之后再看别 ...

  5. Usermod:user oracle is currently logged in 家目录不能改变解决方法

    [root@HE1 ~]# usermod -u 200 -g oinstall -G dba,asmdba,oper oracle[root@HE1 ~]# id oracleuid=200(ora ...

  6. Windows Server 2008 R2防火墙入站规则

    一般服务器的端口都设置了外网无法访问,iis中创建的网站外网也是访问不了的,需要创建指定端口的入站规则后方可访问. 方法/步骤     服务器管理器-->配置-->高级安全windows防 ...

  7. MyBatis java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符

    http://blog.sina.com.cn/s/blog_6da7fcff0101jewf.html 查看SQL语句是否多加了分号";"

  8. HTML5行业现状与未来 - 2016年终大盘点

    * { margin: 0; padding: 0 } .con { width: 802px; margin: 0 auto; text-align: center; position: inher ...

  9. 搭建typescript开发环境最详细的全过程

    搭建typescript开发示例https://github.com/Microsoft/TypeScriptSamples typescript案例https://www.tslang.cn/sam ...

  10. We Chall-Training: ASCII—Writeup

    MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...