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

时间:2015-08-28 03:16:09

标签: swift uigesturerecognizer

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

代码:

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 个答案:

答案 0 :(得分:9)

  

尝试以下代码

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

答案 1 :(得分:9)

     // 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

答案 2 :(得分:5)

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

请注意,单击操作有一个冒号(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!")
}

注意:此示例将以双击方式运行单击操作。

答案 3 :(得分:5)

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

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

答案 4 :(得分:0)

更多Swifty方式:

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