UIView.animation is doing my animation reversed

2019-07-26 04:41发布

问题:

This is my code:

  UIView.animate(withDuration: 2.5, animations: {
                        animatingScoreCircle.alpha = 0.0
                        let screenSize = UIScreen.main.bounds
                        let screenHeight = screenSize.height * 0.10
                        animatingScoreCircle.center.y += screenHeight
                    })

What I want, is that the image view will move up, from the original center.y position with 10% of the current screen size. But now, it goes from the final position (original centery + 10% of the screen height) I want the image to be at, to the original center y. So it is reversed. So again it should be:

step 1: Image should be at the normal state, which is center.y step 2: Image should slowly go the the final state, which is center.y + 10

But now it goes:

step 1: Image starts at center.y + 10 step 2: Image goes slowly to the original center.y
If I change += to -=, it does the same thing but now the image is coming from below to the original center.y position.

I already tried this:

                let originalCenterYForScoreCircle = animatingScoreCircle.center.y
                let screenSize = UIScreen.main.bounds
                let screenHeight = screenSize.height * 0.10
                animatingScoreCircle.center.y = screenHeight
                UIView.animate(withDuration: 2.5, animations: {
                    animatingScoreCircle.alpha = 0.0
                    animatingScoreCircle.center.y = originalCenterYForScoreCircle
                })

But it has the same results. How to fix this code? Thank you.

Edit: I am using autolayout, and I am making a card game. When the user taps an UIButton, I want to use an animation to make it look better. So this is the animation I am calling. This animation will fire when the user taps a button. The function automatically adapts the constrains from that button. After that, I want to use the animation as stated above. This is more code:

let animatingScoreCircle = UIImageView()
                    animatingScoreCircle.image = UIImage(named: "emptyPicture")
                    animatingScoreCircle.layer.cornerRadius = animatingScoreCircle.frame.size.width / 2
                    animatingScoreCircle.clipsToBounds = true
                    animatingScoreCircle.translatesAutoresizingMaskIntoConstraints = false
                    view.addSubview(animatingScoreCircle)
                    let horizontalConstraint = animatingScoreCircle.centerXAnchor.constraint(equalTo: sender.centerXAnchor)
                    let verticalConstraint = animatingScoreCircle.centerYAnchor.constraint(equalTo: sender.centerYAnchor)
                    let heigthContraint = animatingScoreCircle.heightAnchor.constraint(equalTo: sender.heightAnchor, multiplier: 1.0)
                    let widthConstraint = animatingScoreCircle.widthAnchor.constraint(equalTo: sender.widthAnchor, multiplier: 1.0)
                    NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, heigthContraint, widthConstraint])
                    let originalCenterYForScoreCircle = animatingScoreCircle.center.y
                    let screenSize = UIScreen.main.bounds
                    let screenHeight = screenSize.height * 0.10
                    animatingScoreCircle.center.y = screenHeight
                    UIView.animate(withDuration: 2.5, animations: {
                        animatingScoreCircle.alpha = 0.0
                        animatingScoreCircle.center.y = originalCenterYForScoreCircle
                    })

                }

This gets called when user taps a button.