【问题标题】:How do I animate a flip of a UIView multiple times in Swift如何在 Swift 中多次为 UIView 的翻转设置动画
【发布时间】:2017-07-30 00:06:32
【问题描述】:

我想多次翻转UIButton 上的图像。即旋转门式动画。我不在乎它是否翻转了一定次数或一段时间

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

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

【问题讨论】:

  • 你也可以设置重复次数来控制它!

标签: ios swift animation uiview transition


【解决方案1】:

尝试将.repeat 添加到您的选项中

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

希望对你有帮助

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      相关资源
      最近更新 更多