gpt4 book ai didi

ios - 我将 UIGestureRecognizer 添加到 View 以检测 Touch Down Action ,但是 View 上方的按钮将不再起作用

转载 作者:行者123 更新时间:2023-11-28 13:34:50 25 4
gpt4 key购买 nike

我有一个 View 需要在全 View 上检测“Touch Down” Action ,在我发现普通的 UITapGestureRecognizer 无法完成之后,我自定义了 UIestureRecognizer 类来实现它,using this .

所以它确实有效,但我在 View 上有几个按钮,在我可以检测到 View 的 Touch Down 操作后,这些按钮停止工作。我需要它们都工作, View 检测 Touch Down 并且可以在 View 上方按下按钮。我怎样才能实现它?

需要明确的是,那些按钮只需要检测正常的 Touch Up Inside Action ,只有 View 需要检测 Touch Down Action 。此外,当按钮上发生触摸事件时, View 不需要对触摸事件使用react。

谢谢。

import UIKit
import UIKit.UIGestureRecognizerSubclass

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let t = SingleTouchDownGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
self.view.addGestureRecognizer(t)

}

@objc func handleTap(sender: UITapGestureRecognizer? = nil) {

self.view.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
UIView.animate(withDuration: 1) {
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}

}
@IBAction func testBtnPressed(_ sender: Any) {
print("testBtnPressed!")
}

}

class SingleTouchDownGestureRecognizer: UIGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if self.state == .possible {
self.state = .recognized
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
self.state = .failed
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
self.state = .failed
}
}

最佳答案

您可以比较 touchesBegan 中的那个事件具有与分配给您的手势识别器的 View 相同的 View 的触摸

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
guard let view = self.view else { return }
guard let touches = event.touches(for: view) else { return }
super.touchesBegan(touches, with: event)
if self.state == .possible {
self.state = .recognized
}
}

关于ios - 我将 UIGestureRecognizer 添加到 View 以检测 Touch Down Action ,但是 View 上方的按钮将不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56816162/

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