使用Swift将实时秒表计时器格式化为百分之一秒
我有一个使用NSTimer以厘秒(0.01秒)更新间隔的应用程序,以字符串格式显示运行秒表为00:00.00(mm:ss.SS). (基本上克隆iOS内置秒表,以集成到实时运动计时数学问题中,未来可能需要毫秒精度)
我使用(误用?)NSTimer来强制更新UILabel.如果用户按下Start,这是用于开始重复该功能的NSTimer代码: displayOnlyTimer = NSTimer.scheduledTimerWithTimeInterval(0.01,target: self,selector: Selector("display"),userInfo: nil,repeats: true) 这是由上面的NSTimer执行的函数: func display() { let currentTime = CACurrentMediaTime() - timerStarted + elapsedTime if currentTime < 60 { timeDisplay.text = String(format: "%.2f",currentTime) }else if currentTime < 3600 { var minutes = String(format: "%00d",Int(currentTime/60)) var seconds = String(format: "%05.2f",currentTime % 60) timeDisplay.text = minutes + ":" + seconds }else { var hours = String(format: "%00d",Int(currentTime/3600)) var minutes = String(format: "%02d",(Int(currentTime/60)-(Int(currentTime/3600)*60))) var seconds = String(format: "%05.2f",currentTime % 60) timeDisplay.text = hours + ":" + minutes + ":" + seconds } } 将同时运行至少2个显示链接.一旦所有其他元素都在发挥作用,这种方法效率会非常低吗? 当用户按下停止/暂停/重置时,不使用NSTimer更新显示.我没有找到任何直接翻译成Swift的东西.我很确定我正在使用一种低效的方法来强制在UIView中快速更新文本UILabel. 更多细节: 更新:感谢Rob和jtbandes回答我的两个问题(格式化方法和显示更新方法). displayLink = CADisplayLink(target: self,selector: Selector("display")) displayLink.addToRunLoop(NSRunLoop.currentRunLoop(),forMode: NSRunLoopCommonModes) 然后替换代码中的所有实例 displayOnlyTimer.invalidate() 同 displayLink.paused = true (这会暂停显示链接的更新)
对于快速UI更新,您应该使用
CADisplayLink.比显示刷新率更快的任何事情都会浪费处理能力,因为它无法显示.它还提供前一帧的时间戳,以便您可以尝试预测下一帧的时间.
你正在多次计算CACurrentMediaTime() – timerStarted elapsedTime.我建议只做一次并将其保存在局部变量中. 考虑使用NSDateComponentsFormatter.尝试重用格式化程序的一个实例,而不是每次都创建一个新实例(通常是最昂贵的部分).总的来说,你可以做的字符串操作越少越好. 您可以在显示方法的开头和结尾检查CACurrentMediaTime,看看它需要多长时间.理想情况下它应该远小于16.6ms.请关注Xcode调试导航器中的CPU使用率(以及一般功耗). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |