swiftios8xcode6.4

How to blink an object continuously till the app gets killed?


In my storyboard,I have a button which i want to blink it for infinite times till the program gets killed.Here is what I have done so far,This code makes the button to animate only once.

@IBOutlet weak var blinker: UIButton!

 func Blink(){
        blinker.alpha = 0.0
        UIButton.animateWithDuration(1, animations: {
            self.blinker.alpha = 1.0
            }, completion: {
                (value: Bool) in
                println(">>> Animation done.")
        })

    }

Any help would be appreciated....


Solution

  • If you are not going to use CABasicAnimation or other variant of CAAnimation the best way is to do it recursively. For example:

    func Blink(){
            blinker.alpha = 0.0
            UIButton.animateWithDuration(1, animations: {
                self.blinker.alpha = 1.0
                }, completion: {
                    (value: Bool) in
                    println(">>> Animation done.")
                    Blink()
            })
    
        }
    

    this way when the animation is finished your are calling it again, and again, and again...

    Update

    Please refer to Glenn's answer for the proper way to handle it. This solution may cause stack overflow issues.