gpt4 book ai didi

swift - 当 WKInterfaceTimer 达到 0 时打印到控制台

转载 作者:行者123 更新时间:2023-11-28 07:03:19 26 4
gpt4 key购买 nike

我想看到我的程序知道计时器何时结束。我在 Xcode 7 Beta 3 (7A152u) 中使用 WKInterfaceTimer。当计数器正在倒计时时,“Tick Tock”打印到控制台。但是当它达到 0 时,“Timer Done”不会打印。

    @IBOutlet var myTimer: WKInterfaceTimer!

@IBAction func startButton() {
myTimer.start()
myTimer.setDate(NSDate(timeIntervalSinceNow: 4)) // Arbitrary 4 second coundown.

// Impliment an alert.
if myTimer == 0 {
print("Timer Done")

} else {
print("Tick Tock")

}
}

最佳答案

这是我想出的解决方案。现在,计时器结束时的警报只是用“完成”打印到控制台。非常感谢用户 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()
}

}

关于swift - 当 WKInterfaceTimer 达到 0 时打印到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31504162/

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