UIView的框架,范围,中心,原点,什么时候使用什么? [英] UIView's frame, bounds, center, origin, when to use what?

查看:61
本文介绍了UIView的框架,范围,中心,原点,什么时候使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"...

框架-这是您通常用于普通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.

中心-对于可能会发生移动或缩放的基于Sprite的游戏和动画,这可能是您关注的属性.默认情况下,动画和旋转将基于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.

边界-此属性不是定位属性,而是定义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的框架,范围,中心,原点,什么时候使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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