How to animate the width and height of a UIView in Xcode?

前端 未结 7 659
温柔的废话
温柔的废话 2021-01-30 23:27

I have this subview I want to add to my main view, but make it expand from the origin. I read some of the Apple documentation but I don\'t understand where I am making mistake.

7条回答
  •  一向
    一向 (楼主)
    2021-01-30 23:39

    To make a view "grow" into place, don't animate the frame. Animate the transform.

    Load your subview from the nib. Set its transform to a scale of 0:

    view.transform = CGAffineTransformMakeScale(0,0);
    

    Then add it to your superview.

    Inside the animation block, set the transform to identity:

    view.transform = CGAffineTransformIdentity;
    

    And the view will grow to normal size. You may need to fiddle with the anchor point to make it grow from the right point.

    You can also change the frame within the block, but to move a view only I prefer to change the center property, you shouldn't try to set the frame if you also have a transform.

    For bonus points, you can also animate to a slightly bigger than 1.0 scale, then animate back to the identity transform in a chained animation from the completion block. This makes the view "pop" out of the screen like an alert view.

提交回复
热议问题