Search code examples
iosobjective-cgestures

How can I detect scrolling for update before end dragging event in iOS?


I want to add update gesture in some views in my iOS application
Usually this gesture is scroll above top and hold for some small time

I could make just gesture handler that works if user did hold scrolled view more than 1 second:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (scrollView == _tableView) {
        _scrollViewDragStarted = [NSDate date];
    }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if (scrollView == _tableView) {
        if (velocity.y < 0 && targetContentOffset->y < 0) {
            NSTimeInterval hold = [[NSDate date] timeIntervalSinceDate:_scrollViewDragStarted];
            if (hold > 1) {
                // refreshing method calls here
            }
        }
        _scrollViewDragStarted = nil;
    }
}

It works perfectly but hint about holding is needed when user scrolled view above top

How can I detect such scrolling? scrollViewWillBeginDragging: event has no scroll parameters as scrollViewWillEndDragging:withVelocity:targetContentOffset:

Or maybe exist another way to implement this?


Solution

  • Try using scrollViewDidScroll(_:). https://developer.apple.com/reference/uikit/uiscrollviewdelegate/1619392-scrollviewdidscroll

    Note that you can use UIScrollView.contentOffset.y < 0 to check whether it has scrolled above the top.