UIScrollView滑动停止监听
2020-06-06 本文已影响0人
zgsddzwj
如何监测UITableView等UIScrollView滑动停止呢?
有两种方式:
第一种:
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (dragToDragStop) {
[self scrollViewDidEndScroll];
}
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (scrollToScrollStop) {
[self scrollViewDidEndScroll];
}
}
- (void)scrollViewDidEndScroll {
//在这里写监听滑动停止要做的事
}
第二种:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([self.mainDelegate respondsToSelector:@selector(mainScrollViewDidScroll:)]) {
[self.mainDelegate mainScrollViewDidScroll:scrollView];
}
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.1];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
//在这里写监听滑动停止要做的事
}
参考:
Hook系统代理方法,添加UIScrollView滚动停止回调
iphone - How to detect when a UIScrollView has finished scrolling - Stack Overflow