ios UIScrollView 和 setContentOffset

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5977640/
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 19:53:25  来源:igfitidea点击:

UIScrollView and setContentOffset

iphonecocoa-touchiosuiscrollview

提问by izan

My question is about this method:
(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

我的问题是关于这种方法:
(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

I have read the documentation, but i don't understand what this method is for.

我已阅读文档,但我不明白此方法的用途。

thanks for your answers.

感谢您的回答。

回答by Alex Stanciu

UIScrollView lets you have content that is larger than what you can view on the screen. In the image below you can see a large red rectangle with a green rectangle inside.

UIScrollView 让你拥有比你在屏幕上可以看到的更大的内容。在下图中,您可以看到一个大的红色矩形,里面有一个绿色矩形。

The contentArea property of the UIScrollView defines the logical size of your view (the red rectangle). The visible area of the scroll view is represented by the green rectangle. The contentOffset is the upper left corner of the visible area. Changing contentOffset, the visible area will move around.

UIScrollView 的 contentArea 属性定义了视图的逻辑大小(红色矩形)。滚动视图的可见区域由绿色矩形表示。contentOffset 是可见区域的左上角。改变 contentOffset,可见区域会四处移动。

(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

The method above will move the contentOffset (the upper left corner of the green rectangle) to a specified location, thus moving the visible area (the green rectangle).

上面的方法将 contentOffset(绿色矩形的左上角)移动到指定位置,从而移动可见区域(绿色矩形)。

Hope this helps.

希望这可以帮助。

enter image description here

在此处输入图片说明

回答by Janak Nirmal

As mentioned in documentation.

文档中所述

Scrolling to a Specific Offset

滚动到特定偏移量

Scrolling to a specific top-left location (the contentOffsetproperty) can be accomplished in two ways. The setContentOffset:animated:method scrolls the content to the specified content offset. If the animated parameter is YES, the scrolling will animate from the current position to the specified position at a constant rate. If the animated parameter is NO, the scrolling is immediate and no animation takes place. In both cases, the delegates scrollViewDidScroll:messages. If animation is disabled, or if you set the content offset by setting the contentOffsetproperty directly, the delegate receives a single scrollViewDidScroll:message. If animation is enabled, then the delegate receives a series of scrollViewDidScroll:messages as the animation is in progress. When the animation is complete, the delegate receives a scrollViewDidEndScrollingAnimation:message.

滚动到特定的左上角位置(contentOffset属性)可以通过两种方式完成。该setContentOffset:animated:方法将内容滚动到指定的内容偏移量。如果动画参数为YES,则滚动将以恒定速率从当前位置动画到指定位置。如果动画参数为NO,则立即滚动且不发生动画。在这两种情况下,代表scrollViewDidScroll:消息。如果禁用动画,或者如果您通过contentOffset直接设置属性来设置内容偏移,则委托会收到一条scrollViewDidScroll:消息。如果启用了动画,则代理会scrollViewDidScroll:在动画进行时收到一系列消息。当动画完成时,代理会收到一条scrollViewDidEndScrollingAnimation:消息。

i.e. In simple words if you want to scroll UIScrollViewprogramatically by passing scrolling position values i.e. how much amount to scroll, you can use this method.

即简单来说,如果您想UIScrollView通过传递滚动位置值(即滚动量)以编程方式滚动,则可以使用此方法。

This method also calls delegate scrollViewDidScroll:i.e. delegate method of UIScrollViewclass through which you can maintain the amount of scrolling of UIScrollView.

该方法还调用delegate scrollViewDidScroll:ieUIScrollView类的delegate 方法,通过该方法您可以保持滚动量UIScrollView