如何在Swift中多次为UIView的翻转设置动画

时间:2017-07-30 00:06:32

标签: ios swift animation uiview transition

我想在UIButton多次翻转图像。即旋转门型动画。我不在乎它是否会翻转一定次数或持续一段时间

现在我可以使用以下代码翻转一次:

UIView.transition(with: thisButton, duration: 0.2, options: .transitionFlipFromTop, animations: {
    thisButton.setImage(myButtonImage, for: .normal)
}, completion: nil)

2 个答案:

答案 0 :(得分:2)

尝试将volume: nocopy: true 添加到您的选项中

.repeat

希望这有帮助

答案 1 :(得分:0)

Reinier的回答可以让它永远重复。

为了让它在一段时间后停止,我添加了thisButton.layer.removeAllAnimations()

所以下面的代码一起工作:

UIView.transition(with: thisButton, duration: 0.1, options: [.transitionFlipFromTop,.repeat], animations: {
    thisButton.setImage(myButtonImage, for: .normal)
}, completion: nil)

let delayTime = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: delayTime) {
    thisButton.layer.removeAllAnimations()
}