Making a Cocoa Button look like is pressed, programmatically
I have some key events, that correspond to buttons also. What property/method to I have to set/call for a button to look depressed (change state?) for say hal开发者_C百科f a second?
The way I solved this is I set the NSButton to a type of 'Push On Push Off' and then used the following code in my key event handler:
NSButton *button = [self.superview viewWithTag:event.keyCode];
if (button != nil && button.state == NSOffState) {
[button performClick:event];
[button performSelector:@selector(performClick:) withObject:event afterDelay:0.5];
}
This will highlight the button as if the user had clicked on it, and then it will click on it again in half a second.
I believe the button cell's -setHighlighted:
method controls whether the button looks pressed or not. You may also need to call -setNeedsDisplay:
on the button after changing it, and it's possible that the button will change its cell's highlighted state by itself, so I'm afraid you may need to fiddle around to get this working.
(I have to admit, though, that I'm not entirely certain about any of this.)
精彩评论