0

in my playground I need to repeat this func

func changeButton () {
        UIView.animate(withDuration: 1.0, delay: 1.0, animations: { () -> Void in
            button.titleLabel?.alpha = 0.0
        }) { (_finished: Bool) -> Void in
            button.setTitle("Text", for: .normal)
            UIView.animate(withDuration: 1.0, delay: 0.0, animations: { () -> Void in
                button.titleLabel?.alpha = 1.0
            }) { (_finished: Bool) -> Void in
                UIView.animate(withDuration: 1.0, delay: 1.0, animations: { () -> Void in
                    button.titleLabel?.alpha = 0.0
                }) { (_finished: Bool) -> Void in
                    button.setTitle("Cacca", for: .normal)
                    UIView.animate(withDuration: 1.0, delay: 0.0, animations: { () -> Void in
                        button.titleLabel?.alpha = 1.0
                    })
                }
            }

I tried to repeat the func with this while loop but doesn't work, someone can help me?

repeat{
        changeButton()
    } while(true)
3
  • You can create a Group Animation and make that animation repeat the number of times that you need even forever Commented Mar 26, 2018 at 8:53
  • @ReinierMelian CAAnimationGroup is a good idea, but it refers to Core Animation Framework... Commented Mar 26, 2018 at 8:55
  • @ReinierMelian "I think that is not a good solution" could you please mention why do you think that? Commented Mar 26, 2018 at 9:06

1 Answer 1

1

Trying to repeat the series of animation using repeat-while loop won't work as expected, because you need to repeat the whole series of animation after its done, not while is currently performed.

If you are aiming to repeat the whole process (all the series of the animations), calling the function itself (recursion) leads to the desired result:

func changeButton () {
    UIView.animate(withDuration: 1.0, delay: 1.0, animations: { () -> Void in
        button.titleLabel?.alpha = 0.0
    }) { (_finished: Bool) -> Void in
        button.setTitle("Text", for: .normal)
        UIView.animate(withDuration: 1.0, delay: 0.0, animations: { () -> Void in
            button.titleLabel?.alpha = 1.0
        }) { (_finished: Bool) -> Void in
            UIView.animate(withDuration: 1.0, delay: 1.0, animations: { () -> Void in
                button.titleLabel?.alpha = 0.0
            }) { (_finished: Bool) -> Void in
                button.setTitle("Cacca", for: .normal)
                UIView.animate(withDuration: 1.0, delay: 0.0, animations: { () -> Void in
                    button.titleLabel?.alpha = 1.0
                })

                button.setTitle("Cacca", for: .normal)
                UIView.animate(withDuration: 1.0, delay: 0.0, animations: { () -> Void in
                    button.titleLabel?.alpha = 1.0
                }) { (_finished: Bool) -> Void in
                    self.changeButton()
                }
            }
        }
    }
}

What I did is: I just let the last animation to contains a completion handler and recall the function changeButton() itself.

Obviously, based on the above code the series of animation goes forever, so you might want to do a logic for stopping it...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.