gpt4 book ai didi

ios - subview Controller 上的 PanGestureRecognizer

转载 作者:行者123 更新时间:2023-11-29 05:49:33 28 4
gpt4 key购买 nike

我在 subview Controller 的容器 View 上设置了一个 PanGestureRecognizer 。这本质上是一个抽屉 View ,所以我希望 subview Controller 能够处理任何滚动,除非 subview Controller 的 TableView 滚动到顶部并且用户向下平移。在这种情况下,我希望容器 View 手势识别器接管,以便 subview Controller 作为一个整体可以向下动画化。我有一个手势识别器的委托(delegate),如果它不是 subview Controller 和容器 View ,而只是一个 TableView ,并且我将平移手势识别器添加到其中,那么它就可以完美工作。但现在的情况是,委托(delegate)在应该返回 true 的时候返回 true,但handlePan 方法没有被调用?任何想法,将不胜感激。谢谢!

下面的所有代码都位于具有容器 View 的父 UIViewController 中,我正在向其应用手势识别器。一切都显示正确,只是没有调用手势识别器方法,而是调用了委托(delegate)。

func setUpItemDetailsController() {
guard let itemDetailsController = itemDetailsController else { return }
add(itemDetailsController)
itemDetailsContainerView.addSubview(itemDetailsController.view)

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panGestureRecognizer.delegate = self
itemDetailsContainerView.addGestureRecognizer(panGestureRecognizer)
}

@objc func handlePan(_ sender: UIPanGestureRecognizer) {
...
}

extension MoverScanAndDiscoverResultController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
guard let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer else { return true }

let translation = panGestureRecognizer.translation(in: view).y
let topLimit = view.bounds.height - self.statusAndNavBarCombinedHeight

print(itemDetailsTableView.contentOffset.y)

// Allows for normal UITableView scrolling
if translation < 0
&& resultCardViewBottomConstraint.constant == topLimit
|| itemDetailsController?.tableView.contentOffset.y ?? 0 > 0 {
return false
}

return true
}
}

最佳答案

您是否尝试过使用 UIGestureRecognizerDelegate 函数与其他手势识别器同时进行识别?

class MyViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// etc...

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panned(_:)))
panGestureRecognizer.delegate = self
self.tableView.addGestureRecognizer(panGestureRecognizer)
}

@objc
func panned(_ sender: UIPanGestureRecognizer) {
print("Panning")
}
}

extension MyViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true // obviously this could be more refined if you have other gesture recognizers
}
}

我没有尝试将手势识别器添加到容器 View ,但将其直接添加到 tableView 对我来说效果很好。

关于ios - subview Controller 上的 PanGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55822237/

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