user7433826
user7433826

Reputation:

Long Press Gesture called multiple times

I am getting an issue with UILongPressGestureRecognizer, I have used this code:-

func addLongPressGesture(){
        let lngPr = UILongPressGestureRecognizer.init(target: self, action: #selector(self.handleLongPress(gesture:)))
        lngPr.delaysTouchesEnded = true
        self.addGestureRecognizer(lngPr)
    }
    @objc func handleLongPress(gesture:UIGestureRecognizer){
       
        if selectedIndexPath != nil && delegate != nil{
            self.delegate?.delegateLongPressed(atIndexPath: selectedIndexPath!)
        }
    }

Upvotes: 3

Views: 1934

Answers (1)

Nancy Madan
Nancy Madan

Reputation: 447

Hey you need to check the state of UILongPressGesture to reform your functions Try this :-

func addLongPressGesture(){
        let lngPr = UILongPressGestureRecognizer.init(target: self, action: #selector(self.handleLongPress(gesture:)))
        lngPr.delaysTouchesEnded = true
        self.addGestureRecognizer(lngPr)
    }
    @objc func handleLongPress(gesture:UIGestureRecognizer){
        if gesture.state == .ended{
        if selectedIndexPath != nil && delegate != nil{
            self.delegate?.delegateLongPressed(atIndexPath: selectedIndexPath!)
        }
        }
    }

Upvotes: 9

Related Questions