简体   繁体   English

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

[英]Swift 3 - Button Animation Jumps to the right

I have made this button animate to "bounce" when clicked. 点击后,我将此按钮设置为“反弹”。 It does work but once the animation has finished it jumps to the right and then goes back to where it should be. 它确实有效,但是一旦动画结束,它就跳到右边,然后回到它应该的位置。 I think it's best if you run this code (hooked up to a button) to see for yourself. 我认为最好是运行此代码(连接到按钮)以便自己查看。 I am testing my app on my iPad. 我正在iPad上测试我的应用程序。 Basically, I want to know why it's jumping to he right after animating. 基本上,我想知道为什么它会在动画制作之后跳到他身边。 Here's my code: 这是我的代码:

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

It also occurs when outside of the first if statement which checks if an array, "choices" is empty 当在第一个if语句之外检查数组“choices”是否为空时,也会发生这种情况

Cheers Matt 干杯马特

Ok so like 10 minutes after asking this question I was messing around with my code and I changed the last animation 好吧就像问了这个问题后10分钟我搞乱了我的代码,我改变了最后一个动画

UIView.animate(withDuration: 0.5, 

to

UIView.animate(withDuration: 0,

Not sure why but this seemed to do the trick! 不知道为什么,但这似乎做了伎俩!

Remove your completion block. 删除完成块。

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

PS PS

The code: 编码:

UIView.animate(withDuration: 0,

means that your animation will be carried out during 0 seconds. 意味着您的动画将在0秒内执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM