2013-03-14 9 views
2

사용자 지정 NSCell을 사용하여 사용자 지정 NSControl을 작성하고 있습니다. 그것은 컨트롤이므로 마우스에 응답해야합니다. 내 컨트롤에 NSTrackingArea를 만들었고 -mouseEntered:, -mouseExited:-mouseMoved:을 구현했습니다. (그리고 나는 -mouseUp/Down:을 구현해야 할 것이다. 그러나 무엇을 해야할지 모르겠다. 왜냐하면 지금은 아직 그 메소드를 오버라이드하지 않았다.)이 방법들에서 나는 현재 마우스가 어느 셀에 있는지를 성공적으로 결정한다. 이제 두 가지 질문이 있습니다.NSCell에서 어떤 방법을 사용해야합니까?

  • 마우스를 추적하는 좋은 방법입니까? 그렇지 않다면 대신 무엇을해야합니까?
  • 마우스를 클릭하면 셀에 들어가고 마우스가 셀을 떠날 때 내 NSCell에서 어떤 방법으로 호출해야합니까? 애플의 문서는 이것에 대해 분명하지 않다.

그래서 기본적으로 : NSCell에서 마우스 이벤트에 응답 할 수있는 방법은 언제 호출해야합니까?

편집 : 문서를 다시 읽기는
, 나는 NSCell에의 -trackMouse:inRect:ofView:untilMouseUp:에 전화 -startTrackingAt:inView:, -continueTracking:at:inView:-stopTracking:at:inView:mouseIsUp:을 무시한다고 생각합니다. 다시 두 가지 질문 : 1) 문서가 마우스를 눌렀을 때만 호출된다는 인상을줍니다. 그 맞습니까? 그럼 내가 뭘해야하지? 2) 언제 NSCell의 -trackMouse:inRect:ofView:untilMouseUp:으로 전화해야합니까?

// MyControl.m: 

- (void)mouseDown:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     currentCell = cell; 
     [currentCell mouseDown:theEvent]; 
    } 
} 

- (void)mouseUp:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     [cell mouseUp:theEvent]; 
    } 
} 

- (void)mouseEntered:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     currentCell = cell; 
     [currentCell mouseEntered:theEvent]; 
    } 
} 

- (void)mouseExited:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     [cell mouseExited:theEvent]; 
     currentCell = nil; 
    } 
} 

- (void)mouseMoved:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    MKListCell *cell; 
    if (currentCellIndex < [cells count]) { 
     cell = [cells objectAtIndex:currentCellIndex]; 
    } 
    if (currentCell != cell) { 
     [currentCell mouseExited:theEvent]; 
     [cell mouseEntered:theEvent]; 
     currentCell = cell; 
    } 
    else { 
     [currentCell mouseMoved:theEvent]; 
    } 
} 

- (void)mouseDragged:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    MKListCell *cell = nil; 
    if (currentCellIndex < [cells count]) { 
     cell = [cells objectAtIndex:currentCellIndex]; 
    } 
    if (currentCell != cell) { 
     [currentCell mouseExited:theEvent]; 
     [cell mouseEntered:theEvent]; 
     currentCell = cell; 
    } 
    else { 
     [currentCell mouseMoved:theEvent]; 
    } 
} 

- (int)indexOfCellAtPoint:(NSPoint)p { 
    int cellIndex = (self.bounds.size.height - p.y)/cellHeight; 
    return cellIndex; 
} 

그리고 물론, MyCell.h에서 :

+1

NSActionCell을 살펴보십시오. 따라야 할 패턴을 제공해야합니다. –

+0

확장 할 수 있습니까? 강조 표시도 추가하고 싶기 때문에 기본 대상/액션 지원 이상의 것을 필요로합니다. @ 짐 펄스 – 11684

답변

1

내가 내 자신의 마우스 추적 메커니즘 구현 결국 그 방법에 대해 빈 구현

- (void)mouseDown:(NSEvent *)event; 
- (void)mouseUp:(NSEvent *)event; 
- (void)mouseMoved:(NSEvent *)event; 
- (void)mouseEntered:(NSEvent *)event; 
- (void)mouseExited:(NSEvent *)event; 

을 (그래서 컴파일러는 불평하지 않는다 서브 클래스에 마우스 처리 메소드의 구현을 맡길 수 있습니다).