美文网首页常看锻炼吃饭的家伙
UIScrollView滑动停止监听

UIScrollView滑动停止监听

作者: zgsddzwj | 来源:发表于2020-06-06 10:55 被阅读0次

    如何监测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

    相关文章

      网友评论

        本文标题:UIScrollView滑动停止监听

        本文链接:https://www.haomeiwen.com/subject/fdugzhtx.html