Swift 3 - 按钮动画跳转到右侧

时间:2017-04-22 13:16:36

标签: ios swift animation

单击时,我已将此按钮设置为“弹跳”。它确实有效,但是一旦动画结束,它就跳到右边,然后回到它应该的位置。我认为最好是运行此代码(连接到按钮)以便自己查看。我正在iPad上测试我的应用程序。基本上,我想知道为什么它会在动画制作之后跳到他身边。这是我的代码:

@IBAction func randomiseButton(_ sender: Any) {
    if self.choices.isEmpty == true {
        let theButton = sender as! UIButton
        let bounds = theButton.bounds
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 10, options: .curveEaseInOut, animations: {
            theButton.bounds = CGRect(x: bounds.origin.x - 20, y: bounds.origin.y, width: bounds.size.width + 60, height: bounds.size.height)
        }) { (success:Bool) in
            if success {
                UIView.animate(withDuration: 0.5, animations: {
                    theButton.bounds = bounds
                })
            }
        }

当在第一个if语句之外检查一个数组,“choices”是否为空时,它也会发生

干杯马特

2 个答案:

答案 0 :(得分:0)

好的,就像问了这个问题后10分钟我搞乱了我的代码,我改变了最后一个动画

UIView.animate(withDuration: 0.5, 

UIView.animate(withDuration: 0,

不确定为什么,但这似乎可以解决问题!

答案 1 :(得分:0)

删除完成块。

UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 10, options: .curveEaseInOut, animations: {
    theButton.bounds = CGRect(x: bounds.origin.x - 20, y: bounds.origin.y, width: bounds.size.width + 60, height: bounds.size.height)
})

P.S。

代码:

UIView.animate(withDuration: 0,

表示您的动画将在0秒内执行。

相关问题