2016-03-04 32 views
1

我有一個自動重複的NSStepper,當我收到某個NSNotification時,我想停止跟蹤。如何中止NSStepper自動重複?

一個想法是從接收通知的方法發送[_stepper setAutorepeat: NO]。這是行不通的。我想步進器只在跟蹤開始時檢查自動重複標誌。

然後,我想我可以繼承NSStepperCell,並使用覆蓋-[NSCell continueTracking:at:inView:]中止跟蹤。然而,顯然,當一個步進器在沒有鼠標移動的情況下自動重複時,該方法不會被調用。

我需要完全重寫trackMouse:inRect:ofView:untilMouseUp:嗎?我想那時我必須在鼠標移入或移出時處理突出顯示的步進部分,並且我沒有看到任何公共API甚至找不到突出顯示的部分。

回答

0

我結束了發佈鼠標事件。我將NSStepperCell分類爲便於觀看通知併發布活動的便利場所。

@interface AbortableStepperCell : NSStepperCell 
{ 
    BOOL _isTracking; 
    BOOL _isObserverInstalled; 
} 


@implementation AbortableStepperCell 

- (void) dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver: self]; 

    [super dealloc]; 
} 

- (BOOL)startTrackingAt: (NSPoint) startPoint 
    inView: (NSView *) controlView 
{ 
    _isTracking = YES; 

    if (! _isObserverInstalled) 
    { 
     [[NSNotificationCenter defaultCenter] 
      addObserver: self 
      selector: @selector(abortTracking:) 
      name: @"AbortMouseTracking" 
      object: nil]; 

     _isObserverInstalled = YES; 
    } 

    return [super startTrackingAt: startPoint inView: controlView]; 
} 

- (void) abortTracking: (NSNotification*) note 
{ 
    if (_isTracking) 
    { 
     NSWindow* myWindow = self.controlView.window; 
     NSGraphicsContext* gc = 
      [NSGraphicsContext graphicsContextWithWindow: myWindow]; 

     NSEvent* upEvent = [NSEvent 
      mouseEventWithType: NSLeftMouseUp 
      location: NSZeroPoint 
      modifierFlags: 0 
      timestamp: 0.0 
      windowNumber: myWindow.windowNumber 
      context: gc 
      eventNumber: 0 
      clickCount: 1 
      pressure: 0.0f ]; 

     if (upEvent) 
     { 
      [NSApp postEvent: upEvent atStart: YES]; 
     } 
    } 
} 

- (void)stopTracking:(NSPoint)lastPoint 
     at:(NSPoint)stopPoint 
     inView:(NSView *)controlView 
     mouseIsUp:(BOOL)flag 
{ 
    _isTracking = NO; 

    [super stopTracking: lastPoint 
      at: stopPoint 
      inView: controlView 
      mouseIsUp: flag]; 
} 

@end