1

I can not able to show custom MenuController while long Press gesture on tableview cell.

My code is as follow:

 override func viewDidLoad() {
        super.viewDidLoad()
        self.setupLongPressGesture()
    }

func setupLongPressGesture() {
        let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
        self.tbl.addGestureRecognizer(longPressGesture)
    }

    @objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
        if gestureRecognizer.state == .began {
            let touchPoint = gestureRecognizer.location(in: self.tbl)
            if let indexPath = tbl.indexPathForRow(at: touchPoint) {

                let cell = tbl.cellForRow(at: indexPath) as! ChattingTextCell
                self.view.becomeFirstResponder()                
                let copy = UIMenuItem(title: "piy", action: #selector(copy_text))

                let menucontroller = UIMenuController.shared
                menucontroller.menuItems = [copy]
                menucontroller.update()
                //menucontroller.setTargetRect(cell.frame, in: tbl)
                menucontroller.setTargetRect(CGRect(x: 100, y: 200, width: 100, height: 50), in: self.view)
                menucontroller.setMenuVisible(true, animated: true)

            }
        }
    }


     func canBecomeFirstResponder() -> Bool {
        return true
    }



    func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
    {
        print(action)
        return true
    }

I already refer to many sites but all of them saying the same thing... I can not find any good tutorial for this as well... Please help me to solve this....

nirav
  • 573
  • 5
  • 20

0 Answers0