Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 核心动画:将anchorPoint设置为10.8以围绕层的中心旋转层_Objective C_Macos_Cocoa_Core Animation_Core Graphics - Fatal编程技术网

Objective c 核心动画:将anchorPoint设置为10.8以围绕层的中心旋转层

Objective c 核心动画:将anchorPoint设置为10.8以围绕层的中心旋转层,objective-c,macos,cocoa,core-animation,core-graphics,Objective C,Macos,Cocoa,Core Animation,Core Graphics,NB:这是针对OS X上的Cocoa应用程序,而不是iOS。 我有一个层备份的NSButton(NSView的子类)。我想做的是使用核心动画旋转按钮。我正在使用以下代码执行此操作: CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; a.fromValue = [NSNumber numberWithFloat:0]; a.toValue = [NSNumber number

NB:这是针对OS X上的Cocoa应用程序,而不是iOS。

我有一个层备份的NSButton(NSView的子类)。我想做的是使用核心动画旋转按钮。我正在使用以下代码执行此操作:

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覆盖方法”来设置层的锚点(我在NSView上找不到这样的方法)。在一天结束时,我只想让我的按钮围绕其中心点无限旋转

我在
NSView
上没有看到任何方法是
anchorPoint
的直接“掩护”

除了你所引用的,我在书中看到的是:

锚点也始终设置为(0,0)

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

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

当层的
锚点
为(0.5,0.5)时,需要计算层的
位置
,如下所示:

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

我遇到了与Swift中的@Bryan完全相同的问题:在动画过程中,对象会从其原始位置跳开。以下是macOS脉冲按钮的代码:

  let frame : CGRect = startButton.layer!.frame
  let center : CGPoint = CGPoint(x: frame.midX, y: frame.midY)
  startButton.layer?.position = center;
  startButton.layer?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        
  let pulse = CASpringAnimation(keyPath: "transform.scale")
  pulse.duration = 0.2
  pulse.fromValue = 0.95
  pulse.toValue = 1.0
  pulse.autoreverses = true
  pulse.repeatCount = 10
  pulse.initialVelocity = 0.5
  pulse.damping = 1.0
        
  startButton.layer?.add(pulse, forKey: "pulse")

完美的你知道的;我看到了position属性,但没有尝试,因为我读到上面的说明,上面说AppKit将控制它。谢谢你的解决方案!你知道为什么旋转M_PI/2第一次是逆时针旋转,之后是顺时针旋转吗?我得看看你的代码。你应该提出你自己的问题。给你: