当前位置: 首页 > news >正文

iOS——frame和bounds的区别

把frame理解为占用区域,把bounds理解为边界。View在旋转过程中,其实自己的坐标系统并没有发生改变,bounds中的origin只能通过setBounds方法修改。

frame 定义了视图在其父视图坐标系统中的位置和大小。其坐标系是相对于俯视图的坐标系。
bounds 定义了视图自身坐标系统中的位置和大小。其坐标系是相对于自己本身视图的坐标系。

UIView.h中的注释:

// 如果视图进行了变换,不要使用 frame,因为它不会正确反映视图的实际位置。使用 bounds + center 代替。
@property(nonatomic) CGRect frame;// 如果非恒等变换,请使用 bounds/center 而不是 frame。
@property(nonatomic) CGRect bounds;      // 默认 bounds 是原点为零,大小为 frame 的大小。
@property(nonatomic) CGPoint center;      // center 是 frame 的中心,相对于 anchorPoint。
  • bounds的x,y是根据自己的坐标系统而言的。没错,每个view都有自己的坐标系。以自己左上角点为坐标原点。所以bounds的x,y默认为(0,0),除非调用setBounds方法;
  • frame的size不一定等于bounds的size,在旋转后它们的size就不一样了。

有如下示例:


- (void)viewDidLoad {[super viewDidLoad];UIButton *animateButton = [UIButton buttonWithType:UIButtonTypeSystem];animateButton.frame = CGRectMake(100, 250, 100, 50);[animateButton setTitle:@"Animate" forState:UIControlStateNormal];[animateButton addTarget:self action:@selector(startAnimation) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:animateButton];self.fView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];[self.view addSubview:self.fView];self.fView.backgroundColor = [UIColor orangeColor];self.sView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];[self.fView addSubview:self.sView];self.sView.backgroundColor = [UIColor blueColor];NSLog(@"Bounds:");NSLog(@"fView x:%f; sView x:%f", self.fView.bounds.origin.x, self.sView.bounds.origin.x);NSLog(@"fView y:%f; sView y:%f", self.fView.bounds.origin.y, self.sView.bounds.origin.y);NSLog(@"frame:");NSLog(@"fView x:%f; sView x:%f", self.fView.frame.origin.x, self.sView.frame.origin.x);NSLog(@"fView y:%f; sView y:%f", self.fView.frame.origin.y, self.sView.frame.origin.y);
}- (void) startAnimation {[self.fView setBounds:CGRectMake(60, 60, 100, 100)];NSLog(@"Bounds:");NSLog(@"fView x:%f; sView x:%f", self.fView.bounds.origin.x, self.sView.bounds.origin.x);NSLog(@"fView y:%f; sView y:%f", self.fView.bounds.origin.y, self.sView.bounds.origin.y);NSLog(@"frame:");NSLog(@"fView x:%f; sView x:%f", self.fView.frame.origin.x, self.sView.frame.origin.x);NSLog(@"fView y:%f; sView y:%f", self.fView.frame.origin.y, self.sView.frame.origin.y);
}

点击按钮我们发现,明明更改的是fView的bounds,为什么fView的位置没变,但是sView的位置向左上了呢?
其实setBounds中的(x,y)只改变自己的坐标系统,子View的bounds和frame并不会改变;
setBounds是修改自己坐标系的原点位置,进而影响到子View的显示位置;
bounds改变位置时,改变的是子视图的位置,自身没有影响,其实就是改变了自身的坐标系原点,默认原点在左上角。

因此执行完setBounds后,实际上是将当前fView的视图的原点移到了相对于原来原点的(60,60)位置,但是由于fView设定的frame是(100,100),因此它本身的frame不变,只是相当于移动了坐标系往左上了。又因为sView是fView的子视图,sView的frame是相对于fView的坐标系来设定位置的,因此,sView还是处于相对于fView的(0,0)位置,因此我们看上去sView的位置就像是随着fView的坐标系一起向左上移动了,但实际上sView的frame没有改变,打印出来还是(0,0)。

总结

frame: 描述视图在其父视图中的位置和大小,是“绝对”的。
bounds: 描述视图自身的内容区域和尺寸,是“相对”的。

http://www.lryc.cn/news/431093.html

相关文章:

  • Trm理论 3(注意力机制)
  • Vue2和Vue3项目创建的区别和 element ui 和element plus的导入方式
  • 基于STM32的猫狗宠物喂养系统设计(微信小程序)(215)
  • spark读取csv文件
  • 钢铁百科:Q420DR力学性能、Q420DR执行标准、Q420DR低温容器钢板
  • 三菱机器人手柄维修示教器维修手操器面板等
  • 中间件的学习理解总结
  • 编程秘密武器:提升工作效率的关键工具
  • Git+word记笔记
  • java-antrl手敲命令的hello world
  • 法规探讨 | 《医疗器械管理法(草案征求意见稿)》初探(1)
  • 大语言模型的上下文窗口(Context Windows):对人工智能应用的影响
  • Java【数组】
  • xAI巨无霸超级计算机上线:10万张H100 GPU,计划翻倍至20万张
  • python集合
  • 算法打卡 Day29(回溯算法)-复原 IP 地址 + 子集 + 子集 Ⅱ
  • LeetCode 热题100-17 缺失的第一个正数
  • 基于CloudflareSpeedTest项目实现git clone加速
  • 对与单纯post方法写项目的修改成baseservlet方法
  • 北京地铁换乘站人流量监控与图像识别技术优化
  • Day16_0.1基础学习MATLAB学习小技巧总结(16)——元胞数组
  • C#自定义控件的放置与拖动
  • python circular import python循环导入问题
  • kafka集群安装
  • SQL通用语法、SQL分类以及DDL
  • 静态链接和动态链接
  • 构建智能门禁安防系统:树莓派 4B、OpenCV、SQLite 和 MQTT 的应用(代码示例)
  • 基于 Konva 实现Web PPT 编辑器(二)
  • 【开源免费】基于SpringBoot+Vue.JS在线竞拍系统(JAVA毕业设计)
  • Qt TabWidget添加多个窗口,实现分页窗体布局