skip to Main Content

I have a UIImageView that I wish to animate at the click of a button. But before the animation, I set its image. However, this image-setting messes up the animation. When I comment out the image-set, the animation works as expected.

        var busFrame = self.newBusView.frame

        let sourceFrame = self.buttonA.superview?.convert(self.buttonA.frame.origin, to: nil)

        busFrame.origin.y = sourceFrame!.y
        busFrame.origin.x = sourceFrame!.x

        self.newBusView.frame = busFrame

        //the following line messes up the animation
//        self.newBusView.image = UIImage(named: "back_blue")
        
        UIView.animate(withDuration: 2.0, delay: 0.3, options: .curveEaseOut, animations: {

            busFrame = self.newBusView.frame
            let desstinationFrame = self.buttonD.superview?.convert(self.buttonD.frame.origin, to: nil)
            busFrame.origin.y = desstinationFrame!.y
            busFrame.origin.x = desstinationFrame!.x
            self.newBusView.frame = busFrame
        }, completion: { finished in
            print("Done!")
            //same thing happening here
            //self.newBusView.image = UIImage(named: "Background 1")
        })

Also note that the buttons are in separate Stack views (hence the need for superview?.convert)

UPDATE:

So it was noticed that whenever I have a button.setImage() the positions go wrong (Even if the button has nothing to do with the animation). But instead of setImage, if I use button.imageView.image = something, then it has no side effects on animations. What is happening?

Expected:
enter image description here

Actual:

enter image description here

2

Answers


  1. As we do not know the "expected" animation, I could not repeat the problem that you have. When I set same image again in the commented line, I get the same animation.

    It may be about the area that the image covers. New image’s ratio may be different than the default, therefore you see different animation although view’s animation is same. You actually animate the view. Hence, you should focus on frames.

    Login or Signup to reply.
  2. I believe that as @musakokcen mentioned your image have different size.
    Try to use:

    yourImageView.layer.masksToBounds = yes
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search