UIScrollView 如何改变框架而不是边界?UIScrollView 是如何工作的? [英] How does UIScrollView change the frame, but not the bounds? How does UIScrollView work?

查看:23
本文介绍了UIScrollView 如何改变框架而不是边界?UIScrollView 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解框架和边界之间的区别,我认为边界是视图的本地坐标系,而框架是超级视图.

I understand the difference between frames and bounds I think, bounds are to the view's local coordinate system, while frames are to the superviews.

然而,对于滚动视图,我有点困惑.

With scrollviews however, I'm a little confused.

当我在 UIScrollView 中有一个 UIImageView 并捏住以放大它时,似乎框架变大(宽度和高度)也会增加相应 UIImage 的宽度和高度.

When I have a UIImageView in a UIScrollView and pinch to zoom it larger, it seems the frame grows larger (width and height) increases the width and height of the corresponding UIImage as well.

然而,UIImageView 的边界似乎根本没有改变.

However, it seems like the bounds of the UIImageView don't change at all.

这是为什么?滚动视图如何在所有这些方面工作?我已经阅读了多个问题并检查了文档,但我无法解释这个解释.

Why is this? How do scroll views work in regards to all of this? I've read multiple questions and checked the documentation but this explanation is eluding me.

放大 UIScrollView 会如何影响其内容?它是否只是改变了缩放视图的框架而不是边界?

How does zooming in a UIScrollView affect its contents? Does it just change the frame of the zooming view but somehow not the bounds?

推荐答案

查看说明 in我的书:

滚动视图通过对可缩放视图应用缩放变换来缩放;因此可缩放视图的框架也被缩放.此外,滚动视图关注使滚动继续正常工作:用户滚动时的限制应该继续与内容的限制相匹配,并且像 scrollRectToVisible:animated: 这样的命令应该继续以相同的方式对相同的值工作.因此,滚动视图会自动缩放自己的 contentSize 以匹配当前的 zoomScale.(您实际上可以通过覆盖 UIScrollView 子类中的 setContentSize 来检测这种情况:您可以看到滚动视图在缩放时调整自己的内容大小.)

The scroll view zooms by applying a scaling transform to the scalable view; therefore the frame of the scalable view is scaled as well. Moreover, the scroll view is concerned to make scrolling continue to work correctly: the limits as the user scrolls should continue to match the limits of the content, and commands like scrollRectToVisible:animated: should continue to work the same way for the same values. Therefore, the scroll view automatically scales its own contentSize to match the current zoomScale. (You can actually detect this happening by overriding setContentSize in a UIScrollView subclass: you can see the scroll view adjusting its own content size as you zoom.)

基本上(尽管从上面的引用中不太清楚)我们与可伸缩视图的框架无关 - 或任何视图 - 应用了非身份转换它,正是这里的情况.这个事实在 UIView 上的 Apple 文档中非常清楚.因此,您不应该查看框架 - 只查看转换.您正在阅读的框架纯粹是作为变换变化的副作用而变化的.

Basically (though that is not quite clear from the above quotation) we have no business concerning ourselves with the frame of the scalable view - or any view - that has a non-identity transform applied to it, which is exactly the case here. That fact is made very clear by the Apple documentation on UIView. Thus you should not be looking at the frame - only the transform. The frame value that your are reading changes purely as a side-effect of the transform change.

至于可伸缩视图的边界——显然边界不会改变;这就是转换如何工作的重点.它保持一个恒定的 centerbounds,这样子视图和绘图继续在参考坐标系内正确运行,尽管有变换.我的书也让您了解对此的理解.

As for the bounds of the scalable view - obviously the bounds do not change; that is the whole point of how a transform works. It maintains a constant center and bounds, so that subviews and drawing continue to operate correctly within the frame-of-reference coordinates despite the transform. My book talks you through an understanding of this as well.

另一方面,滚动视图自己的边界当然可以改变它们的原点,尤其是因为这与滚动视图滚动的含义完全相同.滚动边界原点的改变,简单明了.这在缩放过程中并不奇怪,因为正如我刚才所说,内容大小发生了变化,因此内容可能会重新定位,以便在缩放时保持连贯一致.

On the other hand, the scroll view's own bounds can certainly change their origin, not least because that is exactly and identically what it means for a scroll view to scroll. Scrolling is a change of bounds origin, plain and simple. And this would not be surprising during zooming, because, as I just said, the content size has changed, so the content may be repositioned in order to keep displaying it coherently as you zoom.

这篇关于UIScrollView 如何改变框架而不是边界?UIScrollView 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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