使用Swift对UIButton进行多次触摸?

时间:2015-05-31 20:13:41

标签: ios swift uibutton touch

我正在尝试计算某人同时触摸该按钮的手指数量。

我点击一次:+1,我用两根手指敲击:+2,我用三根手指敲击:+3依旧......

我现在有什么:

var count = 0;
@IBAction func onTap(sender: UIButton) {
    count++;
}

" onTap" 功能,即使我用多个手指触摸按钮,也只会触发一次。

如何跟踪按钮被触摸的手指数量?

1 个答案:

答案 0 :(得分:2)

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var bigButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        let oneFingerTap = UITapGestureRecognizer(target: self, action:"oneFingerTapDetected:")
        oneFingerTap.numberOfTouchesRequired = 1
        let twoFingerTap = UITapGestureRecognizer(target: self, action:"twoFingerTapDetected:")
        twoFingerTap.numberOfTouchesRequired = 2
        let threeFingerTap = UITapGestureRecognizer(target: self, action:"threeFingerTapDetected:")
        threeFingerTap.numberOfTouchesRequired = 3
        bigButton.addGestureRecognizer(oneFingerTap)
        bigButton.addGestureRecognizer(twoFingerTap)
        bigButton.addGestureRecognizer(threeFingerTap)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    func oneFingerTapDetected(sender:UITapGestureRecognizer) {
        println("one")
    }
    func twoFingerTapDetected(sender:UITapGestureRecognizer) {
        println("two")
    }
    func threeFingerTapDetected(sender:UITapGestureRecognizer) {
        println("three")
    }
}