简体   繁体   中英

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. 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

Cheers Matt

Ok so like 10 minutes after asking this question I was messing around with my code and I changed the last animation

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

The code:

UIView.animate(withDuration: 0,

means that your animation will be carried out during 0 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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