【问题标题】:Checking if NSButton is down on drawRect检查 NSButton 是否在 drawRect 上关闭
【发布时间】:2013-02-07 15:35:48
【问题描述】:

我想在我的自定义 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];
}

【问题讨论】:

    标签: objective-c xcode macos drawrect nsbutton


    【解决方案1】:

    我不认为“drawRect:”是尝试抓住这一点的正确位置。

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

    【讨论】:

    • 我试过self.state,但它只适用于按钮是“开”还是“关”!所以按钮是“开”,一旦我按下它并释放它。我想检查使用是否正在按下。 MouseDown 也是一个选项,但由于某种原因我无法让 mouseUp 工作。
    【解决方案2】:
    if([pBtn state]==NSOnState){
       NSLog(@" button Pressed ")
    }else{
       NSLog(@" button not pressed ");
    
    }
    

    【讨论】:

    • 我试过self.state,但它只适用于按钮是“开”还是“关”!所以按钮是“开”,一旦我按下它并释放它。我想检查用户是否正在按下。
    • 你能用Push按钮吗,它基本上是一个切换按钮,也许你可以得到你想要的..
    • 我试过了。不工作。我最终做了上面的事情。如果您有更好的解决方案,请告诉我。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 我快要对生活生气了
      猜你喜欢
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 2013-02-16
      • 2011-03-22
      相关资源
      最近更新 更多