UIView Animation of Label - Restarting Animation

Here is my little Visual Metronome which animates a flashing light. I made it for my wife who wanted a metronome for church that was silent with only a flashing light to get the correct tempo.

I just created a square label and animate the alpha value to make it look like it is flashing.

It works well but after I stop the Metronome using: self.flashingLightLabel.layer.removeAllAnimations()
Then I can never get the animation started again unless I close the app and restart the app.

The user is supposed to be able to change the “Beats Per Minute” in the textbox and press the GO button to start the animation with a different “withDuration” value. However, nothing happens. The app must be terminated and then restarted before it will work again. It seems like a simple problem but has been very hard for me to figure out:

import UIKit

class ViewController: UIViewController {
@IBOutlet var beatRateTextBox: UITextField!
@IBOutlet var flashingLightLabel: UILabel!

var beatsPerMinute: Double = 0.0

override func viewDidLoad() {
    flashingLightLabel.isHidden = true

func stopMetronome(){

@IBAction func metronomeSpeedChange(_ sender: Any) {

@IBAction func goButtonPressed(_ sender: Any) {
    flashingLightLabel.isHidden = false
    beatsPerMinute = 60/((beatRateTextBox.text! as NSString).doubleValue)
    UIView.animate(withDuration: beatsPerMinute,
                   animations: { self.flashingLightLabel.alpha = 0 },
        completion: nil)


try setting the alpha to 1 before the UIView.animate call:

flashingLightLabel.isHidden = false
flashingLightLabel.alpha = 1

Yes!! Thank You!! that was the answer to my problem.
(you can tell I am a novice!)
I appreciate the help, my wife will be happy with the app!

This topic was automatically closed after 166 days. New replies are no longer allowed.