如何实现双击以获取更多信息等进一步操作?

How to implement double tap for further action like for more information?

我实现了 switch 语句。现在我正在努力添加一个双击手势来显示更多信息,具体取决于从开关中选择的案例。


override func viewDidLoad() {

    let tap = UITapGestureRecognizer(target: self, action: #selector(moreInfo))
    tap.numberOfTapsRequired = 2
    view.addGestureRecognizer(tap)
}

// the main switch case
switch choice {
case 1:
    print("1 is chosen")
    moreInfo(option:1)
case 2:
    print("2 is chosen")
    moreInfo(option:2)

}


//to be activated when double tap 
@objc func moreInfo(option: Int) {
    switch choice {
    case 1:
        print("more information for 1")

    case 2:
        print("more information for 2")
    }
}

我只是写了一些示例代码。希望对你有帮助。

像下面的代码一样,您可以使用双击和单击操作。

class ViewController: UIViewController {

    var singleTap: UITapGestureRecognizer!
    var doubleTap: UITapGestureRecognizer!

    override func viewDidLoad() {
        super.viewDidLoad()

        doubleTap = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
        view.addGestureRecognizer(doubleTap)
        doubleTap.numberOfTapsRequired = 2
        doubleTap.delaysTouchesBegan = true

        singleTap = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
        singleTap.delaysTouchesBegan = true
        singleTap.require(toFail: doubleTap)
        view.addGestureRecognizer(singleTap)
    }

    @objc
    func tap(gesture: UITapGestureRecognizer) {
        switch gesture {
        case singleTap:
            moreInfo(option: 1)
        case doubleTap:
            moreInfo(option: 2)
        default:
            print("---")
        }
    }

    func moreInfo(option: Int) {
        switch option {
        case 1:
            print("more information for 1")

        case 2:
            print("more information for 2")
        default:
            print("---")
        }
    }
}