如何監(jiān)測(cè)UITableView等UIScrollView滑動(dòng)停止呢?
有兩種方式:
第一種:
-(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 {
//在這里寫(xiě)監(jiān)聽(tīng)滑動(dòng)停止要做的事
}
第二種:
-(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];
//在這里寫(xiě)監(jiān)聽(tīng)滑動(dòng)停止要做的事
}
參考:
Hook系統(tǒng)代理方法缸沃,添加UIScrollView滾動(dòng)停止回調(diào)
iphone - How to detect when a UIScrollView has finished scrolling - Stack Overflow