0

I'm using the following code to scale an image slightly larger and then back to its original size:

float width = image.image.size.width;
float newWidth = width * 1.05;
float height = image.image.size.height;
float newHeight = height * 1.05;

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:.7f];
NSAnimationContext.currentContext.completionHandler = ^{
   [NSAnimationContext beginGrouping];
   [[NSAnimationContext currentContext] setDuration:.7f];
   [[image animator] setFrameSize:NSMakeSize(width, height)];
   [NSAnimationContext endGrouping];
};

[[image animator] setFrameSize:NSMakeSize(newWidth, newHeight)];
[NSAnimationContext endGrouping];

The problem is that the image moves slightly up and to the right during the animation instead of staying in place.

How can I keep the image in place during the animation?

Thank you.

codeman
  • 8,868
  • 13
  • 50
  • 79

1 Answers1

1

It's because the method you're using, -setFrameSize:, does not alter the view's origin, (which is the lower left hand corner on a Mac), so the view literally grows up and to the right. Use -setFrame: with the animator proxy, or use a transform on the view's layer in conjunction with a CABasicAnimation to apply the animation to the view's coordinates linearly.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • Can you show me an example of setFrame with the animator proxy? – codeman Mar 31 '13 at 03:06
  • It's just a bit of [math](http://stackoverflow.com/questions/11879643/calculating-new-origin-of-insetted-cgrect-after-its-size-changes). What about is is confusing? – CodaFi Mar 31 '13 at 03:08
  • Tried this: [image animator] setFrame:NSMakeRect(image.frame.origin.x, image.frame.origin.y, newWidth, newHeight)]; But has the same effect. – codeman Mar 31 '13 at 03:11
  • Exactly, because you still aren't transforming the origin coordinates. Multiply them by 1.05 as well. – CodaFi Mar 31 '13 at 03:14
  • Wow, I'm tired. Sorry about that. Thanks for the help. – codeman Mar 31 '13 at 03:16