Ios 使用NSTimeInterval在WatchOS2中触发通知
我正在尝试在苹果手表(OS2)上制作一个pomodoro应用程序。我想在倒计时结束后触发一个通知,在第一步中,我试图在控制台中打印一些单词,但它仍然不起作用。如何使用Ios 使用NSTimeInterval在WatchOS2中触发通知,ios,swift,countdown,watchos-2,nstimeinterval,Ios,Swift,Countdown,Watchos 2,Nstimeinterval,我正在尝试在苹果手表(OS2)上制作一个pomodoro应用程序。我想在倒计时结束后触发一个通知,在第一步中,我试图在控制台中打印一些单词,但它仍然不起作用。如何使用NSTimeInterval来获得剩余的时间 import WatchKit import Foundation class InterfaceController: WKInterfaceController { let countdown:NSTimeInterval = 1501 var timerRunn
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)
}
}
}
导入WatchKit
进口基金会
类InterfaceController:WKInterfaceController{
让我们倒计时:NSTimeInterval=1501
var timerRunning=false
@IBOutlet var pauseButton:WKInterfaceButton!
@IBOutlet var定时器:WKInterfaceTimer!
重写func awakeWithContext(上下文:AnyObject?){
super.awakeWithContext(context)
}
重写func willActivate(){
super.willActivate()
}
重写func diddactivate(){
超级
}
@iAction func startPomodoro(){
let date=NSDate(timeIntervalSinceNow:倒计时)
timer.setDate(日期)
timer.start();
WKInterfaceDevice.currentDevice().Play触觉(.Start)
WKInterfaceDevice.currentDevice().Play触觉(.Start)
}
@iAction func ResetPomOrotimer(){
计时器停止()
让我们重新开始倒计时:NSTimeInterval=1501
let date=NSDate(timeIntervalSinceNow:reset倒计时)
timer.setDate(日期)
WKInterfaceDevice.currentDevice().Play触觉(.Retry)
WKInterfaceDevice.currentDevice().Play触觉(.Retry)
}
@iAction函数pausePomodoro(){
计时器停止()
如果!计时{
pauseButton.setTitle(“重新启动”)
}
WKInterfaceDevice.currentDevice().Play触觉(.Stop)
WKInterfaceDevice.currentDevice().Play触觉(.Stop)
}
func showNotification(){
如果倒计时<1490
{
打印(“通知”)
WKInterfaceDevice.currentDevice().Play触觉(.Success)
WKInterfaceDevice.currentDevice().Play触觉(.Success)
}
}
}
要在WKInterfaceTimer
完成后触发通知,您必须添加具有相同时间间隔的NSTimer
。您同时启动这两个程序,并且当NSTimer启动时,您知道WKInterfaceTimer也完成了。IMHO这不是一个非常优雅的解决方案,但在苹果的文档中有建议,所以显然没有其他方法可以做到这一点
如果要添加暂停/重启功能,则必须在用户点击“暂停”并停止两个计时器时跟踪剩余时间。当用户再次启动计时器时,您可以将两个计时器设置为剩余时间并启动它们
下面是一个具有暂停/重启功能的工作示例(它有一个连接到按钮
出口的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)
}
}