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

时间:2022-09-06 21:05:04

I have already accomplished a single tap recognizer but can not figure out how to make that single tap recognizer a double tap instead. I could use some guidance.

我已经完成了单击识别器,但无法弄清楚如何将该单击识别器改为双击。我可以使用一些指导。

Code:

码:

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

5 个解决方案

#1


2  

I solved this with an extension:

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

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

#2


8  

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

#3


7  

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

Magic in below three lines

魔术在三线以下

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

#4


5  

Here is some example code for a single and double tap action.

以下是单击和双击操作的示例代码。

Notice that the single tap action has a colon (handleSingleTap:) but the double tap action does not (handleDoubleTap)? This indicates that the selector takes an argument. So in the functions below, we can check the sender of the single tap which would be useful if you were assigning the tap function to many elements. If you have the colon you must have the parameters in the function.

请注意,单击操作有一个冒号(handleSingleTap :),但双击动作没有(handleDoubleTap)?这表明选择器采用了参数。因此,在下面的函数中,我们可以检查单击的发送者,如果您将tap功能分配给许多元素,这将是有用的。如果您有冒号,则必须在函数中包含参数。

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

Note: this example will run the single tap action in a double tap.

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

#5


0  

More Swifty way:

更多Swifty方式:

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

Closure:

关闭:

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

It's very important not to lose reference to your action (you can make it as field like in above example).

非常重要的是不要丢失对您的操作的引用(您可以像上面的示例中那样将其设置为字段)。

by using this UIView extension: https://gist.github.com/yoman07/7aae5abb957ff656b2f118ad1e6c2d36

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

#1


2  

I solved this with an extension:

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

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

#2


8  

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

#3


7  

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

Magic in below three lines

魔术在三线以下

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

#4


5  

Here is some example code for a single and double tap action.

以下是单击和双击操作的示例代码。

Notice that the single tap action has a colon (handleSingleTap:) but the double tap action does not (handleDoubleTap)? This indicates that the selector takes an argument. So in the functions below, we can check the sender of the single tap which would be useful if you were assigning the tap function to many elements. If you have the colon you must have the parameters in the function.

请注意,单击操作有一个冒号(handleSingleTap :),但双击动作没有(handleDoubleTap)?这表明选择器采用了参数。因此,在下面的函数中,我们可以检查单击的发送者,如果您将tap功能分配给许多元素,这将是有用的。如果您有冒号,则必须在函数中包含参数。

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

Note: this example will run the single tap action in a double tap.

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

#5


0  

More Swifty way:

更多Swifty方式:

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

Closure:

关闭:

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

It's very important not to lose reference to your action (you can make it as field like in above example).

非常重要的是不要丢失对您的操作的引用(您可以像上面的示例中那样将其设置为字段)。

by using this UIView extension: https://gist.github.com/yoman07/7aae5abb957ff656b2f118ad1e6c2d36

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