Checking if NSButton is down on drawRect

自闭症网瘾萝莉.ら 提交于 2020-01-03 10:59:08

问题


I would like to check if my custom NSButton is currently in a pressed state (the user is clicking down on it) in my custom drawRect method. Something like this:

- (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 a thing like that? Is it possible?

SOLUTION

I ended up checking the mouseDownFlags on the buttons cell. Don't know if it's the "right" way to do it, 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];
}

回答1:


I solved this by checking [self isHighlighted].




回答2:


I don't think "drawRect:" is the right place to try to catch this.

You can subclass NSButton and then override either mouseDown: or setState: (looking for "NSOnState", which signifies the button is either selected or being pressed on).




回答3:


I ended up checking the mouseDownFlags on the buttons cell. Don't know if it's the "right" way to do it, so let me know if you have a better suggestion. Solution is updated in the question above.




回答4:


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

}


来源:https://stackoverflow.com/questions/15017047/checking-if-nsbutton-is-down-on-drawrect

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