Timm Kent
Timm Kent

Reputation: 1268

FadeIn FadeOut Animation Cascade - is there a better way?

At the moment I do this to animate a Countdown. Is there a more clever way to do this? Is it clever to use a protocol to inform a delegate that the animation has completed?

You can download the code I'm currently working on at:

https://github.com/madeTK/TKAnimations.git

Thanks for any comments.

override func viewDidLoad() {
    super.viewDidLoad()
    self.threeTwoOneCounter()
}

func threeTwoOneCounter() {
    let fadeInTime:Double = 1.0
    let fadeOutTime:Double = 0.5
    let label = UILabel(frame: CGRectMake(self.view.bounds.width/2-50,100,100,100))
    label.text = "3"
    label.font = UIFont(name: "Arial", size: 50)
    label.textColor = UIColor.blackColor()
    label.textAlignment=NSTextAlignment.Center
    label.alpha = 0.0
    self.view.addSubview(label)

    UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
        label.alpha = 1.0
    }) { (Bool) -> Void in

        UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
            label.alpha = 0.0
        }, completion: { (Bool) -> Void in
            label.text = "2"
            UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
                label.alpha = 1.0
            }, completion: { (Bool) -> Void in
                UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
                    label.alpha = 0.0
                }, completion: { (Bool) -> Void in
                    label.text = "1"
                    UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
                        label.alpha=1.0
                    }, completion: { (BOOl) -> Void in
                        UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
                            label.alpha = 0.0
                        }, completion: { (Bool) -> Void in
                            println("done.maybe calling delegate now")
                        })
                    })
                })
            })

        })
    }

Upvotes: 1

Views: 959

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

You can refactor the the code like this.

func threeTwoOneCounter() {

    let label = UILabel(frame: CGRectMake(self.view.bounds.width/2-50,100,100,100))
    label.text = "3"
    label.font = UIFont(name: "Arial", size: 50)
    label.textColor = UIColor.blackColor()
    label.textAlignment=NSTextAlignment.Center
    label.alpha = 0.0
    self.view.addSubview(label)

    animateCountdownLabel(label, startValue: 3) { () -> () in
        println("done.maybe calling delegate now")
    }
}

func animateCountdownLabel(label : UILabel,startValue : Int,completed : () -> ())
{
    if (startValue <= 0)
    {
        completed()
        return
    }
    let fadeInTime:Double = 1.0
    let fadeOutTime:Double = 0.5
    label.text = "\(startValue)"

    UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
        label.alpha = 1.0
        }) { (Bool) -> Void in

            UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
                label.alpha = 0.0
                }, completion: { (Bool) -> Void in

                    self.animateCountdownLabel(label, startValue: startValue - 1,completed: completed)

            })
    }
}

The animateCountdownLabel() function is called recursively till the countdown becomes 0.

Upvotes: 1

Related Questions