如何让NSButton从Cocoa的中心点顺时针旋转?
How to rotate NSButton in clockwise direction from its center point in Cocoa?
我想在我的 mac 应用程序中为 NSButton 设置动画。
我按照下面的 link 进行了相同的操作,但下面的代码适用于 iOS,不适用于 Mac。
How to rotate a flat object around its center in perspective view?
我想达到的目标:
基本上,我想从图像的中心点顺时针旋转图像,就像上面的代码适用于 iOS 一样。
当前有问题:
在 Mac 应用中,图像是从其角点而不是中心旋转的。
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
animation.duration = 1.0;
animation.repeatCount = INFINITY;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[btnScan setWantsLayer:YES];
[btnScan.layer addAnimation:animation forKey:@"transform.rotation.z"];
发生旋转的点受图层的 position
和 anchorPoint
属性影响,请参阅 Anchor Points Affect Geometric Manipulations。这些属性的默认值似乎与文档不匹配,至少在 macOS 10.11 和您使用的任何版本下都是如此。
尝试通过添加如下四行来设置它们:
[btnScan setWantsLayer:YES];
CALayer *btnLayer = btnScan.layer;
NSRect frame = btnLayer.frame;
btnLayer.position = NSMakePoint(NSMidX(frame), NSMidY(frame)); // position to centre of frame
btnLayer.anchorPoint = NSMakePoint(0.5, 0.5); // anchor point to centre - specified in unit coordinates
[btnScan.layer addAnimation:animation forKey:@"transform.rotation.z"];
设置这些属性的顺序实际上并不重要,结果应该是图像围绕按钮的中心点旋转。
我想在我的 mac 应用程序中为 NSButton 设置动画。
我按照下面的 link 进行了相同的操作,但下面的代码适用于 iOS,不适用于 Mac。
How to rotate a flat object around its center in perspective view?
我想达到的目标:
基本上,我想从图像的中心点顺时针旋转图像,就像上面的代码适用于 iOS 一样。
当前有问题:
在 Mac 应用中,图像是从其角点而不是中心旋转的。
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
animation.duration = 1.0;
animation.repeatCount = INFINITY;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[btnScan setWantsLayer:YES];
[btnScan.layer addAnimation:animation forKey:@"transform.rotation.z"];
发生旋转的点受图层的 position
和 anchorPoint
属性影响,请参阅 Anchor Points Affect Geometric Manipulations。这些属性的默认值似乎与文档不匹配,至少在 macOS 10.11 和您使用的任何版本下都是如此。
尝试通过添加如下四行来设置它们:
[btnScan setWantsLayer:YES];
CALayer *btnLayer = btnScan.layer;
NSRect frame = btnLayer.frame;
btnLayer.position = NSMakePoint(NSMidX(frame), NSMidY(frame)); // position to centre of frame
btnLayer.anchorPoint = NSMakePoint(0.5, 0.5); // anchor point to centre - specified in unit coordinates
[btnScan.layer addAnimation:animation forKey:@"transform.rotation.z"];
设置这些属性的顺序实际上并不重要,结果应该是图像围绕按钮的中心点旋转。