13

我想为 1 个特定的 NSColorWell 显示的 NSColorPanel 添加一个不透明度滑块。所有其他颜色孔不应显示不透明度滑块。

我知道我可以像这样为 sharedColorPanel 设置它:

 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];

但是,当我只希望这种行为适用于单一颜色时,我该怎么做呢?

我尝试添加一个 IBAction,但是当您单击颜色时不会调用此 IBAction。(所以在面板显示之前我无法进行任何更改)。当您在颜色面板中选择另一种颜色时调用它。

4

4 回答 4

15

OK, here's the code that works. Set the colorwell class in IB to AlphaColorWell:**

@implementation AlphaColorWell

- (void)activate:(BOOL)exclusive
{
    [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
    [super activate:exclusive];
}

- (void)deactivate
{
    [super deactivate];
    [[NSColorPanel sharedColorPanel] setShowsAlpha:NO];
}

@end
于 2013-03-04T06:56:25.757 回答
7

AppKit isn't showing the opacity slider in the panel because it thinks the app doesn't support alpha, which is the default. So rather than subclass, just do this:

func applicationDidFinishLaunching(_ aNotification: Notification) {
    ...
    NSColor.ignoresAlpha = false
    ...
}
于 2018-04-20T17:32:50.867 回答
3
class ANColorWell: NSColorWell {
override func activate(_ exclusive: Bool) {
    NSColorPanel.shared.showsAlpha = true;
    super.activate(exclusive);
}

override func deactivate() {
    NSColorPanel.shared.showsAlpha = false;
    super.deactivate();

}
}

This is the swift4.0 version I tried, which I hope will be useful to you

于 2017-08-26T12:57:05.297 回答
1

就像处理大多数 AppKit 一样,答案是子类化。

@interface AlphaColorPanel : NSColorPanel

@end

@implementation AlphaColorPanel

- (BOOL)showsAlpha {
    return YES;
}

@end

然后进入 IB 并覆盖要显示 alpha 滑块的单一颜色面板的类。

于 2013-03-03T19:30:15.783 回答