gpt4 book ai didi

ios - 如何排除 subview 的手势?

转载 作者:行者123 更新时间:2023-11-29 05:57:17 24 4
gpt4 key购买 nike

我正在使用一个在 ImageView 中获取签名的应用程序。我使用 SWRevealViewController 作为侧面菜单。我的应用程序看起来像这样。

enter image description here

     class CaptureSignature: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate {

我正在编写下面的代码来显示侧面菜单

     override func viewDidLoad() {
super.viewDidLoad()
let revealViewController = self.revealViewController()
menuBtn.addTarget(revealViewController, action: #selector(SWRevealViewController.rightRevealToggle(_:)), for: .touchUpInside)
revealViewController?.panGestureRecognizer()?.isEnabled = true
}

现在我的问题是,当我从右向左滑动 ImageView 时,侧边菜单打开,而不是画一条线。(因为我在 SWRevealViewController 中启用了 panGesture)。

我不想要的是,我不想在 imageView 上启用滑动手势以在向右滑动时显示菜单栏。我写了下面的函数,但它不起作用(即使当我在其中放置断点时它也没有进入方法)

       func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {


if let touchedView = touch.view, touchedView.isDescendant(of: imageView) {
print("touched in image view")
return false
}
return false

}

谁能帮帮我。

最佳答案

您可以使用以下方法忽略特定区域的滑动手势。

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool

您仍然可以调用touchesMoved来绘制您的签名。这是示例代码。

func addSwipeGesture() {
let leftSwipe = UISwipeGestureRecognizer(target: self, action:
#selector(handleSwipes(_:)))
leftSwipe.delegate = self
let rightSwipe = UISwipeGestureRecognizer(target: self, action:
#selector(handleSwipes(_:)))
rightSwipe.delegate = self

leftSwipe.direction = .left
rightSwipe.direction = .right

self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}

@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {

if (sender.direction == .left) {
print("Swipe Left")
}

if (sender.direction == .right) {
print("Swipe Right")
}
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive
touch: UITouch) -> Bool {
return !self.imgView.bounds.contains(touch.location(in: self.imgView))
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("moving")
}

对于上面的代码片段, ImageView 的滑动将被忽略,即 imgView

关于ios - 如何排除 subview 的手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55022207/

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