注意:这是针对OS X上的Cocoa应用程序,而不是iOS。核心动画:在10.8上设置anchorPoint以围绕其中心旋转一层
我有一个层支持NSButton(NSView的子类)。我想要做的是使用Core Animation旋转该按钮。我使用下面的代码来做到这一点:
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
a.fromValue = [NSNumber numberWithFloat:0];
a.toValue = [NSNumber numberWithFloat:-M_PI*2];
[_refreshButton.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
a.duration = 1.8; // seconds
a.repeatCount = HUGE_VAL;
[_refreshButton.layer addAnimation:a forKey:nil];
这工作,但它运行时,层跳下,并向左以便其中心点是在一个NSView,起源点,这(0,0)的左下角。然后该层围绕其中心旋转,但显然跳到较低的左角是不可接受的。
所以,大量的阅读之后,我发现这条线在10.8 API的版本说明:
On 10.8, AppKit will control the following properties on a CALayer
(both when "layer-hosted" or "layer-backed"): geometryFlipped, bounds,
frame (implied), position, anchorPoint, transform, shadow*, hidden,
filters, and compositingFilter. Use the appropriate NSView cover methods
to change these properties.
这意味着,AppKit的被“忽略”我呼吁在上面的代码-setAnchorPoint,取而代之的,是将该锚点设置为NSView的原点(0,0)。
我的问题是:我该如何解决这个问题?什么是“合适的NSView覆盖方法”来设置图层的anchorPoint(我无法在NSView上找到这样的方法)。在一天结束时,我只想让我的按钮无限期地围绕其中心点旋转。
完美!你懂;我看到了位置属性,但没有尝试,因为我读了上面的说明,说AppKit将控制它。感谢您的解决方案! – Bryan 2013-02-13 01:15:26
任何想法为什么旋转M_PI/2会第一次逆时针旋转,然后顺时针旋转? – greg 2014-07-08 04:23:30
我将不得不看你的代码。你应该发布你自己的问题。 – 2014-07-08 05:36:05