gpt4 book ai didi

ios - WatchOS2中使用NSTimeInterval触发通知

转载 作者:行者123 更新时间:2023-11-30 13:57:39 24 4
gpt4 key购买 nike

我正在尝试在 Apple Watch (OS2) 上制作番茄钟应用程序。我想在倒计时完成后触发通知,第一步我尝试在控制台中打印一些单词,但它仍然不起作用。如何使用 NSTimeInterval 来获取执行此操作的剩余时间?

  import WatchKit
import Foundation


class InterfaceController: WKInterfaceController {

let countdown:NSTimeInterval = 1501

var timerRunning = false

@IBOutlet var pauseButton: WKInterfaceButton!

@IBOutlet var timer: WKInterfaceTimer!

override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}

override func willActivate() {
super.willActivate()
}

override func didDeactivate() {
super.didDeactivate()
}

@IBAction func startPomodoro() {

let date = NSDate(timeIntervalSinceNow: countdown)
timer.setDate(date)
timer.start();
WKInterfaceDevice.currentDevice().playHaptic(.Start)
WKInterfaceDevice.currentDevice().playHaptic(.Start)
}


@IBAction func resetPomodoroTimer() {
timer.stop()
let resetCountdown:NSTimeInterval = 1501
let date = NSDate(timeIntervalSinceNow: resetCountdown)

timer.setDate(date)
WKInterfaceDevice.currentDevice().playHaptic(.Retry)
WKInterfaceDevice.currentDevice().playHaptic(.Retry)
}


@IBAction func pausePomodoro() {
timer.stop()

if !timerRunning{
pauseButton.setTitle("Restart")
}
WKInterfaceDevice.currentDevice().playHaptic(.Stop)
WKInterfaceDevice.currentDevice().playHaptic(.Stop)
}

func showNotification(){

if countdown < 1490
{
print("Notification")
WKInterfaceDevice.currentDevice().playHaptic(.Success)
WKInterfaceDevice.currentDevice().playHaptic(.Success)
}
}
}

最佳答案

要在 WKInterfaceTimer 完成后触发通知,您必须添加具有相同时间间隔的 NSTimer。您同时启动两者,当 NSTimer 触发时,您知道 WKInterfaceTimer 也完成了。恕我直言,这不是一个非常优雅的解决方案,但苹果的文档中建议了它,所以显然没有其他方法可以做到这一点。

如果您想添加暂停/重新启动功能,您必须跟踪用户点击“暂停”时的剩余时间并停止两个计时器。当用户再次启动计时器时,您将两个计时器设置为剩余时间并启动它们。

这是一个具有暂停/重新启动功能的工作示例(它有一个连接到 button 导出的 WKInterfaceButton 和 didPressButton: 操作:

enum TimerState {
case Idle, Running, Paused, Finished
}

class InterfaceController: WKInterfaceController {

let countdownDuration: NSTimeInterval = 10
var remainingDuration: NSTimeInterval = 10
var timer: NSTimer?
var timerState = TimerState.Idle

@IBOutlet var interfaceTimer: WKInterfaceTimer!
@IBOutlet var button: WKInterfaceButton!

@IBAction func didPressButton() {

switch timerState {
case .Idle:
startTimer(remainingDuration: countdownDuration)
case .Running:
let fireDate = timer!.fireDate
remainingDuration = fireDate.timeIntervalSinceDate(NSDate())
interfaceTimer.stop()
timer?.invalidate()
button.setTitle("Continue")
timerState = .Paused
case .Paused:
startTimer(remainingDuration: remainingDuration)
case .Finished:
break
}
}

func startTimer(remainingDuration duration:NSTimeInterval) {
interfaceTimer.setDate(NSDate(timeIntervalSinceNow: duration))
interfaceTimer.start()
timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: Selector("timerDidFire:"), userInfo: nil, repeats: false)
button.setTitle("Pause")
timerState = .Running
}

func timerDidFire(timer: NSTimer) {
interfaceTimer.stop()
timerState = .Finished
WKInterfaceDevice.currentDevice().playHaptic(.Success)
}
}

关于ios - WatchOS2中使用NSTimeInterval触发通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com