问题
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