Programmatically reveal a UIView

2019-05-09 22:35发布

问题:

I am attempting to reveal (through animation) a UIView. Specifically I want to show the center portion of the view and then slowly reveal the outer edges of it (sort of like pulling back a curtain).

My first attempt was to simply set the bounds rect to be smaller and animate it to be the full size of the view's frame, but this did not have the desired effect since by changing the bounds I was also changing the frame.

If what I am trying to do does not sound possible (at least not in a simple manner), at least I would like to be able to have is some way to make the subviews of the main view stationary relative to the screen, NOT their parent view, as the parent resizes (this would give a similar effect).

Any ideas?

Thank you,

-Matt

回答1:

It definitely is possible. What you need to do is

  • For the view you're animating, setAutoresizesSubviews:NO and setClipsToBounds:YES.
  • Set the view's bounds (NOT the frame) to a rect with zero size and origin at the center point of the rect you want the view to occupy when it is fully revealed (in the view's own coordinate system). In other words, startBounds.origin.x should equal half of endBounds.size.width and similarly for y.
  • Position the view by setting its center (in the parent view's coordinate system).
  • In an animation block, change the view's bounds to zero origin and full size.
  • In the animation's completion block, you probably want to setAutoresizesSubviews:YES again.

You may also need to set the view's autoresizing mask to be fully flexible (springs but no struts), depending on what other layout gets triggered as you resize.



回答2:

Sounds like you want to change its clipping. A cheap (code-wise) way to do that would be to insert the view into a parent view (with autoresizing set to center it), set the parent to clip its children and then animate the parent's frame.