先看下官方以及个人翻译:

frame: This rectangle defines the size and position of the view in its superview’s coordinate system. Use this rectangle during layout operations to set the size and position the view. Setting this property changes the point specified by the center property and changes the size in the bounds rectangle accordingly. The coordinates of the frame rectangle are always specified in points.

frame:一个矩形框,描述了view在其父控件坐标系中的大小和位置。修改frame会相应的改变center的值以及bounds.size属性。

bounds:The bounds rectangle, which describes the view’s location and size in its own coordinate system.The default bounds origin is (0,0) and the size is the same as the size of the rectangle in the frame property. Changing the size portion of this rectangle grows or shrinks the view relative to its center point. Changing the size also changes the size of the rectangle in the frame property to match. The coordinates of the bounds rectangle are always specified in points.

bounds:一个矩形框,描述了view在其自己坐标系中的大小和位置。bounds.origin默认是(0,0),bounds.size和frame.size一致。改变bounds.size,会相对view的 center point增加或减少矩形框。改变bounds.size也相应的改变 frame.size。

--》这里我补一句,既然相对于center point向四周增加、减少,那必然也会改变frame.origin

--》对比还发现,更改bounds并没有改变center property,但是修改frame会改变,也就引发了后文提及的注意事项。

总结:

1. 改变bounds的origin,相当于改变view内容显示的起点位置。比如,红色矩形view在右下角有个按钮(该按钮为红色view的子控件),如果origin设置为(20,20),那么会发现按钮开始靠近左上角,发生了位置偏移。

2. 修改一个控件的bounds.origin,不会改变该控件以及其子控件的frame,但是由于显示内容的起点位置发生改变,因此,会相对(相对于控件的左上角)改变子控件的显示位置。因此,其子控件的frame中的origin,并不一定是该控件的左上角,只有当该控件的bounds的origin为(0,0)时才是左上角。

3. 其实,控件的内容是无限大的,只不过我们在这个平面建立了一个坐标系,确定了坐标原点,确定了显示的起点位置而已。就好比你拿着望远镜(view)在看风景(view的内容),改变的只是你看到的区域(view.bounds),风景的相对位置并没有改变(子控件的frame不会改变),但是给你的感觉是风景在移动(相对改变子控件的显示位置)。

4. 通过修改一个控件的frame.size来修改控件的长宽,长宽向右下增加/减少(不会改变该控件frame.origin)。但是通过修改bounds.size修改控件的长宽,长宽是向四周均匀增加/减少(会改变该控件的frame.origin)。

注意:

1. 在开发中设置一个控件的center时,最好先设置控件的宽高(size.width & size.height),再设置其center。

2. 如果使用frame.size来设置width & height应该先设置width & height再设置center。

 如果使用bounds.size 来设置width & height 设置顺序不影响结果。

3. 设置center时,不管哪种情况,先大小 后位置 不容易出问题。

实例:

理解bounds之后,我们可以模仿UIScrollView,让控制器的View也可以实现滚动功能,并限制其滚动范围。

- (void)viewDidLoad {
[super viewDidLoad];
// 添加拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(offset:)];
[self.view addGestureRecognizer:pan];
} - (void)offset:(UIPanGestureRecognizer *)pan {
CGPoint offset = [pan translationInView:self.view];
CGRect temp = self.view.bounds;
temp.origin.x += - offset.x;
temp.origin.y += - offset.y;
if (temp.origin.y < - || temp.origin.y > || temp.origin.x < - || temp.origin.x > ) {
temp.origin.x = self.view.bounds.origin.x;
temp.origin.y = self.view.bounds.origin.y;
}
self.view.bounds = temp;
// 复位
[pan setTranslation:CGPointZero inView:self.view];
NSLog(@"%f",offset.y);
}

相关链接:关于UIScollView 中的contentOffset 的理解


效果

  

进一步理解 frame 和 bounds的更多相关文章

  1. iOS 中的frame,bounds,center,transform关联

    这里有一篇好文章 http://www.winddisk.com/2012/06/07/transform/ 先看几个知识点,UIView 的frame,bounds,center,transform ...

  2. 深入探究frame和bounds的区别以及setbounds使用

    [转自]http://blog.csdn.net/hherima/article/details/39501857 在iOS开发中经常遇到两个词Frame和bounds,本文主要阐述Frame和bou ...

  3. ios开发之UIView的frame、bounds跟center属性的区别(附图)

    博文暂时想到什么写什么,不顺理成章,不顺章成篇. 先看几个概念 坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y 大小Size:由宽度width和高度height构成,表示一个矩形 ...

  4. iOS View的Frame和bounds之区别,setbounds使用(深入探究)

    前言: 在ios开发中经常遇到两个词Frame和bounds,本文主要阐述Frame和bound的区别,尤其是bound很绕,较难理解. 一.首先,看一下公认的资料: 先看到下面的代码你肯定就明白了一 ...

  5. frame、bounds表示大小和位置的属性以及center、position、anchorPosition

    在iOS开发开发过程中经常会用到界面元素的frame.bounds表示大小和位置的属性以及center.position.anchorPosition等单纯表示位置的属性.这些属性究竟什么含义?彼此间 ...

  6. iOS开发中frame与bounds的区别

    闲话不多说,先上两张图,大伙们就已经明白了: 显示出来的效果是这样子滴:  总结: 要理清这两者的区别,最主要的要理解一下几个概念:frame可以理解为可视的范围,而bounds可以理解为可视范围内的 ...

  7. 深入浅出了解frame和bounds

    frame frame的官方解释如下: The frame rectangle, which describes the view's location and size in its supervi ...

  8. iOS开发-View中frame和bounds区别

    开发中调整View的时候的经常会遇到frame和bounds,刚开始看的时候不是很清楚,不过看了一下官方文档,frame是确定视图在父视图中的位置,和本身的大小,bounds确定可以确定子视图在当前视 ...

  9. ios视图frame和bounds的对比

    bounds坐标:自己定义的坐标系统,setbound指明了本视图左上角在该坐标系统中的坐标,   默认值(0,0) frame坐标:  子视图左上角在父视图坐标系统(bounds坐标系统)中的坐标, ...

随机推荐

  1. 设备信息工具pv-jd快速上手

    pv-jd 这是一个判断设备信息的小工具,可以判断出移动端还是PC端,提供了多种API 快速开始 安装npm install pv-jd -S 示例 import {judgeDevice, judg ...

  2. Highcharts error #14: www.highcharts.com/errors/14

    错误原因:数据类型错误,需要的是Number类型,传入的却是String 以为为官网说明: Highcharts Error #14 String value sent to series.data, ...

  3. 本机浏览器访问不到Linux虚拟机中的nginx开启页面

    1.使用该执行打开端口文件vi /etc/sysconfig/iptables 2.随便复制一行现有内容,将你要打开的端口设置上就行了,这里是打开80端口-A INPUT -m state --sta ...

  4. Ruby系列文章之1---开发者应该熟悉的10个工具

    1. Git Git是进入Ruby这个生态圈首先最应该学会的工具.几乎所有以Ruby开发出来的套件都放在Github上.也就是不管你要下载或修改协作都需要透过Git. 2. RVM Ruby有很多种i ...

  5. 爬虫入门之urllib库(一)

    1 爬虫概述 (1)互联网爬虫 一个程序,根据Url进行爬取网页,获取有用信息 (2)核心任务 爬取网页 解析数据 难点 :爬虫和反爬虫之间的博弈 (3)爬虫语言 php 多进程和多线程支持不好 ja ...

  6. Windows 2012R2远程桌面服务简介

    一.远程桌面服务概述 远程桌面服务加快并扩展了到任何设备的桌面和应用程序部署,在帮助保护关键知识产权的安全的同时提高了工作人员的工作效率,简化了法规遵从性. 远程桌面服务启用虚拟机基础结构 (VDI) ...

  7. 实验:将系统进程映射移到 Python 字典中

    参考官方文档,测试下列代码,把oracle的进程映射到python的字典中: [oracle@ycr python]$ more pro_get.py import reimport subproce ...

  8. 设计模式——装饰模式(Decorator Pattern)

    装饰模式:动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活. UML图: 模型类: Component类: package com.cnblog.clarck; /** ...

  9. BZOJ3262:陌上花开(CDQ分治)

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美 ...

  10. 2018.11.22 mac中"允许所有安装来源"的命令 & Mac窗口标题显示文件的路径

    当Mac遇到软件无法安装或者此文件已经损坏之类的时候 原因是软件为破解版,地址来源已改变,被系统拦截了,解决办法就是直接在终端中输入"sudo spctl --master-disable ...