CoreAnimation setFrameCenterRotation 无法正确动画
我在让 CoreAnimation 围绕其中心点旋转 NSView 时遇到了一些麻烦。我想要做的就是旋转 NSView,同时保持其中心点静态,以便对象看起来在原地旋转。
我选择执行此操作的方法涉及使用 NSView 中的 animator
属性。代码如下:
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:1.0];
[[view animator] setFrameCenterRotation:45.0];
[NSAnimationContext endGrouping];
其中view
是我试图旋转的NSView。这段代码的结果使对象突然向右移动一些,然后进行旋转。最终结果是正确的,但动画看起来不正确,因为它在开始时进行了移动。我假设这是因为 setFrameCenterRotation:
的工作方式是首先将框架原点移动到视图的中心点,然后进行旋转。
我在这里做错了什么?也许有更好的方法来围绕中心旋转 NSView 吗?多谢!
I'm having a bit of trouble with getting CoreAnimation to rotate an NSView about its center point. All I want to do is rotate an NSView while keeping its center point static so that the object appears to rotate in place.
The way I chose to do this involves the use of the animator
property in NSView. The code is as follows:
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:1.0];
[[view animator] setFrameCenterRotation:45.0];
[NSAnimationContext endGrouping];
Where view
is the NSView that I am trying to rotate. The result of this code makes the object abruptly shift to the right somewhat then does the rotation. The final result is correct, but the animation doesn't look right because of the movement it makes in the beginning. I'm assuming that this is because the way that setFrameCenterRotation:
works is that it shifts the frame origin to the view's center point first, then makes the rotation.
What am I doing wrong here? Is there a better way, perhaps, to rotate an NSView about the center? Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先确保 NSView 层支持(启用核心动画),否则动画代理将在没有 CA 的情况下进行动画处理。使用 -setWantsLayer:YES 或通过在 IB 中检查该视图的相应选项来执行此操作。
其次,将图层锚点设置为 0.5、0.5:
第三,如果您无法获得所需的内容,您可能需要直接图层交互,但首先尝试使用 AppKit API,因为它保留了鼠标事件等。
First make sure NSView layer backed (Core Animation enabled), animator proxy would otherwise animate without CA. Do this with -setWantsLayer:YES, or by checking the corresponding option for that view in IB.
Second, set the layer anchor point to 0.5, 0.5:
Third, you may need direct layer interaction if you can't get what you want, but first try with AppKit API's, as it preserves mouse events, etc.