我正在swift 2中製作一個應用程序,其中有兩個計時器。 10秒後,我想要另一個計時器加快速度。我試圖做這樣的,但它沒有工作(我試圖改變VAR time
1):如何在X秒後更改NSTimer的NSTimeInterval?
@IBOutlet var displayTimeLabel: UILabel!
var startTimer = NSTimeInterval()
var timer = NSTimer()
var timer2:NSTimer = NSTimer()
var time = 2.0
@IBAction func Start(sender: UIButton) {
if !timer2.valid {
timer2 = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true)
startTimer = NSDate.timeIntervalSinceReferenceDate()
}
timer = NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: "timer:", userInfo: nil, repeats: true)
}
func timer(timer: NSTimer){
//code
}
func updateTime() {
if displayTimeLabel.text >= "00:10.00"{
print("00:10.00") //works
time = 1 // trying to execute code after 10 seconds(doesn't work)
}
let currentTime = NSDate.timeIntervalSinceReferenceDate()
var elapsedTime: NSTimeInterval = currentTime - startTimer
let minutes = UInt8(elapsedTime/60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
let fraction = UInt8(elapsedTime * 100)
let strMinutes = String(format: "%02d", minutes)
let strSeconds = String(format: "%02d", seconds)
let strFraction = String(format: "%02d", fraction)
displayTimeLabel.text = "\(strMinutes):\(strSeconds).\(strFraction)"
}
如果我寫在func timer
打印(時間),在十秒鐘後它會打印1而不是2,但它仍然每兩秒重複一次。請幫忙。要清楚,我希望能夠在10秒後將time
更改爲1而不是2。我也不想讓計時器失效。定時器也在repeat = true
。在此先感謝...安東
您如何知道'time = 1'不工作?應該發生什麼不是? –
@聰明錯誤func計時器中的代碼不是每1秒重複一次,而是每兩秒鐘重複一次。 –
@聰明錯誤如果我在函數計時器中寫入print(time),十秒後它將打印1而不是2,但它仍然每兩秒重複一次。 –