在 Swift 中无法识别点击手势手势开始

问题描述

UITapGestureRecogniser 开始状态不被识别,只有结束被识别。

override func viewDidLoad() {
     let tapgr = UITapGestureRecognizer(target: self,action: #selector(tapTrigger(recongizer:)))
     bottomBar.addGestureRecognizer(tapgr)
}

@objc func tapTrigger(recongizer: UITapGestureRecognizer){
        if recongizer.state == .began{
            print("recognised") // does not print
        }else if recogniser.state == .ended{
           print("ended") //prints
        }
}

我想要做的是在识别触摸时突出显示视图(而不是添加识别器的视图),并在取消触摸时取消突出显示

解决方法

您可以使用 UITapGestureRecognizer 来获取不同的状态,而不是使用 UILongPressGestureRecognizer

像这样:

let tapgr = UILongPressGestureRecognizer(target: self,action: #selector(tapTrigger(recongizer:)))
tapgr.minimumPressDuration = 0
bottomBar.addGestureRecognizer(tapgr)

@objc func tapTrigger(recongizer: UITapGestureRecognizer){
        if recongizer.state == .began{
            print("recognised") 
        } else if recongizer.state == .ended{
           print("ended") //prints
        }
}