我正在尝试在Swift中长按Gesture作为Copy Option。

但这是行不通的。它也不能在UiView或UILabel中标识手势。

下面是我的代码

在视图中

     let copyLongPress = UILongPressGestureRecognizer(target: self, action: #selector(ContactDetailController.handleLongPress(_:)))
    copyLongPress.numberOfTouchesRequired = 0
    copyLongPress.delegate = self
    copyLongPress.minimumPressDuration=0.5
    self.lblDynaMobile.addGestureRecognizer(copyLongPress)
    self.lblDynaMobile.userInteractionEnabled = true
    self.lblDynaDDI.addGestureRecognizer(copyLongPress)
     self.lblDynaDDI.userInteractionEnabled = true
    self.GeneralView.addGestureRecognizer(copyLongPress)
    self.EmailView.addGestureRecognizer(copyLongPress)
    self.AddressView.addGestureRecognizer(copyLongPress)


新方法

func handleLongPress(longPressView :UILongPressGestureRecognizer) {

    let lblFont:UILabel = (longPressView.view as? UILabel)!
    UIPasteboard.generalPasteboard().string = lblFont.text

}


我也在类的声明中添加了UIGestureRecognizerDelegate

最佳答案

试试看,看看(它正在工作。)

// in viewDidLoad()
let copyLongPress = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress(_:)))
self.lblDynaMobile.addGestureRecognizer(copyLongPress)

func handleLongPress(_ gesture: UILongPressGestureRecognizer) {

    if let lblFont = gesture.view as? UILabel {
      //UIPasteboard.generalPasteboard().string = lblFont.text
    }

}

关于ios - Swift,UILongPressGestureRecognizer在UIlabel中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37303904/

10-14 21:41