xcode – Swift计时器,以毫秒为单位

xcode – Swift计时器,以毫秒为单位,第1张

概述我想每毫秒更改一次计时器,但它不能按预期工作. NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("advanceTimer:"), userInfo: nil, repeats: true);func advanceTimer(timer: NSTimer){ self.ti 我想每毫秒更改一次计时器,但它不能按预期工作.
NSTimer.scheduledTimerWithTimeInterval(0.001,target: self,selector: Selector("advanceTimer:"),userInfo: nil,repeats: true);func advanceTimer(timer: NSTimer){    self.time += 0.001;    let milliseconds = self.time * 100;    let remaingMilliseconds = Int((milliseconds % 1000) / 10);    let seconds = Int((milliseconds / 1000) % 60)    let strSeconds = String(format: "%02d",seconds)    let strFraction = String(format: "%02d",remaingMilliseconds)    timerText.text = "\(strSeconds):\(strFraction)"; }

结果是

计时器在毫秒部分(00:100)中更改为100,然后更改为01:00 = 40实际秒

邓肯方法:

var time: NSTimeInterval = 0;var startTime: NSTimeInterval = 0;//And your  timer method...func advanceTimer(timer: NSTimer){    //Total time since timer started,in seconds    self.time = NSDate.timeIntervalSinceReferenceDate() - startTime    println(self.time);    //The rest of your code goes here}overrIDe func dIDMovetoVIEw(vIEw: SKVIEw) {    // Set anchor point    self.anchorPoint = CGPointMake(0.5,0.5);    var startTime: NSTimeInterval    //Sart the timer    startTime = NSDate.timeIntervalSinceReferenceDate()    NSTimer.scheduledTimerWithTimeInterval(0.02,repeats: true)}

结果:456680125.54539第一次打印

解决方法 正如Martin在评论中所说,定时器的分辨率为50-100毫秒(0.05到0.1秒).尝试运行时间间隔短于此的计时器将无法提供可靠的结果.此外,计时器不是实时的.它们取决于它们所连接的运行循环,如果运行循环变忙,则计时器的触发会延迟.

不是每次计时器触发时都尝试递增计数器,而是在启动计时器时记录开始时间,然后进行一些数学计算以确定发生了多长时间:

var startTime: NSTimeInterval//Sart the timerstartTime = NSDate.timeIntervalSinceReferenceDate()NSTimer.scheduledTimerWithTimeInterval(0.02,repeats: true)//And your  timer method...func advanceTimer(timer: NSTimer){  //Total time since timer started,in seconds  self.time = NSDate.timeIntervalSinceReferenceDate() - startTime  //The rest of your code goes here }

编辑:

此代码的Swift 3版本如下所示:

(在测试项目中写为视图控制器)

class VIEwController: UIVIEwController {  weak var timer: Timer?  var startTime: Double = 0  var time: Double = 0  @IBOutlet weak var timeValueLabel: UILabel!  /*   When the vIEw controller first appears,record the time and start a timer  */  overrIDe func vIEwDIDAppear(_ animated: Bool) {    startTime = Date().timeIntervalSinceReferenceDate    timer = Timer.scheduledTimer(timeInterval: 0.05,selector: #selector(advanceTimer(timer:)),repeats: true)  }  //When the vIEw controller is about to disappear,invalIDate the timer  overrIDe func vIEwWilldisappear(_ animated: Bool) {    timer?.invalIDate()  }  func advanceTimer(timer: Timer) {    //Total time since timer started,in seconds    time = Date().timeIntervalSinceReferenceDate - startTime    //The rest of your code goes here    //Convert the time to a string with 2 decimal places    let timeString = String(format: "%.2f",time)    //display the time string to a label in our vIEw controller    timeValueLabel.text = timeString  }}
总结

以上是内存溢出为你收集整理的xcode – Swift计时器,以毫秒为单位全部内容,希望文章能够帮你解决xcode – Swift计时器,以毫秒为单位所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1098155.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存