WatchKit 上的计时器不会每秒触发一次 [英] Timer not firing every second on WatchKit

查看:18
本文介绍了WatchKit 上的计时器不会每秒触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个计时器不是每秒触发一次,当我检查日志和 UI 时,它似乎每 3-4 秒触发一次.

func startTimer() {打印(启动计时器")timer = Timer.scheduledTimer(timeInterval: 1,目标:自己,选择器:#selector(timerDidFire),用户信息:无,重复:正确)}func timerDidFire(定时器:定时器){打印(计时器")更新标签()}

这只是由于缺乏功能而会在 Watch 上发生的事情,还是我的代码有问题?

如果需要,这里是日志:

0.03960001468658453.994041025638587.9750190377235411.9065310359001

为了澄清起见,我每秒更新的是锻炼计时器,因此它需要每隔一秒更新一次.

解决方案

考虑使用 WKInterfaceTimer 标签代替用于显示时间的标签:

<块引用>

WKInterfaceTimer 对象是一种特殊类型的标签,它显示倒计时或倒计时计时器.使用定时器对象来配置时间量和计时器文本的外观.你几时开始计时器,WatchKit 会自动更新显示的文本用户的 Apple Watch,而无需与您的扩展程序进行进一步交互.Apple 文档.

WatchOS 将负责保持最新状态.操作系统为您处理标签,但您必须跟踪经过的时间:您只需要设置一个 NSDate 即可(参见下面的示例).

示例代码.

在您的 WKInterfaceController 子类中:

<前>//连接一个对计时器的引用.@IBOutlet var trainingTimer:WKInterfaceTimer!//跟踪锻炼开始的时间.var 锻炼开始时间:NSDate?功能开始锻炼(){//向上计数使用 0.0 或更少,否则计时器向下计数.trainingTimer.setDate(NSDate(timeIntervalSinceNow: 0.0))锻炼定时器.start()self.workoutStartTime = NSDate()}功能停止锻炼(){锻炼定时器.stop()}func 锻炼SecondsElapsed() -> NSTimeInterval?{//如果定时器还没有启动,则返回 nil守卫让开始时间 = self.workoutStartTime else {返回零}//过去日期的时间间隔是负数,所以//乘以-1得到经过的时间.返回 -1.0 * self.startTime.timeIntervalSinceNow}

综合博客条目:此处.

This timer isn't firing every second, when I check the log and UI it seems to be firing every 3-4 seconds.

func startTimer() {
    print("start timer")
    timer = Timer.scheduledTimer(timeInterval: 1,
                                 target: self,
                                 selector: #selector(timerDidFire),
                                 userInfo: nil,
                                 repeats: true)
}

func timerDidFire(timer: Timer) {
    print("timer")
    updateLabels()
}

Is this just something that is going to happen on the Watch due to lack of capabilities, or is there something wrong in my code?

Here is the log if needed:

0.0396000146865845
3.99404102563858
7.97501903772354
11.9065310359001

EDIT:

And for clarification, what I'm updating every second is the workout timer, so it needs to be updated every second that ticks by.

解决方案

Consider using a WKInterfaceTimer label in place of the label that you are using to show the timing:

A WKInterfaceTimer object is a special type of label that displays a countdown or count-up timer. Use a timer object to configure the amount of time and the appearance of the timer text. When you start the timer, WatchKit updates the displayed text automatically on the user’s Apple Watch without further interactions from your extension. Apple Docs.

WatchOS will then take responsibility for keeping this up-to-date. The OS handles the label for you, but you have to keep track of the elapsed time: you just need to set an NSDate to do that (see example below).

Sample Code.

In your WKInterfaceController subclass:


    // Hook up a reference to the timer.
    @IBOutlet var workoutTimer: WKInterfaceTimer!

    // Keep track of the time the workout started.
    var workoutStartTime: NSDate?

    func startWorkout() {
        // To count up use 0.0 or less, otherwise the timer counts down.
        workoutTimer.setDate(NSDate(timeIntervalSinceNow: 0.0))
        workoutTimer.start()
        self.workoutStartTime = NSDate()
    }

    func stopWorkout() {
        workoutTimer.stop()
    }

    func workoutSecondsElapsed() -> NSTimeInterval? {
        // If the timer hasn't been started then return nil
        guard let startTime = self.workoutStartTime else {
            return nil
        }
        // Time intervals from past dates are negative, so
        // multiply by -1 to get the elapsed time.
        return -1.0 * self.startTime.timeIntervalSinceNow
    }

Comprehensive blog entry: here.

这篇关于WatchKit 上的计时器不会每秒触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆