I translated some code from a storyboard to be created programmatically. So why isn't my gesture recognizer getting called when I programmatically create a gesture recognizer?
There are other SO posts about ensuring UIView.isUserInteractionEnabled
is enabled on the view and that's not the problem. I've also tried setting the UIGestureRecognizerDelegate: In gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
I see my gesture triggering and always returning true doesn't help. Yes, I am installing the gesture recognizer with UIView.addGestureRecognizer(_:)
!
class ExampleView: UIView {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(myAction(_:)))
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
@objc func myAction(_ sender: UITapGestureRecognizer) {
print("never happens")
}
private func commonInit() {
isUserInteractionEnabled = true
tapGestureRecognizer.delegate = self
addGestureRecognizer(tapGestureRecognizer)
}
}
extension ExampleView: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}