0

I am trying long press Gesture in Swift for Copy Option.

But it is not working. It is not identifying the Gesture in UiView Or UILabel either.

Below is my Code

In View 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

}

I have added UIGestureRecognizerDelegate too in the Declaration of class

Krunal
  • 77,632
  • 48
  • 245
  • 261
dhaval shah
  • 4,499
  • 10
  • 29
  • 42
  • check this answer http://stackoverflow.com/questions/10613118/uilabel-uilongpressgesturerecognizer-not-working?rq=1 – Sanman May 18 '16 at 15:36
  • @sanmanborate, i have already done that..but not working.. self.lblDynaMobile.userInteractionEnabled = true – dhaval shah May 19 '16 at 04:16
  • @dhavalshah, check this answer http://stackoverflow.com/questions/13929703/why-does-uitableviews-swipe-delete-sometimes-work-fine-sometimes-not/20157326#20157326 – bademi Jan 07 '17 at 00:43

1 Answers1

0

Try this, and see (it's working.)

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

}
Krunal
  • 77,632
  • 48
  • 245
  • 261