如何在 iOS 中结束拖动事件之前检测滚动更新?

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

我想在我的 iOS 应用程序的某些视图中添加更新手势
通常这个手势是在顶部滚动并保持一小段时间

如果用户确实按住滚动视图超过 1 秒,我可以让手势处理程序正常工作:

- (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;
    }
}

它工作得很好,但是当用户在顶部滚动视图时需要关于保持的提示

如何检测这种滚动? scrollViewWillBeginDragging: 事件没有滚动参数,因为 scrollViewWillEndDragging:withVelocity:targetContentOffset:

或者可能存在另一种实现方法?

尝试使用 scrollViewDidScroll(_:)https://developer.apple.com/reference/uikit/uiscrollviewdelegate/1619392-scrollviewdidscroll

请注意,您可以使用 UIScrollView.contentOffset.y < 0 检查它是否已滚动到顶部。

1) You can use refreshcontrol and customize it the way you want. Would recommend using this as its core api and gives you fun animations as well. plus this is generic and used everywhere so user would already know how to use compare to your scroll and hold-for-a-second gesture.

-(void)viewDidLoad {
...
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[self.myTableView addSubview:refreshControl];
...
}

-(void)handleRefresh:(id)sender { // do your refresh here... }

您可以将 KVO 用于 contentffset 参数

这是我的 Github 存储库之一

UIScrollView-XYPlusingRefresh

想要xiaoyulevin@gmail.com

联系我