我是一個(gè)不愛總結(jié)的人,但是今天在開發(fā)遇到的bug,我不記錄下來(lái)是不行了,一是為了自己防止以后忘了,二是給以后的人鋪路,都2020年了,不能讓百度再把,15、16年的東西放在前面了,惡心到我了。
Xcode 版本11.2, Swift版本5,這是關(guān)于添加手勢(shì)后,點(diǎn)擊事件無(wú)法響應(yīng)的問(wèn)題,(事先聲明:isUserInteractionEnabled = true 不要忘了)
不知道何時(shí)起Swift的手勢(shì)識(shí)別器需要懶加載初始化,有大佬說(shuō)是Xcode 10之后,我具體也不清楚,不過(guò)今天是遇到了,不響應(yīng),就需要懶加載,好了上代碼。
class ViewController: UIViewController {
@IBOutlet weak var aImageView: UIImageView! //拖拽的控件
lazy var tap: UITapGestureRecognizer = {
let tap = UITapGestureRecognizer(target: self, action: #selector(singleTapAction))
return tap
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.isUserInteractionEnabled = true
aImageView.isUserInteractionEnabled = true
aImageView.addGestureRecognizer(tap)
// Do any additional setup after loading the view.
}
@objc func singleTapAction() {
print("\(String(describing: tap.view))")
}
}
對(duì)了,補(bǔ)充一下:方法不需要加參數(shù),不用聽一些人扯屁,不需要參數(shù)也可以調(diào)用點(diǎn)擊方法