0

我做了一些代码,当用户在屏幕上的任何地方点击时,点/分数/点击将增加 1。但问题是它会计算我连续点击的次数,然后如果我在两者之间留下 1 秒的间隔按下它将重新启动计数器。有什么办法可以让它停止重新启动?

代码:

import UIKit

class ViewController: UIViewController {



    @IBOutlet weak var tapsLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        let touch = touches.first as! UITouch
        let tapCount = touch.tapCount


        tapsLabel.text = "Taps: \(tapCount)"

    }
}
4

1 回答 1

1

tapCount文档说:

该属性的值是一个整数,表示用户在预定义的时间段内用手指点击某个点的次数。

它应该在一些“预定义的时间”之后重置。你试图将它用于它不是为它设计的东西。

相反,您需要创建一个属性ViewController来计算总抽头数:

class ViewController: UIViewController {

    private var tapCount = 0
    @IBOutlet weak var tapsLabel: UILabel!

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        tapCount += 1
        tapsLabel.text = "Taps: \(tapCount)"
    }
}

(请注意,我这里的代码是针对 iOS 9 API 的。touchesBegan:withEvent:方法签名与 iOS 8 API 中的略有不同。)

于 2015-08-27T01:57:23.843 回答