How do you call addGestureRecognizer if the interaction is added to a long press function - and not an override func?

166 views Asked by At

Why does the add interaction need to be in objC func?

Because it should only be added if certain database checks are met.

Below is how it works successfully if interaction is added to override function. addGestureRecognizer is also added there.

var cc: UIContextMenuInteraction!


override func viewDidLoad() {
 super.viewDidLoad()
  like?.isUserInteractionEnabled = true
     self.cc = UIContextMenuInteraction(delegate: self)
     like.addInteraction(cc) 
         let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
           like?.addGestureRecognizer(longPress)
}

@objc func didLongPress() {
////whatever goes
}

However, if the interaction is added to the objC function instead of the override function, it only works on 2nd and later long presses. This is presumably because the 1st long press only enables the interaction.

verride func viewDidLoad() {
 super.viewDidLoad()
  like?.isUserInteractionEnabled = true
         let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
           like?.addGestureRecognizer(longPress)
}


      @objc func didLongPress() {
         if statement{
          cc = UIContextMenuInteraction(delegate: self)
          like.addInteraction(cc) 
          }
     } 
0

There are 0 answers