如何在 Swift 中添加双击手势识别器

How to add a double tap Gesture Recognizer in Swift

我已经完成了单击识别器,但不知道如何将单击识别器改为双击。我可以使用一些指导。

代码:

import Foundation
import UIKit

class MainBoardController: UIViewController{

    let tap = UITapGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "GotoProfile")
        swipe.direction = UISwipeGestureRecognizerDirection.Right
                    self.view.addGestureRecognizer(swipe)

        tap.addTarget(self, action: "GotoCamera")
        view.userInteractionEnabled = true
        view.addGestureRecognizer(tap)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func GotoProfile(){
        self.performSegueWithIdentifier("Profilesegue", sender: nil)
    }

    func GotoCamera(){
        self.performSegueWithIdentifier("Camerasegue", sender: nil)
    }
}

Try Below Code

import UIKit

class MainBoardController: UIViewController{

let tap = UITapGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "GotoProfile")
    swipe.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipe)

    // DOUBLE TAP
    tap.numberOfTapsRequired = 2
    tap.addTarget(self, action: "GotoCamera")
    view.userInteractionEnabled = true
    view.addGestureRecognizer(tap)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func GotoProfile(){
    self.performSegueWithIdentifier("Profilesegue", sender: nil)
}

func GotoCamera(){
    self.performSegueWithIdentifier("Camerasegue", sender: nil)
}
}

这是单击和双击操作的一些示例代码。

注意到单击操作有一个冒号 (handleSingleTap:) 但双击操作没有 (handleDoubleTap)?这表明选择器接受一个参数。因此,在下面的函数中,我们可以检查单击的发送者,如果您将点击函数分配给多个元素,这将很有用。如果你有冒号,你必须在函数中有参数。

override func viewDidLoad() {
    super.viewDidLoad()

    // Single Tap
    let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
    singleTap.numberOfTapsRequired = 1
    self.view.addGestureRecognizer(singleTap)

    // Double Tap
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleDoubleTap")
    doubleTap.numberOfTapsRequired = 2
    self.view.addGestureRecognizer(doubleTap)
}

func handleSingleTap(sender: AnyObject?) {
    print("Single Tap!")
}
func handleDoubleTap() {
    print("Double Tap!")
}

注意:此示例将运行双击操作中的单击操作。

     // Single Tap
    let singleTap: UITapGestureRecognizer =  UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleSingleTap(sender:)))
    singleTap.numberOfTapsRequired = 1
    self.viewTop.addGestureRecognizer(singleTap)

    // Double Tap
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleDoubleTap))
    doubleTap.numberOfTapsRequired = 2
    self.viewTop.addGestureRecognizer(doubleTap)

    singleTap.require(toFail: doubleTap)
    singleTap.delaysTouchesBegan = true
    doubleTap.delaysTouchesBegan = true

   func handleSingleTap(sender: AnyObject?) {

    print("Single Tap!")

   }
   func handleDoubleTap() {

    print("Double Tap!")
   }

三行以下的魔法

    singleTap.require(toFail: doubleTap)
    singleTap.delaysTouchesBegan = true
    doubleTap.delaysTouchesBegan = true

更快捷的方式:

view?.tap(numberOfTapsRequired: 2, action: action)

关闭:

private lazy var action: Action = Action(action: {[weak self] _ in
    print("Double click")
})

重要的是不要丢失对你的操作的引用(你可以像上面的例子那样把它作为字段)。

通过使用这个 UIView 扩展: https://gist.github.com/yoman07/7aae5abb957ff656b2f118ad1e6c2d36

我用扩展解决了这个问题:

override func viewDidLoad() {
    super.viewDidLoad()
            
    let tapGR = UITapGestureRecognizer(target: self, action: #selector(PostlistViewController.handleTap(_:)))
    tapGR.delegate = self
    tapGR.numberOfTapsRequired = 2
    view.addGestureRecognizer(tapGR)
}

extension MainBoardController: UIGestureRecognizerDelegate {
    func handleTap(_ gesture: UITapGestureRecognizer){
        print("doubletapped")
    }
}