gpt4 book ai didi

ios - ScrollView 平移手势与 panGestureRecognizer 之间的冲突

转载 作者:搜寻专家 更新时间:2023-10-30 22:10:11 28 4
gpt4 key购买 nike

我在 UIViewController 中有一个 UIScrollView,它由 segue 模态显示,另外一个 UIPanGestureRecognizer 确实关闭了 View Controller 平底锅。此手势仅在

 scrollView.contentOffset.y == 0

问题是,现在两个平移手势相互冲突,我不能再 ScrollView 了。

为了解决这个问题,我尝试使用 gestureRecognizer(_: shouldRecognizeSimultaneouslyWith:) 方法,返回是,而且,我尝试将我的自定义平移手势添加到 UIScrollView 像这样平移手势识别器:

 scrollView.panGestureRecognizer.addTarget(self, action: #selector(handlePanGesture(_:)))

但是这些并不能解决问题如果您知道如何解决此问题,我将不胜感激。

已编辑

这是我关闭 View Controller 的平移手势的代码:

     @IBAction func handlePanGesture(_ sender: UIPanGestureRecognizer) {
let percentThreshold: CGFloat = 0.3

if scrollView.contentOffset.y == 0 {
let translation = sender.translation(in: view)
let verticalMovement = translation.y / view.bounds.height
let downwardMovement = fmaxf(Float(verticalMovement), 0.0)
let downwardMovementPercent = fminf(downwardMovement, 1.0)
let progress = CGFloat(downwardMovementPercent)

guard let interactor = interactor else {return}
switch sender.state {
case .began:
interactor.hasStarted = true
dismiss(animated: true, completion: nil)
case .changed:
interactor.shouldFinish = progress > percentThreshold
interactor.update(progress)
case .cancelled:
interactor.hasStarted = false
interactor.cancel()
case .ended:
interactor.hasStarted = false
interactor.shouldFinish ? interactor.finish() : interactor.cancel()
default:
break
}

}
}

EDITED_2以下是 Interactor 的代码:

class Interactor: UIPercentDrivenInteractiveTransition {
var hasStarted = false
var shouldFinish = false

附言我知道有很多类似的问题,但它们对我不起作用。

最佳答案

要在 UIPanGestureRecognizer 在 ScrollView 上时允许滚动,您需要创建一个 UIGestureRecognizerDelegate,它在 gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) 上返回 true >

如果不这样做,将无法在 ScrollView 上滚动。

这样做是这样的:

let scrollViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(onPan(_:)))
scrollViewPanGesture.delegate = self
scrollView.addGestureRecognizer(scrollViewPanGesture)

extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

关于ios - ScrollView 平移手势与 panGestureRecognizer 之间的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48491689/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com