【发布时间】:2015-04-01 05:57:05
【问题描述】:
我正在开发 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]];
【问题讨论】:
-
图片不显示
-
@ysaditya 但我没有使用图像作为 NSButton 的背景。我用一个简单的 NSColor 填充它。
-
您正在使用哪个
type按钮(例如,瞬时按下等) -
@l'L'l 所以是的,对不起,我的截图是垃圾。但基本上当我在 viewDidLoad 中设置背景单元格颜色时,它工作得很好。当我运行动画时,按钮很好地向下移动,但红色背景颜色完全消失。动画开始后由于某种原因不再有背景颜色。
-
@Supertecnoboff,你应该在
[super drawRect]中设置属性
标签: macos cocoa nsbutton nsanimationcontext nsanimation