問題描述
注意:給出的答案這里不適合我.
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 中的滾動?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!