0

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

Anton O.
  • 653
  • 7
  • 27
  • How do you know `time = 1` is not working? What should be happening that isn't? – Craig Siemens Jan 03 '16 at 04:45
  • @ Clever Error The code in func timer is not repeating every 1 second but instead every two seconds. – Anton O. Jan 03 '16 at 04:47
  • @ Clever Error 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. – Anton O. Jan 03 '16 at 04:50

2 Answers2

3

A few of things.

First, you can't change the time interval of a repeating NSTimer - you need to invalidate the first timer and schedule a new one for the new interval.

Second a >= comparison on a string won't work for what you are trying to achieve.

Finally, you have fallen into the same bad habit as many Swift programmers and initialised your timer variables needlessly only to subsequently throw those timers away. You should use either an optional or an implicitly unwrapped optional

@IBOutlet var displayTimeLabel: UILabel!
var startTime:NSDate?
var timer : NSTimer?
var timer2: NSTimer?  
var time = 2.0

@IBAction func Start(sender: UIButton) {
    if (self.timer2 == nil) {
        self.time=2.0 
        self.timer2 = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true)
        self.startTime = NSDate()
        self.timer = NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: "timer:", userInfo: nil, repeats: true)
    }
}

func timer(timer: NSTimer){
//code
}
func updateTime() {        
    let currentTime = NSDate.timeIntervalSinceNow()
    var elapsedTime = -self.startTime!.timeIntervalSinceNow()
    if (elapsedTime >10.0 && self.time==2.0) {
       timer.invalidate()
       self.time=1.0
       self.timer = NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: "timer:", userInfo: nil, repeats: true)
    }

    let minutes = UInt8(elapsedTime / 60.0)
    elapsedTime -= (NSTimeInterval(minutes) * 60)
    let seconds = UInt8(elapsedTime)
    elapsedTime -= NSTimeInterval(seconds)
    let fraction = UInt8(elapsedTime * 100)
    displayTimeLabel.text = String(format: "%02d:%02d.%02d", minutes,seconds,fraction)
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Whats the difference between what I have now concerning variables and your code(making the variables optional)? Does it change the performance of the app? – Anton O. Jan 03 '16 at 05:26
  • There might be a performance improvement but it is more a question of style - you are creating an NSTimer (which isn't scheduled or set up) just to satisfy Swift's requirement that non-optional variables are initialised but you are just going to throw this timer away later. If a variable won't really be initialised until later then best to use an optional to ensure that your code handles this properly. Optionals in Swift allow you to avoid a whole class of bugs to do with use of initialised variables; initialising them in the way you were subverts this whole benefit – Paulw11 Jan 03 '16 at 05:32
2

Just setting time = 1 will not modify timer.

The timeInterval of a timer can not be changed. Your only options are to create a new timer or start it with a timeInterval of 1 and just do nothing every other time timer() is called until the 10 sec have passed.

Craig Siemens
  • 12,942
  • 1
  • 34
  • 51