gpt4 book ai didi

objective-c - Core Animation : set anchorPoint on 10. 8 围绕其中心旋转图层

转载 作者:太空狗 更新时间:2023-10-30 03:25:57 24 4
gpt4 key购买 nike

注意:这是针对 OS X 而非 iOS 上的 Cocoa 应用。

我有一个层支持的 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)。层然后围绕它的中心旋转,但显然跳到左下角是 Not Acceptable 。

所以,经过大量阅读,我在 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 的调用,而是将该 anchor 设置为 NSView 的原点 (0,0)。

我的问题是:我该如何解决这个问题?为图层设置 anchor 的“适当的NSView覆盖方法”是什么(我在NSView上找不到这样的方法)。归根结底,我只想让我的按钮无限期地围绕其中心点旋转。

最佳答案

我没有在 NSView 上看到任何直接“覆盖”anchorPoint 的方法。

我在 the 10.8 release notes 中看到了什么,除了你引用的,是这样的:

The anchorPoint is also always set to be (0,0), …

anchorPoint 控制图层的哪个点位于 super 图层坐标系中的位置NSViewself.layer.anchorPoint 设置为 (0,0),这意味着图层的左下角位于 self.layer.position .

当您将anchorPoint 设置为(0.5,0.5) 时,这意味着图层的中心应该位于图层的位置。由于您没有修改 position,这具有将层向下和向左移动的效果,如您所见。

anchorPoint 为 (0.5,0.5) 时,您需要计算图层所处的 position,如下所示:

CGRect frame = _refreshButton.layer.frame;
CGPoint center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
_refreshButton.layer.position = center;
_refreshButton.layer.anchorPoint = CGPointMake(0.5, 0.5);

关于objective-c - Core Animation : set anchorPoint on 10. 8 围绕其中心旋转图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com