I want to create a custom switch for my application, in which I supply three images for the possible states (on, off, mousedown). The whole appearance is contained in the images, so I do not want Cocoa to highlight (darken) the button by itself during mousedown.
At the moment, I have created a NSButtonCell subclass, dragged a bevel button of NSButtonTypeMomentaryChange into my view and custom set the button cell class type to my subclass. The subclass just implements awakeFromNib to try and set some desired behavior:
- (void)awakeFromNib
{
[self setShowsStateBy:NSContentsCellMask];
[self setHighlightsBy:NSContentsCellMask];
}
which I thought (according to the documentation) would disable the mousedown-darkening of the button. It does not.
This is where I stand and now I have two questions:
Set your button's type to On/Off. Subclass its cell. Write this two methods:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if ([self state])
{
[onStateImage drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
else
{
[offStateImage drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
}
- (void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if (flag){
[downStateImage drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With