我使用以下代码将图像缩放得稍大,然后恢复到其原始大小:
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];
问题是图像在动画过程中稍微向上和向右移动,而不是停留在原处。
如何在动画播放过程中将图像保持在原位?
谢谢。
最佳答案
这是因为您使用的方法 -setFrameSize:
不会改变 View 的原点(即 Mac 上的左下角),因此 View 实际上会变大并且向右。将 -setFrame:
与动画代理一起使用,或将 View 层上的变换与 CABasicAnimation 结合使用。将动画线性地应用到 View 的坐标。
关于objective-c - 更改帧大小时 NSImageView 的动画未保持在原位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15725757/