多次调用长按手势

Long Press Gesture called multiple times

我遇到了 UILongPressGestureRecognizer 的问题,我使用了这段代码:-

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!)
        }
    }

嘿,你需要检查 ONLongPressGesture 的状态来改造你的功能 试试这个:-

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!)
        }
        }
    }