ios 为什么在 UIView 中有一个框架矩形和一个边界矩形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/749558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:49:17  来源:igfitidea点击:

Why is there an frame rectangle and an bounds rectangle in an UIView?

iosiphone

提问by Thanks

Well although it's late in the dark night, I don't get it why there are two different rectangles: frame and bounds.

好吧,虽然已经是深夜了,但我不明白为什么有两个不同的矩形:框架和边界。

Like I understand it, one single rectangle would have been just enough to do everything. Positioning the View itself relative to another coordinate system, and then clipping it's content to a specified size. What else would you do with two rectangles? And how do they interact with each other?

就像我理解的那样,一个矩形就足以完成所有事情。相对于另一个坐标系定位 View 本身,然后将其内容裁剪到指定的大小。你还能用两个矩形做什么?以及它们之间是如何交互的?

Does anyone have a good explanation? The one from the Apple docs with the kid holding the fruit is not very good for understanding.

有没有人有好的解释?苹果文档中孩子拿着水果的那个不太好理解。

回答by amattn

Here's the cheatsheet:

这是备忘单:

  • frameis where the view is(with respect to the superview)
  • boundsis where the view is allowed to draw(with respect to itself)
  • frame是视图所在的位置(相对于父视图)
  • bounds允许视图绘制的地方(相对于自身)

Some more clarification:

还有一些澄清:

If you are positioning the view in its superview, you almost always change the frame origin.

如果您将视图定位在其超级视图中,您几乎总是会更改框架原点。

If you are clipping where the UIView is drawing, you almost always modify its bounds.

如果您在 UIView 正在绘制的位置进行裁剪,您几乎总是会修改其边界。

Note that you are allowed to have bounds that is bigger than the frame. That is, you can draw "outside the lines" of where you are.

请注意,您可以拥有比框架更大的边界。也就是说,您可以绘制您所在位置的“线外”。

回答by Travis Jensen

Frame is in the superview's coordinate system, bounds is in the view's coordinate system. From my perspective, it is a convenience to have both. Frame seems to be the more useful of the two, unless there is some case I am unaware of where a subview can have a completely different coordinate system (e.g. pixels scaled differently) than the superview.

框架在父视图的坐标系中,边界在视图的坐标系中。从我的角度来看,两者兼而有之是很方便的。Frame 似乎是两者中更有用的一个,除非在某些情况下我不知道子视图在哪里可以具有与超级视图完全不同的坐标系(例如像素缩放不同)。

回答by drewww

I've been having troubles with bounds lately and have done some experimentation. The bounds property does limit where a UIView can draw, but does notlimit its subviews. The other thing bounds controls is touch event dispatching. A view will not, as far a I can tell, receive touch events that are outside its bounds. Furthermore, any subview that outside of the parent view's bounds will also not receive touch events. In these situations, you have to pretty meticulously update the bounds of the container view as the size and position of its subviews change. Everything will always draw fine (because subviews aren't clipped by the bounds of their parent) but touches won't be received.

我最近遇到了边界问题,并做了一些实验。bounds 属性确实限制了 UIView 可以绘制的位置,但限制其子视图。边界控件的另一件事是触摸事件调度。据我所知,视图不会接收超出其边界的触摸事件。此外,任何在父视图边界之外的子视图也不会接收触摸事件。在这些情况下,当容器视图的大小和位置发生变化时,您必须非常小心地更新容器视图的边界。一切都会很好地绘制(因为子视图不会被其父视图的边界剪裁),但不会收到触摸。

(This really should be a reply to an earlier post, but since I can't reply yet, it's stuck here...)

(这确实应该是对之前帖子的回复,但是我现在还不能回复,就卡在这里了……)