NSTimer 不在 WatchOS2 中进行调度
NSTimer not Scheduling in WatchOS2
我想制作一个心跳动画来匹配 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];
}
你试过在你想启动定时器的时候派发到主队列吗?
根据watchOS 2.0文档,HKAnchoredObjectQuery的updateHandler是从后台线程调用的。
the query executes this update handler on a background queue.
试试这个。
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)
})
})
}
这看起来对吗?它达到了预期的效果,除了在采集新样本时会粘住。
我想制作一个心跳动画来匹配 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];
}
你试过在你想启动定时器的时候派发到主队列吗?
根据watchOS 2.0文档,HKAnchoredObjectQuery的updateHandler是从后台线程调用的。
the query executes this update handler on a background queue.
试试这个。
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)
})
})
}
这看起来对吗?它达到了预期的效果,除了在采集新样本时会粘住。