长按手势多次调用

时间:2018-08-09 08:47:41

标签: swift4.1

嘿,我在使用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!)
        }
    }

1 个答案:

答案 0 :(得分:0)

嘿,您需要检查UILongPressGesture的状态以改革功能 试试这个:-

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