annaig hoinart
annaig hoinart

Reputation: 35

Swift button resizing animation

I'm trying to animate button resizing, but all I get is instant already resized view. What am i doing wrong?

p.s. sorry for my bad english

swift 5

 UIView.animate(withDuration: 10) {
            self.button.frame.size = CGSize(width: 195, height: 195)
            self.button.frame.size.width += 100
            self.button.frame.size.height -= 100
            self.button.center.y += 200
        }

first three lines inside curly brackets don't work as i expect, but the last one change the position of button as it has to

Upvotes: 0

Views: 2775

Answers (2)

annaig hoinart
annaig hoinart

Reputation: 35

Thanks to everyone who tried to help me, after some researches I decided to use

self.button.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)

CGAffineTransform, instead of changing bounds or frames. Now everything works fine

Upvotes: 3

Sami Sharafeddine
Sami Sharafeddine

Reputation: 410

You don't perform any arithmetics inside UIView.animate, you just set the final values of the constants that you want animated:

UIView.animate(withDuration: 10) {
    self.button.frame.size.width = 10 // The new width of self.button
}

Same concept applies for any frame/width/height constants or even completely new frames.

Upvotes: 1

Related Questions