2

私はMacアプリに取り組んでいます。NSButton を下に移動させる簡単なアニメーションを実行しようとしています。アニメーションは非常にうまく機能しますが、それを行うと、何らかの理由で NSButton の背景色が消えます。これが私のコードです:

// Tell the view to create a backing layer.
additionButton.wantsLayer = YES;

// Set the layer redraw policy. This would be better done in
// the initialization method of a NSView subclass instead of here.
additionButton.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    context.duration = 1.0f;
    additionButton.animator.frame = CGRectOffset(additionButton.frame, 0.0, -20.0);
    //additionButton.frame = CGRectOffset(additionButton.frame, 0.0, -20.0);
} completionHandler:nil];

ボタンを下に移動するアニメーション:

ここに画像の説明を入力

アニメーションを下に移動した後のボタン:

ここに画像の説明を入力

更新 1

明確にするために、ボタンに背景画像を使用していません。次のように viewDidLoad メソッドで設定した背景 NSColor を使用しています。

[[additionButton cell] setBackgroundColor:[NSColor colorWithRed:(100/255.0) green:(43/255.0) blue:(22/255.0) alpha:1.0]];
4

1 に答える 1

1

これは AppKit のバグだと思います。これを回避するには、いくつかの方法があります。


回避策 1:

レイヤーを使用しないでください。アニメーション化しているボタンは小さいようですが、レイヤーに支えられていないアニメーションを使用してもうまくいく可能性があります。ボタンはアニメーションの各ステップで再描画されますが、正しくアニメーション化されます。これは、これが本当にあなたがしなければならないすべてであることを意味します:

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {          
    additionButton.animator.frame = CGRectOffset(additionButton.frame, 0, -20);
} completionHandler:nil];

回避策 2:

レイヤーの背景色を設定します。

additionButton.wantsLayer = YES;
additionButton.layer.backgroundColor = NSColor.redColor.CGColor;
additionButton.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {          
    additionButton.animator.frame = CGRectOffset(additionButton.frame, 0, -20);
} completionHandler:nil];

回避策 3:

をサブクラスNSButtonCell化して実装し-drawBezelWithFrame:inView:、そこに背景色を描画します。ボタンを含む親ビューはレイヤーでサポートする必要があることに注意してください。そうしないと、ボタンはすべてのステップで再描画されます。

于 2015-04-01T23:23:10.650 に答える