Search code examples
swiftxcodeanimationuikitxcode11

How can I repeat chained animation in Swift?


I would like to animate the whole chain for forever.

UIView.animate(withDuration: 2.0, delay: 0.2, options: .curveEaseOut, animations: {
        self.circleImageView.transform = CGAffineTransform(scaleX: 9, y: 9)
    }) { (_) in
        UIView.animate(withDuration: 2.0, delay: 0.2, options: .curveEaseIn, animations: {
            self.circleImageView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        }, completion: nil)
    }

Solution

  • Wrap it up inside a method:

    func repeatingAnimation() {
        UIView.animate(withDuration: 2.0, delay: 0.2, options: .curveEaseOut, animations: {
            self.circleImageView.transform = CGAffineTransform(scaleX: 9, y: 9)
        }) { (_) in
            UIView.animate(withDuration: 2.0, delay: 0.2, options: .curveEaseIn, animations: {
                self.circleImageView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
            }, completion: { [weak self] _ in
                self?.repeatingAnimation()
            })
        }
    }