iphone - How to detect when a UIScrollView has finished scrolling - Stack Overflow
根据这个链接,其实早在09年就有人在SO上问这个问题了,其中一个解决方案至今还很有效,以至于该答案的评论者大呼难以置信。
解决方法是这样的:
-(void)scrollViewDidScroll:(UIScrollView *)sender
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
//ensure that the end of scroll is fired.
[self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:sender afterDelay:0.3];
//NSLog(@"滑动中");
_isScrolling = YES ;
...
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
_isScrolling = NO ;
NSLog(@"滑动停止 = page%@",@(self.currentPage) );
...
}
这段代码应该是来源于:facebookarchive/three20: Three20 is an Objective-C library for iPhone developers
原理:在- (void)scrollViewDidScroll:(UIScrollView *)scrollView内,创建一个异步调用,等待0.3秒后调scrollViewDidEndScrollingAnimation。由于scrollViewDidScroll会不断被调用,再次触发时取消上一次的异步请求。等到不再滚动时,最后一次的请求不会被取消,最终会跑到scrollViewDidScroll,然后,添加想要在滚动停止时调用的代码即可。
网友评论