1

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

4

1 回答 1

-1

The reason you are getting this behavior is that there are two different ways to handle mouse dragging in Cocoa. Both are discussed here:

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

NSButton is most likely using "The Mouse-Tracking Loop Approach", where everything (including the mouse-up event) is done within the mouseDown: method. So in the case you are wondering about, this is how things could look like in your NSButton subclass:

- (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;
}
于 2013-04-01T14:38:27.733 回答