我在视图中添加了一个 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

查看:21
本文介绍了我在视图中添加了一个 UIGestureReconizer 来检测 Touch Down 动作,但是视图上方的按钮将不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图需要在全视图上检测Touch Down"动作,在我发现普通的UITapGestureRecognizer无法完成后,我自定义了UIestureRecognizer类来实现它,使用这个.

I have a view which needs to detect "Touch Down" action on the full view, after I found out normal UITapGestureRecognizer can't accomplish it, I customize UIestureRecognizer class to achieve it, using this.

所以它确实有效,但我在视图上有几个按钮,在我可以检测到视图的 Touch Down 操作后,这些按钮停止工作.我需要它们两个都工作,检测触地的视图和按钮可以在视图上方按下.我怎样才能实现它?

So it does work, but I have several buttons on the view, after I can detect the Touch Down action for the view, those buttons stop to work. I need both of them to work, the view to detect Touch Down and the buttons can be pressed above the view. How can I achieve it?

需要明确的是,这些按钮只需要检测正常的 Touch Up Inside 操作,只有视图需要检测 Touch Down 操作.此外,当按钮上发生触摸事件时,视图不需要对触摸事件做出反应.

To be clear, those buttons just need to detect normal Touch Up Inside action, only the view need to detect Touch Down action. Also, when touch events happen on the buttons, the view doesn't need to do react to the touch event.

谢谢.

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 中的事件对与分配手势识别器的视图相同的视图的触摸

You could compare that event in touchesBegan has touches for the view that is the same as the view to witch your gesture recognizer was assigned

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
        }
    }

这篇关于我在视图中添加了一个 UIGestureReconizer 来检测 Touch Down 动作,但是视图上方的按钮将不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆