Add opacity slider to the color panel for one color well but not others

北城以北 提交于 2019-12-23 07:29:21

问题


I'd like to add a opacity slider to the NSColorPanel that is shown for 1 specific NSColorWell. All other color wells should not show the opacity slider.

I know I can set this for the sharedColorPanel like so:

 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];

But how do I do this when I only want this behavior for a single color well?

I tried adding an IBAction, but this IBAction is not called when you click the color well. (So I can't make any changes before the panel is displayed). It is called when you choose another color in the color panel.


回答1:


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



回答2:


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
    ...
}



回答3:


The answer, like dealing with most of AppKit, is to subclass.

@interface AlphaColorPanel : NSColorPanel

@end

@implementation AlphaColorPanel

- (BOOL)showsAlpha {
    return YES;
}

@end

Then go into IB and override the class of the singular color panel you want to show the alpha slider.




回答4:


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



来源:https://stackoverflow.com/questions/15189074/add-opacity-slider-to-the-color-panel-for-one-color-well-but-not-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!