0

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) 
          }
     } 
uuuuuu
  • 119
  • 1
  • 7
  • What's the question ? if you know that **This is presumably because the 1st long press only enables the interaction.?** then what's your problem ? – Shehata Gamal Mar 12 '21 at 14:23
  • What is the argument for addInteraction? – stevenpcurtis Mar 12 '21 at 14:25
  • @Sh_Khan because the first long press shouldn't only enable interaction. It should also call the gesture. Otherwise a user would always need to long press twice to get the desired gesture – uuuuuu Mar 12 '21 at 14:45
  • why not to add database check inside `viewDidLoad` ? – Shehata Gamal Mar 12 '21 at 14:46
  • @Sh_Khan Because in the line `let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))`, the selector needs to be a distinct ObjC function. If I just add the database check there, it will not have a selector and won't produce the gesture – uuuuuu Mar 12 '21 at 14:55
  • @stevenpcurtis added – uuuuuu Mar 12 '21 at 14:56

0 Answers0