Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift NSTimer未在WatchOS2中调度_Swift_Nstimer_Watchos 2 - Fatal编程技术网

Swift NSTimer未在WatchOS2中调度

Swift NSTimer未在WatchOS2中调度,swift,nstimer,watchos-2,Swift,Nstimer,Watchos 2,我正准备制作一个心跳动画,以匹配WatchOS2中从HealthKit中采集的心率。我似乎找不到根据最近的样本更新计时器间隔的方法 经过一点研究,建议使用使计时器失效和重新调度的方法;但是下面的代码似乎无法完成这项工作 class InterfaceController: WKInterfaceController { var timer: NSTimer? private func updateHeartRate(rate: Int) { ... heartBeatI

我正准备制作一个心跳动画,以匹配WatchOS2中从HealthKit中采集的心率。我似乎找不到根据最近的样本更新计时器间隔的方法

经过一点研究,建议使用使计时器失效和重新调度的方法;但是下面的代码似乎无法完成这项工作

class InterfaceController: WKInterfaceController {

var timer: NSTimer?


private func updateHeartRate(rate: Int) {
    ...


    heartBeatIntensity = NSTimeInterval(0.0166 * Float(rate))
    print(heartBeatIntensity)

    if let timer = timer {
        timer.invalidate()
    }

    timer = NSTimer.scheduledTimerWithTimeInterval(heartBeatIntensity, target:self, selector: Selector("updatesByTimer"), userInfo: nil, repeats: true)
}

这对我有用。我正在定义一个变量

NSTimer *timer;
当我想开始时:

timer = [NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:@selector(timeUpdated) userInfo:nil repeats:YES];
那我就宣布它无效

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];

    [timer invalidate];
}

当您想启动计时器时,是否尝试调度到主队列?

这对我来说很有效。我正在定义一个变量

NSTimer *timer;
当我想开始时:

timer = [NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:@selector(timeUpdated) userInfo:nil repeats:YES];
那我就宣布它无效

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];

    [timer invalidate];
}

当您想启动计时器时,是否尝试调度到主队列?

根据watchOS 2.0文档,从后台线程调用HKAnchoredObjectQuery的updateHandler

查询在后台队列上执行此更新处理程序

试试这个

private func updateHeartRate(rate: Int) {
    ...

    dispatch_async(dispatch_get_main_queue()) {
        //your code
    }
}

根据watchOS 2.0文档,HKAnchoredObjectQuery的updateHandler是从后台线程调用的

查询在后台队列上执行此更新处理程序

试试这个

private func updateHeartRate(rate: Int) {
    ...

    dispatch_async(dispatch_get_main_queue()) {
        //your code
    }
}

好的,在这里,但是它很不稳定

计时器被调度

    dispatch_async(dispatch_get_main_queue()){
        self.heartBeatIntensity = NSTimeInterval(60 / Float(rate))
        if let timer = self.timer {
            timer.invalidate()
        }
        self.timer = NSTimer.scheduledTimerWithTimeInterval(self.heartBeatIntensity, target:self, selector: Selector("updatesByTimer"), userInfo: nil, repeats: true)
    }
然后,我必须为animateWithDuration添加对WKInterfaceController的扩展,以添加一个完成块()。之后,我就这样结束了

func updatesByTimer(){
    WKInterfaceDevice.currentDevice().playHaptic(.Click)

    animateWithDuration(heartBeatIntensity/2,
        animations:
        {
            self.beatingHeart.setHeight(55.0)
            self.beatingHeart.setWidth(55.0)
        },
        completion: {
            self.animateWithDuration(self.heartBeatIntensity/2, animations: {
                self.beatingHeart.setHeight(85.0)
                self.beatingHeart.setWidth(85.0)
            })
        })
}

这看起来对吗?它达到了预期的效果,除了在采集新样本时使用stick之外。

好的,在这里,但它非常不稳定

计时器被调度

    dispatch_async(dispatch_get_main_queue()){
        self.heartBeatIntensity = NSTimeInterval(60 / Float(rate))
        if let timer = self.timer {
            timer.invalidate()
        }
        self.timer = NSTimer.scheduledTimerWithTimeInterval(self.heartBeatIntensity, target:self, selector: Selector("updatesByTimer"), userInfo: nil, repeats: true)
    }
然后,我必须为animateWithDuration添加对WKInterfaceController的扩展,以添加一个完成块()。之后,我就这样结束了

func updatesByTimer(){
    WKInterfaceDevice.currentDevice().playHaptic(.Click)

    animateWithDuration(heartBeatIntensity/2,
        animations:
        {
            self.beatingHeart.setHeight(55.0)
            self.beatingHeart.setWidth(55.0)
        },
        completion: {
            self.animateWithDuration(self.heartBeatIntensity/2, animations: {
                self.beatingHeart.setHeight(85.0)
                self.beatingHeart.setWidth(85.0)
            })
        })
}
这看起来对吗?它具有预期的效果,但在采集新样本时会出现粘滞现象