简体   繁体   中英

Countdown timer in UILabel

I've created an app, a countdown timer, which counts down from xxx sec to 0. This works well.

Now I want to have a precounter.

Something like this:

First counter from 15 to 0 and then my second counter from 120 to 0.

These 2 counters shall use the same UILabel.

Here is what I've done so far:

var timerCount = 10
var timerRunning = false
var timer = NSTimer()
var audioPlayer:AVAudioPlayer?

// Code for the Sound - Start

func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer  {
    let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
    let url = NSURL.fileURLWithPath(path!)

    do {
        try audioPlayer = AVAudioPlayer(contentsOfURL: url)
    } catch {
        print("NO AUDIO PLAYER")
    }

    return audioPlayer!
}

// Code for Sound - End

// INFO LABEL
@IBOutlet weak var infoLabel: UILabel!
// INFO LABEL END

weak var timerLabel: UILabel!

func Counting(){

    timerCount -= 1
    timerLabel.text = "\(timerCount)"
    if timerCount == 0{
        timer.invalidate()
        timerRunning = false
        timerCount = 10
        timerLabel.text = "0"
        timerLabel.backgroundColor = UIColor.redColor()

    }
    if timerCount == 5{

        let backMusic = setupAudioPlayerWithFile("start", type: "wav")
        backMusic.play()

        timerLabel.backgroundColor = UIColor.yellowColor()

    }
    if timerCount == 10{
        timerLabel.backgroundColor = UIColor.redColor()
        timer.invalidate()
        timerRunning = false
        infoLabel.text = "Timer stopped"
    }

}

@IBAction func startButton(sender: UIButton) {

    let backMusic = setupAudioPlayerWithFile("start", type: "wav")
    backMusic.play()


    if timerRunning == false{

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
       timerRunning = true
    }

    timerLabel.backgroundColor = UIColor.greenColor()
    infoLabel.text = "Timer started"

}

You can do it simpler with GCD:

func counting() {
    timeCount--
    timerLabel.text = "\(timerCount)"
    if timeCount == 0 {
        timerRunning = false
        timerLabel.backgroundColor = UIColor.redColor()
        infoLabel.text = "Timer stopped"
        return
    } else if timerCount == 5 {
        let backMusic = setupAudioPlayerWithFile("start", type: "wav")
        backMusic.play()
        timerLabel.backgroundColor = UIColor.yellowColor()
    }
    startNextCount()
}

func startNextCount() {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), self.counting)
}

@IBAction func startButton(sender: UIButton) {
    if timerCount > 0 {
        return
    }
    timerCount = 10
    let backMusic = setupAudioPlayerWithFile("start", type: "wav")
    backMusic.play()
    timerLabel.backgroundColor = UIColor.greenColor()
    infoLabel.text = "Timer started"
    startNextCount()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM