这是我想出的解决方案。现在,计时器结束时的警报只是用“完成”打印到控制台。非常感谢用户 Prawn,因为我在这个解决方案中使用了他的一些代码。
import WatchKit
import Foundation
var myTimer : NSTimer?
var elapsedTime : NSTimeInterval = 0.0
var startTime = NSDate()
var duration : NSTimeInterval = 4.0 //Arbitrary 4 seconds to test timer.
class InterfaceController: WKInterfaceController {
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration))
}
override func willActivate() {
super.willActivate()
}
@IBOutlet var WKTimer: WKInterfaceTimer! //The WatchKit timer the user sees
@IBAction func startButton() { //Start button
NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: ("timerDone"), userInfo: nil, repeats: false)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration ))
WKTimer.start()
}
//Reset button resets the timer back to the original time set.
@IBAction func resetButton() {
WKTimer.stop()
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration))
}
func timerDone() {
print("Done")
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}