Check for NSButton on drawRect

I would like to check if my custom NSButton is in the pressed state (the user clicks on it) in my custom drawRect method. Something like that:

- (void)drawRect:(NSRect)dirtyRect{ if ([self buttonIsInPressedState]) { [[self alternateBGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f]; }else{ [[self BGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f]; } [super drawRect:dirtyRect]; } 

How would you check this out? Is it possible?

Decision

I ended up checking mouseDownFlags on a button cell. I don't know if this is the "right" way to do this, so let me know if you have a better suggestion:

 - (void)drawRect:(NSRect)dirtyRect{ if ([self.cell mouseDownFlags] == 0) { [[self BGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f]; }else{ [[self alternateBGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f]; } [super drawRect:dirtyRect]; } 
+6
source share
4 answers

I ended up checking mouseDownFlags on a button cell. I don't know if this is the “right” way to do this, so let me know if you have a better offer. The solution is updated in the question above.

+2
source

I solved this by checking [self isHighlighted] .

+5
source

I don't think drawRect: is the right place to try and catch it.

You can subclass NSButton and then override mouseDown: or setState: (searching for " NSOnState " means the button is selected or clicked).

+4
source
 if([pBtn state]==NSOnState){ NSLog(@" button Pressed ") }else{ NSLog(@" button not pressed "); } 
0
source

All Articles