Swift:长按手势识别器 - 检测点击和长按

Swift: Long Press Gesture Recognizer - Detect taps and Long Press

我想连接一个动作,如果手势是点击,它会以特定方式为对象设置动画,但如果按下持续时间超过 0.5 秒,它会执行其他操作。

现在,我刚刚连接好动画。我不知道如何区分长按和点击? 如何访问新闻持续时间以实现上述目标?

 @IBAction func tapOrHold(sender: AnyObject) {
        UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {

            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {

                self.polyRotate.transform = CGAffineTransformMakeRotation(1/3 * CGFloat(M_PI * 2))
            })
            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
                self.polyRotate.transform = CGAffineTransformMakeRotation(2/3 * CGFloat(M_PI * 2))
            })
            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
                self.polyRotate.transform = CGAffineTransformMakeRotation(3/3 * CGFloat(M_PI * 2))
            })

            }, completion: { (Bool) in
                let vc : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("NextView")
                self.showViewController(vc as UIViewController, sender: vc)
        })

定义两个 IBActions 并为每个设置一个 Gesture Recognizer。这样你就可以为每个手势执行两个不同的动作。

您可以在界面生成器中将每个 Gesture Recognizer 设置为不同的 IBAction。

@IBAction func tapped(sender: UITapGestureRecognizer)
{
    println("tapped")
    //Your animation code.
}

@IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
    println("longpressed")
    //Different code
}

通过没有界面生成器的代码

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
    self.view.addGestureRecognizer(tapGestureRecognizer)
    
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
    self.view.addGestureRecognizer(longPressRecognizer)

func tapped(sender: UITapGestureRecognizer)
{
    println("tapped")
}

func longPressed(sender: UILongPressGestureRecognizer)
{
    println("longpressed")
}

Swift 5

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.view.addGestureRecognizer(tapGestureRecognizer)
    
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
self.view.addGestureRecognizer(longPressRecognizer)
    
@objc func tapped(sender: UITapGestureRecognizer){
    print("tapped")
}

@objc func longPressed(sender: UILongPressGestureRecognizer) {
    print("longpressed")
}

通过没有界面生成器的代码

// Global variables declaration
var longPressed = false
var selectedRow = 0



override func viewDidLoad() {
        super.viewDidLoad()  
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ContactListTableViewController.handleLongPress(_:)))
        longPressGesture.minimumPressDuration = 1.0 // 1 second press
        longPressGesture.allowableMovement = 15 // 15 points
        longPressGesture.delegate = self
        self.tableView.addGestureRecognizer(longPressGesture)
    }

// Long tap work goes here !!
if (longPressed == true) {

       if(tableView.cellForRowAtIndexPath(indexPath)?.accessoryType == .Checkmark){
                tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None
                self.selectedRow -= 1

                if(self.selectedRow == 0){
                    self.longPressed = false
                }

            } else {
                self.selectedRow += 1
                tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
            }

        } else if(self.selectedRow == 0) {
          // Single tape work goes here !!
        }

但唯一的问题是长按手势运行了两次。如果您找到任何解决方案,请在下方发表评论!

对于swift2

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.featuredCouponColView.addGestureRecognizer(lpgr)

动作

//MARK: - UILongPressGestureRecognizer Action -
    func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
        if gestureReconizer.state != UIGestureRecognizerState.Ended {
            //When lognpress is start or running
        }
        else {
            //When lognpress is finish
        }
    }

对于Swift4.2/Swift5

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.colVw.addGestureRecognizer(lpgr)

//MARK: - UILongPressGestureRecognizer Action -
    @objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
        if gestureReconizer.state != UIGestureRecognizer.State.ended {
            //When lognpress is start or running
        }
        else {
            //When lognpress is finish
        }
    }

Swift 5 使用界面生成器

对于普通点击,您只需从按钮创建一个 touch up inside 操作即可。

对于长按,为您的按钮创建一个出口,创建点击手势识别器并将其设置为按钮,然后创建选择器方法来执行长按任务。

@IBOutlet var myButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addToListButtonLongPress(_:)))
    longPressRecognizer.numberOfTouchesRequired = 1
    longPressRecognizer.allowableMovement = 10
    longPressRecognizer.minimumPressDuration = 0.5
    myButton.addGestureRecognizer(longPressRecognizer)
}

// Connected to myButton in interface builder.
@IBAction func myButtonTapped(_ sender: UIButton) {
    print("button tapped")
}

@objc func myButtonLongPressed(_ sender: UILongPressGestureRecognizer) {
    print("button long pressed")
}