UIScrollView 和 setContentOffset
我的问题是关于这个方法:(void)setContentOffset:(CGPoint)contentOffsetanimated:(BOOL)animated
我已阅读文档,但我不明白此方法的用途。
感谢您的回答。
My question is about this method:(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIScrollView 允许您拥有比屏幕上可以查看的内容更大的内容。
在下图中,您可以看到一个大的红色矩形,里面有一个绿色矩形。
UIScrollView 的 contentArea 属性定义视图的逻辑大小(红色矩形)。
滚动视图的可见区域由绿色矩形表示。 contentOffset 是可见区域的左上角。改变contentOffset,可见区域将会移动。
上面的方法会将 contentOffset(绿色矩形的左上角)移动到指定位置,从而移动可见区域(绿色矩形)。
希望这有帮助。
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.
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.
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).
Hope this helps.
如文档中所述。
滚动到特定偏移量
滚动到特定左上角位置(
contentOffset
属性)可以通过两种方式完成。setContentOffset:animated:
方法将内容滚动到指定的内容偏移量。如果动画参数为YES
,则滚动将以恒定速率从当前位置动画到指定位置。如果动画参数为NO
,则立即滚动并且不发生动画。在这两种情况下,委托scrollViewDidScroll:
消息。如果禁用动画,或者通过直接设置contentOffset
属性来设置内容偏移量,则委托会收到一条scrollViewDidScroll:
消息。如果启用了动画,则当动画正在进行时,委托会收到一系列scrollViewDidScroll:
消息。动画完成后,委托会收到scrollViewDidEndScrollingAnimation:
消息。即简单来说,如果您想通过传递滚动位置值(即滚动量)以编程方式滚动
UIScrollView
,您可以使用此方法。该方法还调用委托
scrollViewDidScroll:
,即UIScrollView
类的委托方法,通过该方法可以维护UIScrollView
的滚动量。As mentioned in documentation.
Scrolling to a Specific Offset
Scrolling to a specific top-left location (the
contentOffset
property) can be accomplished in two ways. ThesetContentOffset:animated:
method scrolls the content to the specified content offset. If the animated parameter isYES
, the scrolling will animate from the current position to the specified position at a constant rate. If the animated parameter isNO
, the scrolling is immediate and no animation takes place. In both cases, the delegatesscrollViewDidScroll:
messages. If animation is disabled, or if you set the content offset by setting thecontentOffset
property directly, the delegate receives a singlescrollViewDidScroll:
message. If animation is enabled, then the delegate receives a series ofscrollViewDidScroll:
messages as the animation is in progress. When the animation is complete, the delegate receives ascrollViewDidEndScrollingAnimation:
message.i.e. In simple words if you want to scroll
UIScrollView
programatically by passing scrolling position values i.e. how much amount to scroll, you can use this method.This method also calls delegate
scrollViewDidScroll:
i.e. delegate method ofUIScrollView
class through which you can maintain the amount of scrolling ofUIScrollView
.