简体   繁体   English

UIScrollView和setContentOffset

[英]UIScrollView and setContentOffset

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. 谢谢你的回答。

UIScrollView lets you have content that is larger than what you can view on the screen. UIScrollView允许您拥有的内容大于您在屏幕上可以查看的内容。 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). UIScrollView的contentArea属性定义视图的逻辑大小(红色矩形)。 The visible area of the scroll view is represented by the green rectangle. 滚动视图的可见区域由绿色矩形表示。 The contentOffset is the upper left corner of the visible area. contentOffset是可见区域的左上角。 Changing contentOffset, the visible area will move around. 更改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. 希望这可以帮助。

在此输入图像描述

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. 滚动到特定的左上角位置( contentOffset属性)可以通过两种方式完成。 The setContentOffset:animated: method scrolls the content to the specified content offset. setContentOffset:animated:方法将内容滚动到指定的内容偏移量。 If the animated parameter is YES , the scrolling will animate from the current position to the specified position at a constant rate. 如果动画参数为YES ,则滚动将以恒定速率从当前位置动画到指定位置。 If the animated parameter is NO , the scrolling is immediate and no animation takes place. 如果动画参数为NO ,则立即滚动并且不进行动画。 In both cases, the delegates scrollViewDidScroll: messages. 在这两种情况下,委托scrollViewDidScroll: messages。 If animation is disabled, or if you set the content offset by setting the contentOffset property directly, the delegate receives a single scrollViewDidScroll: message. 如果禁用动画,或者通过直接设置contentOffset属性设置内容偏移量,则委托会收到单个scrollViewDidScroll:消息。 If animation is enabled, then the delegate receives a series of scrollViewDidScroll: messages as the animation is in progress. 如果启用了动画,则委托会在动画正在进行时收到一系列scrollViewDidScroll:消息。 When the animation is complete, the delegate receives a scrollViewDidEndScrollingAnimation: message. 动画完成后,委托会收到scrollViewDidEndScrollingAnimation:消息。

ie In simple words if you want to scroll UIScrollView programatically by passing scrolling position values ie how much amount to scroll, you can use this method. 即简单来说,如果你想通过传递滚动位置值(即滚动量多少)以编程方式滚动UIScrollView ,你可以使用这种方法。

This method also calls delegate scrollViewDidScroll: ie delegate method of UIScrollView class through which you can maintain the amount of scrolling of UIScrollView . 此方法还调用委托scrollViewDidScroll:UIScrollView类的委托方法,通过该方法可以维护UIScrollView的滚动量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM