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.

4

1 回答 1

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.

于 2013-03-31T02:51:59.133 回答