0
votes

I have a UIView that is a countdown clock, which I animate with UIView.animate. There is also a running timer that shows the time on a UILabel, when the timer is fired. When the game is in transition (during the count down), the UILabel stops changing. I've put a "print" statement into the timer function and it shows up as expected.

I've been searching for hints about maybe the UIVIew.animate freezes other views, but I see multiple apps moving views at the same time. Maybe I'm supposed to put things on different thread?

@objc func timerUpdate () {
        if gameIsRunning {
            let endTime = Date.init()
            let difference = endTime.timeIntervalSince(gameClock)
            let formattedString = String(format: "%3.1f",kDefaultGameClock - difference)
            timerLabel.text = "\(formattedString)"
        }
}


private func countDownFrom (x: Int) {
    if x != 0 && x > 0 {
        UIView.animate(withDuration: 1.0,
                       delay: 0.0,
                       usingSpringWithDamping: 0.5,
                       initialSpringVelocity:1.0,
                       options: .curveEaseOut,
                       animations: {
                        self.successLabel.text = "\(x)"
                        self.successLabel.transform = self.successLabel.transform.scaledBy(x: 4.0, y: 4.0)

        }, completion: {_ in
            self.successLabel.transform = self.successLabel.transform.scaledBy(x: 0.250, y: 0.250)
            if (x - 1 > 0) {
                self.countDownFrom(x: x - 1)
            } else {
                self.successLabel.text = kDefaultGoodGame
                self.successLabel.isHidden = true
                self.startTheGame()
                self.updateScreen()
            }
        })
    }
}
1
What does your timer look like to trigger timerUpdate? countDownFrom looks fine.Derek
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerUpdate), userInfo: nil, repeats: true)kejoki

1 Answers

1
votes

When I tried your code in an empty project, I was able to update both labels.

If timerLabel isn't being updated, it could be a thread issue. UI updates should be on the main thread.

DispatchQueue.main.async {
    self.timerLabel.text = "\(formattedString)"
}

For what it's worth, here is what I have working in an empty project.

@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var successLabel: UILabel!

let kDefaultGameClock = 30.0
var gameIsRunning = false
var gameClock = Date()

override func viewDidLoad() {
    super.viewDidLoad()
    start()
}

func start() {
    gameIsRunning = true
    countDownFrom(x: 30)
    Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerUpdate), userInfo: nil, repeats: true)
}

@objc func timerUpdate() {
    if gameIsRunning {
        let endTime = Date.init()
        let difference = endTime.timeIntervalSince(gameClock)
        let formattedString = String(format: "%3.1f", kDefaultGameClock - difference)
        timerLabel.text = "\(formattedString)" // Could be a main thread issue here?
    }
}

private func countDownFrom(x: Int) {
    if x != 0 && x > 0 {
        UIView.animate(withDuration: 1.0,
                       delay: 0.0,
                       usingSpringWithDamping: 0.5,
                       initialSpringVelocity:1.0,
                       options: .curveEaseOut,
                       animations: {
                        self.successLabel.text = "\(x)"
                        self.successLabel.transform = self.successLabel.transform.scaledBy(x: 4.0, y: 4.0)

        }, completion: {_ in
            self.successLabel.transform = self.successLabel.transform.scaledBy(x: 0.250, y: 0.250)
            if (x - 1 > 0) {
                self.countDownFrom(x: x - 1)
            } else {
                self.successLabel.text = ""
                self.successLabel.isHidden = true
                //self.startTheGame()
                //self.updateScreen()
            }
        })
    }
}

A little off topic, but you might want to declare your Timer as a variable to invalidate it once you're done updating the timerLabel.