如何以编程方式强制停止 UIScrollView 中的滚动?

How can I programmatically force-stop scrolling in a UIScrollView?(如何以编程方式强制停止 UIScrollView 中的滚动?)
本文介绍了如何以编程方式强制停止 UIScrollView 中的滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

注意:给出的答案这里不适合我.

Note: The answer given here doesn't work for me.

我有一个 UIScrollView(不是表格视图,只是一个自定义的东西),当用户执行某些操作时,我想终止视图内的任何滚动(拖动或减速).我试过做例如这个:

I have a UIScrollView (not a table view, just a custom thing), and when the user takes certain actions, I want to kill any scrolling (dragging or deceleration) inside the view. I've tried doing e.g. this:

[scrollView scrollRectToVisible:CGRectInset([scrollView bounds], 10, 10) animated:NO];

理论上,给定一个已知可见的矩形,滚动将停止在它所在的位置,但事实证明这没有任何效果 - 显然滚动视图看到给定的矩形在界限并且不采取任何行动.我可以让滚动停止,如果我给出一个绝对当前可见边界之外的矩形,但在视图的 contentSize 之内.这似乎按预期停止了视图......但也导致它跳转到其他位置.我可能可以在边缘进行一些操作以使其正常工作,但是有没有人知道一种干净的方法来停止正在执行它的滚动视图?

on the theory that, given a rect that's already known visible, the scrolling will just stop where it is, but it turns out that this doesn't have any effect-- apparently the scroll view sees that the given rect is in bounds and takes no action. I can get the scroll to stop, if I give a rect that is definitely outside the currently-visible bounds, but inside the contentSize of the view. This seems to halt the view as expected... but also causes it to jump to some other location. I could probably do a little playing around at the margins to get this to work reasonably OK, but does anyone know of a clean way to halt a scroll view that's doing its thing?

谢谢.

推荐答案

我玩了一下你原来的解决方案,这似乎工作得很好.我认为您几乎拥有它,但是您只是抵消了您使用过多的矩形,而忘记了您可以将矩形直接滚动回原始矩形.

I played with your original solution a bit, and this seems to work just fine. I think you almost had it, but you were just offsetting the rect that you used too much, and forgot that you could just scroll the rect straight back to the original rect.

任何滚动动作的通用解决方案是这样的:

The generalized solution for any scrolling action is this:

- (void)killScroll 
{
    CGPoint offset = scrollView.contentOffset;
    offset.x -= 1.0;
    offset.y -= 1.0;
    [scrollView setContentOffset:offset animated:NO];
    offset.x += 1.0;
    offset.y += 1.0;
    [scrollView setContentOffset:offset animated:NO];
}

从 iOS 4.3(可能更早)开始,这似乎也可以工作

As of iOS 4.3 (and possibly earlier) this also appears to work

- (void)killScroll 
{
    CGPoint offset = scrollView.contentOffset;
    [scrollView setContentOffset:offset animated:NO];
}

这篇关于如何以编程方式强制停止 UIScrollView 中的滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
How to Cancel Scrolling in UIScrollView(如何在 UIScrollView 中取消滚动)