注意:本问题针对的是Mac OS X上的Cocoa应用程序,而不是iOS。
我有一个基于图层的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上找不到这样的方法)。归根结底,我只想让我的按钮无限制地围绕其中心点旋转。