【问题标题】:Repeating animation restarting button's positions重复动画重新启动按钮的位置
【发布时间】:2018-04-06 14:08:48
【问题描述】:

我是 Swift 编码的新手。我正在处理动画,但遇到了问题。

UIView.animate(withDuration: TimeInterval(waitTime), delay: 0, options: [.allowUserInteraction, .allowAnimatedContent, .curveLinear, .repeat], animations: {

    UIView.setAnimationRepeatCount(Float(self.clickAccuracy))

    // Animate tiles going down
    self.tileButton1.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
    self.tileButton2.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
    self.tileButton3.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
    self.tileButton4.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
    self.tileButton5.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)

})

在这里,我想重复一个 UIButton 向下移动的动画。 (我必须以较小的块重复动画,因为我希望能够单击移动按钮)。但是,按钮会尽可能地下降,但不会从该位置再次重复,而是重置。这会导致无限闪烁。

我所需要的只是让按钮向下移动,而不是重新开始到原来的位置,这样我就可以有一个流畅的动画。谢谢!

这是一个视频 https://i.imgflip.com/27uzl3.gif

【问题讨论】:

  • @rmaddy 我不希望动画反转

标签: ios swift uiviewanimation


【解决方案1】:

事实证明,我原来的做法似乎行不通。所以我把它改成了这样:

var numberOfIterations = 0

func repeatAnimation() {

    UIView.animate(withDuration: TimeInterval(waitTime), delay: 0, options: [.allowUserInteraction, .allowAnimatedContent, .curveLinear, .curveLinear], animations: {

        //UIView.setAnimationRepeatCount(Float(self.clickAccuracy))

        // Animate tiles going down
        self.tileButton1.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
        self.tileButton2.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
        self.tileButton3.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
        self.tileButton4.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)
        self.tileButton5.center.y += self.tileButtonExample.frame.height / CGFloat(self.clickAccuracy)

    }, completion: { (Finished) in

        numberOfIterations += 1

        // Repeat
        if numberOfIterations != self.clickAccuracy {

            self.repeatAnimation()

        }

    })

}

当@rmaddy 给我这个答案时,我们忘记删除setAnimationRepeatCount(已注释掉)。为了让它重复一定的次数,我在完成块中放了一个if 语句。

我还去掉了动画选项中的.repeat

感谢大家的帮助!

【讨论】:

  • 很高兴再次重命名为repeatAnimation
猜你喜欢
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
相关资源
最近更新 更多