I'm making an app in swift 2 where there is two timers. After 10 seconds I would like another timer to go faster. I have tried doing it like this but it didn't work (I'm trying to change the var time
to 1):
@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)"
}
If I write print(time) in func timer
, after ten seconds it will print 1 instead of 2 but it will still be repeating every two second. Please help. To be clear, I want to be able to change time
to 1 instead of 2 after ten seconds. I also don't want to invalidate the timers. The timers are also on repeat = true
. Thanks in advance... Anton