tap gesture recognizer not working…?

て烟熏妆下的殇ゞ 提交于 2019-12-10 22:42:33

问题


I had to create a new thread bcoz it's driving me crazy and all the other answers online are exactly the same. I have done this countless of times but I cannot see what I am missing for the life of me. I am using a "test" view controller just to get the tap gesture working but it isn't working at all... I am fairly certain that I am setting this up correctly, as this is how I've always implemented it in the past: (yes, I have checked the box for isUserInteractionEnabled). I am even implementing this on a different viewcontroller this exact way and it is working...

class TestViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    view.addGestureRecognizer(tap)
}

let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))


@objc func wasTapped() {
    print("tapped")
}

}

I have also tried adding the parameters to wasTapped:

 @objc func wasTapped(gestureRecognizer: UITapGestureRecognizer) {
    print("tapped")
}

回答1:


You are saying:

override func viewDidLoad() {
    super.viewDidLoad()

    view.addGestureRecognizer(tap)
}

let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

The problem is the last line:

let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

You cannot just say let tap like that in the middle of nowhere. You are implicitly making an instance property. But you cannot initialize an instance property with a target of self, because self does not exist at the time an instance property is initialized. (I regard the fact that that code even compiles as a bug, and have reported it as such.)

Move that line to the start of viewDidLoad, like this:

override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))
    view.addGestureRecognizer(tap)
}



回答2:


Try something like this

override func viewDidLoad() {
    super.viewDidLoad()

     let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped(sender:)))
     tap.numberOfTapsRequired = 1 // Default value
     view.isUserInteractionEnabled = true
     view.addGestureRecognizer(tap)

}

@objc func wasTapped(sender: UITapGestureRecognizer) {
    print("tapped")
}



回答3:


You have to enable interaction if you want to use gesture recognizers for standard UIView's

Add view.isUserInteractionEnabled = true in your viewDidLoad.




回答4:


var tapGesture = UITapGestureRecognizer()

take a view and set IBOutlet like:

@IBOutlet weak var viewTap: UIView!     

Write pretty code on viewDidLoad() like:

tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.myviewTapped(_:)))
    tapGesture.numberOfTapsRequired = 1
    tapGesture.numberOfTouchesRequired = 1
    viewTap.addGestureRecognizer(tapGesture)
    viewTap.isUserInteractionEnabled = true

this method is calling when tap gesture recognized

  @objc func myviewTapped(_ sender: UITapGestureRecognizer) {

    if self.viewTap.backgroundColor == UIColor.yellow {
        self.viewTap.backgroundColor = UIColor.green
    }else{
        self.viewTap.backgroundColor = UIColor.yellow
    }
}


来源:https://stackoverflow.com/questions/47297551/tap-gesture-recognizer-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!