我将 UIGestureReconizer 添加到视图以检测 Touch Down 动作,但是视图上方的按钮将不再起作用
I add a UIGestureReconizer to a view to detect Touch Down action, but then the buttons above the view won't work anymore
我有一个视图需要检测全视图的"Touch Down"动作,在我发现普通的UITapGestureRecognizer无法完成后,我自定义了UIestureRecognizer class来实现它,using this.
所以它确实有效,但我在视图上有几个按钮,在我可以检测到视图的 Touch Down 操作后,这些按钮停止工作。
我需要它们都工作,视图检测 Touch Down 并且可以在视图上方按下按钮。我怎样才能实现它?
明确地说,那些按钮只需要检测正常的 Touch Up Inside 动作,只有视图需要检测 Touch Down 动作。此外,当按钮上发生触摸事件时,视图不需要对触摸事件做出反应。
谢谢。
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 中的事件与视图的触摸进行比较,该视图与 您的手势识别器已分配的视图相同
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
}
}
我有一个视图需要检测全视图的"Touch Down"动作,在我发现普通的UITapGestureRecognizer无法完成后,我自定义了UIestureRecognizer class来实现它,using this.
所以它确实有效,但我在视图上有几个按钮,在我可以检测到视图的 Touch Down 操作后,这些按钮停止工作。 我需要它们都工作,视图检测 Touch Down 并且可以在视图上方按下按钮。我怎样才能实现它?
明确地说,那些按钮只需要检测正常的 Touch Up Inside 动作,只有视图需要检测 Touch Down 动作。此外,当按钮上发生触摸事件时,视图不需要对触摸事件做出反应。
谢谢。
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 中的事件与视图的触摸进行比较,该视图与 您的手势识别器已分配的视图相同
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
}
}