如何在UIScrollView中以编程方式强制停止滚动? [英] How can I programmatically force-stop scrolling in a UIScrollView?

查看:533
本文介绍了如何在UIScrollView中以编程方式强制停止滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:给出答案这里对我不起作用。

我有一个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中以编程方式强制停止滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆