首页 > 解决方案 > 如何使滚动视图在拖动时停止移动并为已经触摸的手指立即恢复移动?

问题描述

如何使滚动视图在拖动时停止移动,之后,当手指已经触摸滚动视图时,如何使其立即开始滚动?

self.scrollView.notMovingWhileDragging()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
    self.scrollView.resumeMovingForTheAlreadyTouchingFinger()
}

对于上述两个函数,我设置isScrollEnabledfalsethen true,但是如果调用上述回调时手指已经触摸到滚动视图,则滚动视图不会移动。

我想要的结果就像 iOS 地图应用程序一样,当工作表位于顶部时,滚动视图被拖离顶部一点距离。现在我开始向下拖动滚动视图,滚动视图向下移动,当滚动视图滚动到顶部时,然后继续向下拖动,工作表向下(拖动时滚动视图停止移动),然后向上拖动,工作表视图向上,然后当工作表视图位于顶部时,继续向上拖动,滚动视图再次开始滚动(当手指已经在滚动视图上时,滚动视图恢复移动)。

标签: iosuiscrollviewtouchgestureuipangesturerecognizer

解决方案


这是不可能的。a的isScrollEnabled属性UIScrollView只是更新了's的isEnabled属性。有一个定义的生命周期,从着陆开始。并且它的状态变为,因此在触摸已经开始时启用它不会导致它触发。UIScrollViewpanGestureUIPanGesturetouchesBegan.began

在您最初的问题中,您询问了如何在处理 Apple Maps 的工作表样式界面的定位后允许滚动。这可以通过在滚动视图中添加目标来完成panGesture。例如,您可以像这样添加目标tableView.panGestureRecognizer.addTarget(self, action: #selector(handlePan(_:)));然后在你的行动中你可以说:

@objc func handlePan(_ sender: UIPanGestureRecognizer) {
    let velocity = sender.velocity(in: view)
    let translation = sender.translation(in: view)
    switch  sender.state {
    case .began: break
    case .changed:
        // shouldMoveSheet can check to see if the sheet should move or scroll view should scroll based on content offset, pan translation, etc.
        if shouldMoveSheet(for: translation) {
            bottomConstraint.constant = newConstant
            // while moving sheet set the content offset to the top so the scroll view does not scroll
            tableView.setContentOffset(CGPoint(x: 0.0, y: tableView.adjustedContentInset.top), animated: false)
            sender.setTranslation(.zero, in: view)
        }

    case .ended:
        // Depending on if the sheet has been moved when the gesture ends move the sheet to the up or down position
        if velocity.y < 0 && tableView.contentOffset.y <= tableView.adjustedContentInset.top {
            bottomConstraint.constant = constantForSheetUp
        } else if tableView.contentOffset.y <= tableView.adjustedContentInset.top {
            bottomConstraint.constant = constantForSheetDown
        }
        UIView.animate(duration: 0.3) {
            view.layoutIfNeeded()
        }
    case .failed, .cancelled, .possible:
        break
    }
}

这将使平移达到某个阈值然后滚动:

在此处输入图像描述


推荐阅读