微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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)
    }
}
我用扩展解决了这个问题:
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")
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐