1

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.

NoKey
  • 129
  • 11
  • 32
  • fix your grammar errors I can't understand *I want to image to end* – mfaani Feb 08 '17 at 19:31
  • I did some changes, do you understand it now? – NoKey Feb 08 '17 at 19:36
  • I understand it now, not sure why this happens :D – mfaani Feb 08 '17 at 19:56
  • Are you using autolayout? when are you calling this code? can you include more of your code so we can see the flow of what's going on? – gmogames Feb 08 '17 at 19:57
  • @gmogames see edit! – NoKey Feb 08 '17 at 20:09
  • **1.** I don't understand how your `sender.centerXAnchor` are working. `sender` is not of `UIView` type until you cast it to it. – mfaani Feb 08 '17 at 20:40
  • sender is the UIButton, I take the centerX and centerY from that button, since the image needs to be at that place aswell – NoKey Feb 08 '17 at 20:43
  • did you get it to work? I don't know why but if you put your animation inside `DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {//place animation here}` it works as intended **or** even if you simply use a `UIView` it works with or without the delay block. I tried working on it for like 3-4 hrs to figure out why there is a difference of behavior but had no luck. I will later post a detailed question on SO and perhaps we'll get some answers. I think it's just a silly mistake – mfaani Feb 10 '17 at 20:51
  • I just asked it here: http://stackoverflow.com/questions/42168704/why-do-uiview-uiimageview-animate-differently/42168798#42168798 – mfaani Feb 10 '17 at 21:22

0 Answers0