UIView的frame,bounds,center,origin,什么时候用什么? [英] UIView's frame, bounds, center, origin, when to use what?

查看:25
本文介绍了UIView的frame,bounds,center,origin,什么时候用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIView 具有属性 frameboundscenterorigin,而且它们似乎都是相互关联的.大多数时候,我在设置 UIView 的位置和大小时会处理 frame.我知道 frame 使用全局坐标系而 bounds 使用本地视图的坐标(因此它的 x 和 y 为 0,但并非总是如此),但它仍然令人困惑什么时候用什么.

UIView has the properties frame, bounds, center, and origin, and they all seem to be interrelated. Most of the time, I deal with frame when setting the position and size of a UIView. I understand that frame is using global coordinate system and bounds is using coordinate of the local view (therefore its x and y are 0, but not always), but it's still confusing to me when to use what.

其他属性(boundscenterorigin)应该在什么上下文(以及合适的时间)下使用?>

Under what context (and what's the right time) the other properties (bounds, center, origin) should be used?

推荐答案

Marco 上面的回答是正确的,但只是扩展了在什么上下文中"的问题...

Marco's answer above is correct, but just to expand on the question of "under what context"...

frame - 这是您最常用于普通 iPhone 应用程序的属性.大多数控件将相对于包含"控件进行布局,因此 frame.origin 将直接对应控件需要显示的位置,而 frame.size 将确定控件的大小.

frame - this is the property you most often use for normal iPhone applications. most controls will be laid out relative to the "containing" control so the frame.origin will directly correspond to where the control needs to display, and frame.size will determine how big to make the control.

center - 对于可能发生移动或缩放的基于精灵的游戏和动画,您可能会关注此属性.默认动画和旋转将基于 UIView 的中心.通过 frame 属性尝试和管理此类对象很少有意义.

center - this is the property you will likely focus on for sprite based games and animations where movement or scaling may occur. By default animation and rotation will be based on the center of the UIView. It rarely makes sense to try and manage such objects by the frame property.

bounds - 这个属性不是定位属性,而是定义了 UIView 相对于框架的可绘制区域.默认情况下,此属性通常为 (0, 0, width, height).更改此属性将允许您在框架外绘图或将绘图限制在框架内的较小区域.可以在下面的链接中找到对此的很好的讨论.除非特别需要调整绘图区域,否则很少会操纵此属性.唯一的例外是大多数程序将在启动时使用 [[UIScreen mainScreen] bounds] 来确定应用程序的可见区域并相应地设置它们的初始 UIView 框架.

bounds - this property is not a positioning property, but defines the drawable area of the UIView "relative" to the frame. By default this property is usually (0, 0, width, height). Changing this property will allow you to draw outside of the frame or restrict drawing to a smaller area within the frame. A good discussion of this can be found at the link below. It is uncommon for this property to be manipulated unless there is specific need to adjust the drawing region. The only exception is that most programs will use the [[UIScreen mainScreen] bounds] on startup to determine the visible area for the application and setup their initial UIView's frame accordingly.

为什么存在UIView 中的框架矩形和边界矩形?

希望这有助于澄清每个属性可能被使用的情况.

Hopefully this helps clarify the circumstances where each property might get used.

这篇关于UIView的frame,bounds,center,origin,什么时候用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆