swift - 如何停止刷新点击?
swift - how to stop refreshing taps?
我编写了一些代码,当用户点击屏幕上的任何地方时,points/score/taps 将递增 1。但问题是它会计算我连续点击了多少次,如果我离开按下它之间的 1 秒间隔将重新启动计数器。有什么办法可以让它停止重启吗?
CODE:
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)"
}
}
The tapCount
documentation says:
The value of this property is an integer indicating the number of times the user tapped their fingers on a certain point within a predefined period.
它应该在某个“预定时间段”后重置。您正试图将它用于并非设计用于的用途。
相反,您需要在 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.)
我编写了一些代码,当用户点击屏幕上的任何地方时,points/score/taps 将递增 1。但问题是它会计算我连续点击了多少次,如果我离开按下它之间的 1 秒间隔将重新启动计数器。有什么办法可以让它停止重启吗?
CODE:
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)"
}
}
The tapCount
documentation says:
The value of this property is an integer indicating the number of times the user tapped their fingers on a certain point within a predefined period.
它应该在某个“预定时间段”后重置。您正试图将它用于并非设计用于的用途。
相反,您需要在 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.)