首页 > 解决方案 > 如果将交互添加到长按功能而不是覆盖功能,您如何调用 addGestureRecognizer?

问题描述

为什么 add 交互需要在 objC func 中?

因为只有在满足某些数据库检查时才应该添加它。

如果将交互添加到覆盖功能,它是如何成功工作的。addGestureRecognizer 也被添加在那里。

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
}

但是,如果将交互添加到 objC 函数而不是覆盖函数,则它仅适用于第 2 次及以后的长按。这大概是因为第一次长按只启用交互。

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) 
          }
     } 

标签: iosswiftobjective-cuigesturerecognizeruilongpressgesturerecogni

解决方案


推荐阅读