2012-09-15 4 views
2

마우스 오버 이벤트에서 강조 표시된 버튼을 완료하려고합니다. 그래서 나는 NSButton을 서브 클래 싱했다. 여기에는 NSTrackingArea- (void)mouseEntered:(NSEvent *)event- (void)updateTrackingAreas이있다.NSButton NSTrackingArea - 추적이 작동하지 않습니다.

 CalendarTile *button = [[CalendarTile alloc] init]; 

     [button setFrame:CGRectMake(point_x, point_y, button_frame_width, button_frame_height)]; 
     [button setBordered:NO]; 
     [button setBezelStyle:NSRegularSquareBezelStyle]; 
     [button setButtonType:NSMomentaryChangeButton]; 
     [button setFont:[NSFont fontWithName:@"Avenir Next" size:40]]; 
     [button setAlignment:NSCenterTextAlignment]; 

     [button setTitle:[NSString stringWithFormat:@"%i", i]]; 
     [button setTextColor:[NSColor colorWithCalibratedRed:(float)62/255 green:(float)62/255 blue:(float)62/255 alpha:1.0]]; 

     [arrayWithButtons addObject:button]; 

     ... 

     for (CalendarTile *btn in arrayWithButton) { 
     [self addSubview:btn]; 
     } 

을 그리고 이것은 서브 클래스 - CalendarTile.m : 버튼의

창조 그렇게 보이는 (I 수집 배열을 사용 있도록 루프에서의)

@implementation CalendarTile 
- (void)updateTrackingAreas 
{ 
    [super updateTrackingAreas]; 

    if (trackingArea) 
    { 
     [self removeTrackingArea:trackingArea]; 
    } 

    NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow; 
    trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil]; 
    [self addTrackingArea:trackingArea]; 
} 

- (void)mouseEntered:(NSEvent *)event 
{ 
    [self setImage:[NSImage imageNamed:@"highlight.png"]]; 
    NSLog(@"HIGHLIGHT"); 
} 

이 글은 말을해야 마우스 오버시에는 "HIGHLIGHT"가 기록됩니다. 슬프게도 그렇지 않습니다.

도와 주시겠습니까? 내가 틀린 게 뭐야?

+0

NSZeroRect로 추적 영역을 초기화 하시겠습니까? – TheAmateurProgrammer

+0

자,이 문제에 따라 문제를 해결했습니다 : http://stackoverflow.com/questions/7889419/cocoa-button-rollovers-with-mouseentered-and-mouseexited –

답변

0

trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil]; 

대신

다음
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways; 
    trackingArea = [[NSTrackingArea alloc] initWithRect:self.frame options:options owner:self userInfo:nil]; 
+0

NSZeroRect를 설정하면 문제가 해결되지 않을 것이라고 생각합니다. 문제. 보통'self.bounds'로 설정됩니다. –

1

내가 만든 완벽하게 나를 위해 일한입니다 ...

1 단계보십시오 : 추적 영역과 버튼 만들기

NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(100, 7, 100, 50)]; 
[myButton setTitle:@"sample"]; 

[self.window.contentView addSubview:myButton]; 
// Insert code here to initialize your application 
NSTrackingArea* trackingArea = [[NSTrackingArea alloc] 
           initWithRect:[myButton bounds] 
           options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways 
           owner:self userInfo:nil]; 
[myButton addTrackingArea:trackingArea]; 

단계 : 2 다음 방법을 구현하십시오.

- (void)mouseEntered:(NSEvent *)theEvent{ 
NSLog(@"entered"); 
[[myButton cell] setBackgroundColor:[NSColor blueColor]]; 


} 

- (void)mouseExited:(NSEvent *)theEvent{ 
[[myButton cell] setBackgroundColor:[NSColor redColor]]; 
NSLog(@"exited"); 

} 
관련 문제