如何在Swift中重复链接动画?

时间:2019-10-12 13:13:14

标签: swift xcode animation uikit xcode11

我想让整个链永远动起来。

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)
    }

2 个答案:

答案 0 :(得分:1)

您可以将两个动画包装在关键帧动画中。然后,您可以.repeat

UIView.animateKeyframes(withDuration: 4.4, delay: 0, options: .repeat, animations: { 
    UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 2.0/4.4) { 
        self.circleImageView.transform = .init(scaleX: 9, y: 9)
    }
    UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 2.0/4.4) { 
        self.circleImageView.transform = .init(scaleX: 0.01, y: 0.01)
    }
})

答案 1 :(得分:0)

将其包装在方法内部:

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()
        })
    }
}