6

我想在我的自定义 drawRect 方法中检查我的自定义 NSButton 当前是否处于按下状态(用户正在单击它)。像这样的东西:

- (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];
}

你会如何检查这样的事情?可能吗?

解决方案

我最终检查了按钮单元格上的 mouseDownFlags。不知道这是否是“正确”的做法,所以如果您有更好的建议,请告诉我:

- (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];
}
4

4 回答 4

5

我通过检查解决了这个问题[self isHighlighted]

于 2015-01-12T09:03:22.633 回答
3

我不认为“ drawRect:”是试图抓住这一点的正确地方。

您可以继承 NSButton,然后覆盖mouseDown:setState:(寻找“ NSOnState”,这表示按钮被选中或被按下)。

于 2013-02-22T05:21:39.243 回答
1

我最终检查了按钮单元格上的 mouseDownFlags。不知道这是否是“正确”的做法,所以如果您有更好的建议,请告诉我。解决方案已在上述问题中更新。

于 2013-02-22T20:31:32.800 回答
0
if([pBtn state]==NSOnState){
   NSLog(@" button Pressed ")
}else{
   NSLog(@" button not pressed ");

}
于 2013-02-22T06:21:09.660 回答