繁体   English   中英

NSButton 无法使用 Xcode 12.2 正确旋转

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

我在故事板中使用自动布局设置了一个square NSButton 该按钮设置为Image only ,我使用NSRefreshTemplate系统图像。

这是我让它旋转的代码

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"];
    }
}

这一直工作正常,直到我在MacOS 11.0上使用新的Xcode 12.2 beta 进行构建。 现在按钮旋转似乎不是中心。

如果我将按钮Scaling设置为None它会在中心旋转,但图像太小了。 如果我将Scaling Proportionately Up or Down设置为Proportionately Up or Down使图像填满按钮,它会旋转得很奇怪。

我怎样才能解决这个问题?

在此处输入图片说明

问题

在 Catalina 上,它仍然适用于 Xcode 12 beta。 问题似乎是,在 macOS 11 Big Sur 中,按钮不再居中对齐。

可以这样测试:

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

大 sur vs 卡特琳娜

这在 Catalina 上有所不同,按钮居中对齐。 也许这是 Big Sur 中的一个错误,将在即将发布的版本中修复。 对于这两个变体,我使用了 Xcode 12.2 beta 2。所以它与操作系统有关,而不是 Xcode beta。

解决方法

以居中对齐的方式绘制刷新图标并使用您的自定义图标,例如

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

然后动画在 Catalina 和 macOS Big Sur 上的效果一样好。

测试

动画片

暂无
暂无

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

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