CoreAnimation setFrameCenterRotation没有适当地动画 [英] CoreAnimation setFrameCenterRotation Doesn't Animate Properly

查看:344
本文介绍了CoreAnimation setFrameCenterRotation没有适当地动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点麻烦,让CoreAnimation旋转NSView关于它的中心点。所有我想做的是旋转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.

我选择这样做的方式涉及使用NSView中的 animator 属性。代码如下:

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];

其中查看是NSView试图旋转。该代码的结果使对象突然向右移位,然后进行旋转。最终的结果是正确的,但动画看起来不正确,因为它在开始的运动。我假设这是因为 setFrameCenterRotation:的工作方式是首先将框架起点移动到视图的中心点,然后进行旋转。

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.

我在这里做错了什么?有没有更好的方法,也许,旋转一个NSView关于中心?非常感谢!

What am I doing wrong here? Is there a better way, perhaps, to rotate an NSView about the center? Thanks a lot!

推荐答案

首先确保NSView图层支持(启用核心动画)使用-setWantsLayer:YES或者通过在IB中检查该视图的相应选项来执行此操作。

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.

其次,将图层锚点设置为0.5,0.5:

Second, set the layer anchor point to 0.5, 0.5:

view.layer.anchorPoint = CGPointMake(0.5,0.5);

第三,如果您无法获得所需内容,使用AppKit API,因为它保留鼠标事件等。

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.

这篇关于CoreAnimation setFrameCenterRotation没有适当地动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆