Ios 确定两次触摸之间的准确时间
我试图确定用户触摸屏幕的准确时间。 我已经想出了这个(在我的ViewController中):Ios 确定两次触摸之间的准确时间,ios,swift,time,uiview,Ios,Swift,Time,Uiview,我试图确定用户触摸屏幕的准确时间。 我已经想出了这个(在我的ViewController中): var开始时间:日期? 覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){ 开始时间=日期() } 覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){ 让endTime=Date() 打印(endTime.timeIntervalSince(startTime!)) } 看起来效果不错。 这是最精确的吗? 有没有办法测试它有
var开始时间:日期?
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
开始时间=日期()
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
让endTime=Date()
打印(endTime.timeIntervalSince(startTime!))
}
看起来效果不错。这是最精确的吗?
有没有办法测试它有多精确?我会选择与您相同的结构。您的
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)
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
开始时间=日期()
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
//不要使用startTime!而是使用if-let
如果让startTime=startTime{
//在你的差异上使用2位小数,或3位小数,或任何最适合你需要的小数
让差异=字符串(格式:“%.2f”,日期().timeIntervalSince(startTime))
打印(差异)
}
}
我会选择与您相同的结构。您的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)
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
开始时间=日期()
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
//不要使用startTime!而是使用if-let
如果让startTime=startTime{
//在你的差异上使用2位小数,或3位小数,或任何最适合你需要的小数
让差异=字符串(格式:“%.2f”,日期().timeIntervalSince(startTime))
打印(差异)
}
}
首先,您的标题有点混乱。请调整一下!好吧,为了测试它,你要么试着用其他方法来测量时间,要么你需要第二种方法来做同样的事情。但在我看来,当你想测量从触摸开始到结束的总时间时,这应该是最好的方法。但是不要使用Date
你怎么看?首先,你的标题有点混乱。请调整一下!好吧,为了测试它,你要么试着用其他方法来测量时间,要么你需要第二种方法来做同样的事情。但在我看来,当你想测量从触摸开始到结束的总时间时,这应该是最好的方法。但是不要使用Date
日期,你觉得如何?我发现这个方法很有效。还有@Marcel T关于使用UITouches时间戳提高准确性的评论。@Marmelador,太棒了!我发现这很有效。还有@Marcel T关于使用UITouches时间戳提高准确性的评论。@Marmelador,太棒了!