简体   繁体   English

NSButton 无法使用 Xcode 12.2 正确旋转

[英]NSButton doesn't rotate correctly with Xcode 12.2

I have a square NSButton set using auto layout in a storyboard.我在故事板中使用自动布局设置了一个square NSButton The button is set as Image only and I use the NSRefreshTemplate system image.该按钮设置为Image only ,我使用NSRefreshTemplate系统图像。

This is my code to made it spin这是我让它旋转的代码

static BOOL _animate = NO;
- (void)animateRefreshButton:(BOOL)animate
{
    NSButton *btn = _refreshButton;
    [btn setWantsLayer:YES];
    
    // Set the anchor point of the refresh button
    CALayer *btnLayer = btn.layer;
    NSRect frame = btn.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
    
    _animate = animate;
    
    if (animate)
    {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.delegate = self;
        animation.fromValue = [NSNumber numberWithFloat:2 * M_PI];
        animation.toValue = [NSNumber numberWithFloat:0];
        animation.duration = 0.6; // Speed
        animation.repeatCount = 1;// INFINITY; // Repeat forever. Can be a finite number.
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        
        [btn.layer addAnimation:animation forKey:@"refreshbutton.rotation"];
    }
}

This has been working fine until I build with the new Xcode 12.2 beta on MacOS 11.0 .这一直工作正常,直到我在MacOS 11.0上使用新的Xcode 12.2 beta 进行构建。 Now the buttons spin doesn't seem to be central.现在按钮旋转似乎不是中心。

If I set the button Scaling to None it will spin centrally fine but the image is far too small.如果我将按钮Scaling设置为None它会在中心旋转,但图像太小了。 If I set the Scaling to Proportionately Up or Down so the image fills the button, it spins weird.如果我将Scaling Proportionately Up or Down设置为Proportionately Up or Down使图像填满按钮,它会旋转得很奇怪。

How can I fix this?我怎样才能解决这个问题?

在此处输入图片说明

Problem问题

On Catalina it still works even with Xcode 12 beta.在 Catalina 上,它仍然适用于 Xcode 12 beta。 Problem seems to be, that with macOS 11 Big Sur the button is no longer centrally aligned.问题似乎是,在 macOS 11 Big Sur 中,按钮不再居中对齐。

That can be tested like this:可以这样测试:

button.image = [NSImage imageNamed:NSImageNameRefreshTemplate];
[button.cell setBackgroundColor:NSColor.redColor];

大 sur vs 卡特琳娜

This is different on Catalina, where the button is centrally aligned.这在 Catalina 上有所不同,按钮居中对齐。 Maybe this is a bug in Big Sur that will be fixed in an upcoming version.也许这是 Big Sur 中的一个错误,将在即将发布的版本中修复。 For both variants I used Xcode 12.2 beta 2. So it related to the OS and not Xcode beta.对于这两个变体,我使用了 Xcode 12.2 beta 2。所以它与操作系统有关,而不是 Xcode beta。

Workaround解决方法

Draw a refresh icon in a centrally aligned way and use your custom icon, eg以居中对齐的方式绘制刷新图标并使用您的自定义图标,例如

button.image = [NSImage imageNamed:@"my_refresh_icon"];

Then the animation works as well on Catalina as on macOS Big Sur.然后动画在 Catalina 和 macOS Big Sur 上的效果一样好。

Test测试

动画片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM