[英]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];
這在 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.