How can I programmatically force-stop scrolling in a 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 中的滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式强制停止 UIScrollView 中的滚动?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01