get to an UIView frame a zoom animation

2019-05-10 14:12发布

问题:

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

回答1:

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);
    }];


回答2:

To make a view zoom out, while keeping it's own center is easy but you need to use transform. here is a code. Just add and connect a view, named it zoomerView.

In this example, I just add this to the main view touches ended.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint center = zoomerView.center;
    [UIView animateWithDuration:1 animations:^{
        zoomerView.transform = CGAffineTransformScale(zoomerView.transform, 1.2, 1.2);
    }];
}

Hope this helps you



回答3:

try this

[UIView animateWithDuration:secs delay:1.0 options:option
                     animations:^{
                         yourview.transform = CGAffineTransformScale(view.transform, 100.0, 100.0);
                     }
                     completion:nil];