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() {
super.viewDidLoad()
flashingLightLabel.isHidden = true
}
func stopMetronome(){
self.flashingLightLabel.layer.removeAllAnimations()
}
@IBAction func metronomeSpeedChange(_ sender: Any) {
stopMetronome()
}
@IBAction func goButtonPressed(_ sender: Any) {
flashingLightLabel.isHidden = false
beatsPerMinute = 60/((beatRateTextBox.text! as NSString).doubleValue)
UIView.animate(withDuration: beatsPerMinute,
delay:0.0,
options:[.repeat],
animations: { self.flashingLightLabel.alpha = 0 },
completion: nil)
}
}