Swift,UILongPressGestureRecognizer在UIlabel中不起作用

时间:2016-05-18 15:23:13

标签: ios swift uigesturerecognizer uilongpressgesturerecogni

我正试图在Swift中长按Gesture for Copy Option。

但它不起作用。它也没有在UiView或UILabel中识别出手势。

以下是我的代码

查看DidLoad

     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) 

New Mothod

func handleLongPress(longPressView :UILongPressGestureRecognizer) {

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

}

我在类声明中添加了UIGestureRecognizerDelegate

1 个答案:

答案 0 :(得分:0)

试试这个,看看(它正在运作。)

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

}