0

Hello i want to make animation of a frame change. But i don't find nothing similar on the internet. I know how to animate the frame change, but the tricky part is that for example i have a square and i want it to shrink to the center. So how do i change frame to achieve that?

Do i have to change the frame width and height and also the origin of the frame, or maybe there is some simpler solution?

Any help or references on how to do it would be very appreciated! Thanks in advance!

Lukas
  • 2,515
  • 4
  • 28
  • 37

4 Answers4

3
CGRect previousRect = view.layer.bounds;
CGRect newRect = previousRect;
newRect.size = size; // In your case define the new size (CGSize size)
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];

animation.fromValue = [NSValue valueWithCGRect:previousRect];
animation.toValue = [NSValue valueWithCGRect:newRect];


[view.layer addAnimation:animation forKey:@"bounds"];
Bhupendra
  • 2,525
  • 18
  • 20
1

Set the frame before beginning the animation and inside the animation block change the frame to point to center of the actual frame. You need to set height/width as well as origin for that. That's how it is normally done.

iDev
  • 23,310
  • 7
  • 60
  • 85
1

You need use a center of view:

CGPoint *center = view.center;
view.frame = CRectMake (0,0,100,200); //set new frame;
view.center = center;
// here you should start animation
NeverBe
  • 5,213
  • 2
  • 25
  • 39
  • I tried it like that, but the jump of origin x and y ar very visible, mayuur answer works the way i wanted. – Lukas Oct 12 '12 at 08:21
1

If you need the View Shrinking Code, here it is.

    myView.transform=CGAffineTransformMakeScale(0.1, 0.1);
    [UIView beginAnimations:@"animationExpand" context:NULL];
    [UIView setAnimationDuration:0.4];
    myView.transform=CGAffineTransformMakeScale(1, 1);
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

This is for making the View larger. If you want to make view shrink smaller then just replace the transform lines with each other.

Hope it helps! :)

mayuur
  • 4,736
  • 4
  • 30
  • 65
  • Super! i know it should be easy just didn't know what to look for! I just use block animation because there's a lot of it, but anyway, thank you very much! – Lukas Oct 12 '12 at 08:00