3

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

damacri86
  • 295
  • 3
  • 13

3 Answers3

7

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);
    }];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
Aaron Hayman
  • 8,492
  • 2
  • 36
  • 63
  • I use the second solution. No re scaling working fine! Thanks :) – damacri86 Jul 03 '12 at 09:53
  • @Aaron Hayman - Can you please tell me how to shrink the view with animation . Thanks in advance. – Pankaj Gupta Dec 15 '16 at 12:49
  • @PankajGupta You just need to use the same math as I described in the answer. Basically, take the origin and subtract out half the difference between the new size and the old size. Set the view frame to the new origin + size inside the animation block. – Aaron Hayman Dec 16 '16 at 17:39
1

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

Trausti Thor
  • 3,722
  • 31
  • 41
0

try this

[UIView animateWithDuration:secs delay:1.0 options:option
                     animations:^{
                         yourview.transform = CGAffineTransformScale(view.transform, 100.0, 100.0);
                     }
                     completion:nil]; 
Madhumitha
  • 3,794
  • 8
  • 30
  • 45
  • This probably would work, but the view would not constantly re-render so if the animation was even the slightest bit long, it would be very poor quality. – pasawaya Jun 29 '12 at 14:20