Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

swift - Setting the image on UIImageView changes the animation

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...