0

我正在尝试在 Swift 中长按手势以获取复制选项。

但它不起作用。它也不能识别 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

4

1 回答 1

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
    }

}
于 2017-07-26T16:45:02.887 回答