我正在尝试确定用户触摸屏幕的确切时间。 我想出了这个(在我的ViewController中):
var startTime: Date?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
startTime = Date()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let endTime = Date()
print(endTime.timeIntervalSince(startTime!))
}
似乎工作得很好。
这是否准确无误?
有没有办法测试这是多么精确?
答案 0 :(得分:1)
我会采用与你完成相同的结构。您的print(endTime.timeIntervalSince(startTime!))
会为您打印出精确的Double
值。
我会稍微调整一下,检查注释的解释:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
startTime = Date()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// Don´t use startTime!, use if let instead
if let startTime = startTime {
// Use 2 decimals on your difference, or 3, or whatever suits your needs best
let difference = String(format: "%.2f", Date().timeIntervalSince(startTime))
print(difference)
}
}