JoeyTrl
JoeyTrl

Reputation: 35

swift - how use FIRE() in NSTimer

I need a cycle of 5 times of 2.5 seconds in a single cycle, however, that the pressure of a button bait from single cycle without interrupting the scheduling. I use method fire (with pressed button) for stoping a repeating timer without interrupting its regular firing schedule, but I don't know, subsequent cycles are shorter than the defined 2.5 seconds:

.......

@IBAction func buttonZeroPressed(sender: AnyObject) {

    timer.fire()
    ++addWin
}

......

timer = NSTimer.scheduledTimerWithTimeInterval(2.5, target: self,
        selector: Selector("levelOne"), userInfo:nil,repeats: true)

......

func levelOne() {

    level = 1
    count++
    changePhoto()
    if (count  >= 5) {
        timer.invalidate()
       }
}

After the 3 pressed button, the timer seems to be 2.5 seconds, and I do not understand why!

Upvotes: 1

Views: 3278

Answers (1)

Nate Cook
Nate Cook

Reputation: 93296

The fire() method is how you start a timer that's been created but not scheduled. You want to use invalidate() to stop the timer.

@IBAction func buttonZeroPressed(sender: AnyObject) {
    timer.invalidate()
    ++addWin
}

Your timer will start immediately since you call scheduledTimerWith.... If you wanted to create a timer without starting, you would use:

timer = NSTimer(timeInterval: 2.5, target: self, selector: Selector("levelOne"), userInfo:nil,repeats: true)

Upvotes: 1

Related Questions