如何在 Swift 中添加双击手势识别器 [英] How to add a double tap Gesture Recognizer in Swift

查看:22
本文介绍了如何在 Swift 中添加双击手势识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

代码:

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

推荐答案

我通过扩展解决了这个问题:

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

这篇关于如何在 Swift 中添加双击手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆