get to an UIView frame a zoom animation

damacri86 picture damacri86 · Jun 29, 2012 · Viewed 7.5k times · Source

I am trying to add one UIView with a transition style zoom animation like this:

     ^
     |
 <---.--->
     |
     v

The view start with a little frame (centerParent.x, centerParent.y,20, 20) and then change to (centerParent.x, centerParent.y,300, 300).

But when I change the frame I have to change it since the postion (0.0) of the view, not for it's center point. Does anyone know how to do it?

Thanks

Answer

Aaron Hayman picture Aaron Hayman · Jun 29, 2012

With the methodology you're currently using (not using a transform but simply resizing the view) you just need to reset the origin or center of the view). The easiest is the center:

 view.frame = CGRectMake(0, 0, 20, 20);
    CGPoint center = view.center;
    [UIView animateWithDuration: 1.0f animations:^{
        view.frame = CGRectMake(0, 0, 300, 300);
        view.center = center;
    }];

You can also reset the origin by moving subtracting 1/2 the difference of the size change from the x and y, but this is more math:

view.frame = CGRectMake(0, 0, 20, 20);
    CGPoint origin = view.frame.origin.
    [UIView animateWithDuration: 1.0f animations:^{
        origin.x -= (300 - 20) / 2;
        origin.y -= (300 - 20) / 2;
        view.frame = CGRectMake(origin.x, origin.y, 300, 300);
    }];