mouseDown和mouseUp时重绘NSButton

时间:2013-04-01 11:36:39

标签: macos cocoa subclass nsbutton

我继承NSButton并执行自定义绘图。我是方法-mouseDown-mouseUp。当我执行短(快)点击时,我的代码工作完美,但如果我按住鼠标左键一段时间然后释放它,方法-mouseUp不起作用。 也许我不明白按钮是如何工作的...所以我希望按钮在鼠标停机时改变外观,并在它上升时返回到之前。 我做错了什么?

1 个答案:

答案 0 :(得分:-1)

您获得此行为的原因是有两种不同的方法来处理Cocoa中的鼠标拖动。两者都在这里讨论:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html%23//apple_ref/doc/uid/10000060i-CH6-SW18

NSButton最有可能使用“鼠标跟踪循环方法”,其中所有内容(包括鼠标启动事件)都在mouseDown:方法中完成。因此,在您想知道的情况下,这就是NSButton子类中的情况:

- (void)mouseDown:(NSEvent *)event
{
  someIvar = NO;

  [super mouseDown:event];

  //sometimes, when you are here, you have already had the "mouse up"
  //because super's mouseDown did everything.

  //to find out if this is the case, one solution would be to put
  //an instance variable into your subclass (someIvar)

  if (someIvar == YES)
  {
    //you have a "mouse up"
  }
  else
  {
    //you don't have a "mouse up"
  }
}

- (void)mouseDragged:(NSEvent *)event
{
  someIvar = YES;
}