xcode 如何在特定点停止 UIScrollView?

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

How to stop a UIScrollView at a specific point?

objective-ciosxcodeuiscrollview

提问by Jesse Head

How do you stop a UIScrollViewat specific point? That is, set the final position of the UIScrollViewafter user interaction?

你如何UIScrollView在特定点停止?即设置UIScrollView用户交互后的最终位置?

Specifically, how can you set intervals along a horizontal UIScrollView, so that the it will only stop at these points?

具体来说,如何沿水平方向设置间隔UIScrollView,使其仅在这些点处停止?

I hope what I have said is clear enough.

我希望我说的足够清楚。

回答by rob mayoff

Take a look at method scrollViewWillEndDragging:withVelocity:targetContentOffset:of UIScrollViewDelegate. It's intended to do exactly what you need.

看一看方法scrollViewWillEndDragging:withVelocity:targetContentOffset:UIScrollViewDelegate。它旨在完全满足您的需求。

The scroll view sends this message to its delegate when the user finishes dragging the scroll view. The third parameter (targetContentOffset) is passed as a pointer, and you can change its value to change where the scroll view will stop.

当用户完成拖动滚动视图时,滚动视图将此消息发送给其代理。第三个参数 ( targetContentOffset) 作为指针传递,您可以更改其值以更改滚动视图将停止的位置。

iOS 5.0 and later.

iOS 5.0 及更高版本。

回答by basvk

You've got 3 options:

您有 3 个选择:

  1. You can do so using pagingEnabled.

  2. Use setContentOffset: animated:in the UIScrollViewDelegatescrollViewDidEndDecelerating:method

  3. Create your own scroll view using touchesBegan, touchesMovedand touchesEndedand extrapolate to create momentum with a appropriate endpoint
  1. 您可以使用pagingEnabled.

  2. setContentOffset: animated:UIScrollViewDelegatescrollViewDidEndDecelerating:方法中使用

  3. 创建您自己的滚动视图touchesBegantouchesMoved并使用touchesEnded和推断以使用适当的端点创建动量

回答by Yoko

I've found something that works for me. It let's you scroll to point 0,0 but no further:

我找到了对我有用的东西。它让您滚动到 0,0 点,但不能再进一步:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x <= -1) {
        [scrollView setScrollEnabled:NO];
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [scrollView setScrollEnabled:YES];
    }
}

You could do the same for top, bottom or right (x or y)

您可以对顶部、底部或右侧(x 或 y)执行相同操作

回答by Victor Sigler

For those we need to set the targetContentOffsetin Swift like rob mayoffsays in his excellent answer above , this is the way :

对于那些我们需要targetContentOffsetrob mayoff在上面的出色回答中所说的那样在 Swift 中设置的人,方法如下:

func scrollViewWillEndDragging(scrollView: UIScrollView!, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    targetContentOffset.memory.y = x 
}

回答by samwize

This will work just fine:

这将工作得很好:

override func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y > CERTAIN_POINT {
        scrollView.scrollEnabled = false
        scrollView.scrollEnabled = true
    }
}

回答by vishy

to achieve this u need to enable the paging of the scrollview

要实现这一点,您需要启用滚动视图的分页

For example:

例如:

[scrollView setContentSize:CGSizeMake(640, 161)];
[scrollView setPagingEnabled:YES];

here, width of scrollview is 640 (twice of width of iphone screen), if paging is enabled it will divide it equally depending on the width. So as we scroll to this position it will stop at the particular offset.

这里,scrollview 的宽度为 640(iphone 屏幕宽度的两倍),如果启用分页,它将根据宽度平均划分。所以当我们滚动到这个位置时,它会停在特定的偏移处。

回答by Tendulkar

[scrollView setContentSize:CGSizeMake(640, 161)];

回答by Ballu

i think this may help you check this API provided by apple

我认为这可以帮助您检查苹果提供的这个 API

  • (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
  • (void)scrollRectToVisible:(CGRect)rect 动画:(BOOL)动画

And apple clearly said about this This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing.

并且苹果清楚地说明了这个方法滚动内容视图,以便 rect 定义的区域在滚动视图内部可见。如果该区域已经可见,则该方法不执行任何操作。

回答by user3327570

For vertical scrolling i use this . Worked perfectly

对于垂直滚动,我使用这个。完美运行

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.scrollView.contentOffset = CGPointMake(0, desiredPoint.y); }
  • (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.scrollView.contentOffset = CGPointMake(0, desiredPoint.y); }