ios - 如何使滚动视图在拖动时停止移动并为已经触摸的手指立即恢复移动?
问题描述
如何使滚动视图在拖动时停止移动,之后,当手指已经触摸滚动视图时,如何使其立即开始滚动?
self.scrollView.notMovingWhileDragging()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.scrollView.resumeMovingForTheAlreadyTouchingFinger()
}
对于上述两个函数,我设置isScrollEnabled
为false
then true
,但是如果调用上述回调时手指已经触摸到滚动视图,则滚动视图不会移动。
我想要的结果就像 iOS 地图应用程序一样,当工作表位于顶部时,滚动视图被拖离顶部一点距离。现在我开始向下拖动滚动视图,滚动视图向下移动,当滚动视图滚动到顶部时,然后继续向下拖动,工作表向下(拖动时滚动视图停止移动),然后向上拖动,工作表视图向上,然后当工作表视图位于顶部时,继续向上拖动,滚动视图再次开始滚动(当手指已经在滚动视图上时,滚动视图恢复移动)。
解决方案
这是不可能的。a的isScrollEnabled
属性UIScrollView
只是更新了's的isEnabled
属性。有一个定义的生命周期,从着陆开始。并且它的状态变为,因此在触摸已经开始时启用它不会导致它触发。UIScrollView
panGesture
UIPanGesture
touchesBegan
.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
}
}
这将使平移达到某个阈值然后滚动:
推荐阅读
- html - 在 css 中完成后站立动画
- bixby - 如何处理零搜索结果场景
- javascript - 使用Javascript从类名中删除括号
- unity3d - ScriptableObject 是一个好的数据库设计吗?
- json - 如何根据 JSON 文件中的布尔值自动删除标记
- html - 淘汰赛文本混合模式不适用于
- c# - 唯一标识网站的哪个实例处于活动状态或触发事件
- python - 为什么我得到 NoneType 而不是这个正则表达式的匹配对象?
- linux - Linux 和 Yii - Web 进程不可写该目录:/web/assets
- haskell - 如何使用“基本”规范安装 Haskell?