2012-03-30 1 views
4

私はこの方法をApp Delegateで行い、ウィンドウとコンテンツビューを作成しますが、入力時にビューを終了してNSPointを使用してマウスを追跡できるようにしたいと考えています。問題は、NSViewカスタムクラスを作成したくないということです.AppDelegate内でそのすべてを実行したいのです。マウスのトラッキング(下部)は動作しません。NSViewでNSPointマウストラッキングを使用する

- (void)toggleHelpDisplay 
{ 
     // Create helpWindow. 
     NSRect mainFrame = [[NSScreen mainScreen] frame]; 
     NSRect helpFrame = NSZeroRect; 
     float width = 75; 
     float height = 75; 
     helpFrame.origin.x = (mainFrame.size.width - width)/2.0; 
     helpFrame.origin.y = 200.0; 
     helpFrame.size.width = width; 
     helpFrame.size.height = height; 
     helpWindow = [[BrightnessView windowWithFrame:helpFrame] retain]; 

     // Configure window. 
     [helpWindow setReleasedWhenClosed:YES]; 
     [helpWindow setHidesOnDeactivate:NO]; 
     [helpWindow setCanHide:NO]; 
     [helpWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces]; 
     [helpWindow setIgnoresMouseEvents:NO]; 

     // Configure contentView. 
     NSView *contentView = [helpWindow contentView]; 
     [contentView setWantsLayer:YES]; 
     CATextLayer *layer = [CATextLayer layer]; 
     layer.opacity = 0; 
     [contentView setLayer:layer]; 
     CGColorRef bgColor = CGColorCreateGenericGray(0.0, 0.6); 
     layer.backgroundColor = bgColor; 
     CGColorRelease(bgColor); 
     layer.string = ? HELP_TEXT : HELP_TEXT_OFF; 
     layer.contentsRect = CGRectMake(0, 0, 1, 1.2); 
     layer.fontSize = 40.0; 
     layer.foregroundColor = CGColorGetConstantColor(kCGColorWhite); 
     layer.borderColor = CGColorGetConstantColor(kCGColorWhite); 
     layer.borderWidth = 4.0; 
     layer.cornerRadius = 4.0; 
     layer.alignmentMode = kCAAlignmentCenter; 

     [window addChildWindow:helpWindow ordered:NSWindowAbove]; 

     float helpOpacity = (([NSApp isActive] ? 1 : 0)); 
     [[[helpWindow contentView] layer] setOpacity:helpOpacity]; 

     //track mouse so that once hovered make larger. 
    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream]; 

    if (NSPointInRect(mouseLocation, helpFrame)) { 
     NSLog(@"mouse over"); 
    } 
    else 
    { 
     NSLog(@"mouse not over"); 
    } 

} 

答えて

7

これは、NSTrackingAreaを使用して行う必要があります。

self.helpView = contentView; // Need to store a reference to the view if you want to convert from its coordinate system 
// Set up a tracking area 
NSTrackingArea *trackingArea = [[[NSTrackingArea alloc] initWithRect:[self.helpView bounds] 
                  options:NSTrackingActiveInActiveApp | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved 
                   owner:self 
                  userInfo:nil] autorelease]; 
[self.helpView addTrackingArea:trackingArea]; 

- (void)mouseEntered:(NSEvent *)event; 
{ 
    NSPoint location = [self.helpView convertPoint:[event locationInWindow] fromView:nil]; 
    // Do whatever you want to do in response to mouse entering 
} 

- (void)mouseExited:(NSEvent *)event; 
{ 
    NSPoint location = [self.helpView convertPoint:[event locationInWindow] fromView:nil]; 
    // Do whatever you want to do in response to mouse exiting 
} 

- (void)mouseMoved:(NSEvent *)event; 
{ 
    NSPoint location = [self.helpView convertPoint:[event locationInWindow] fromView:nil]; 
    // Do whatever you want to do in response to mouse movements 
} 
+1

トラッキングエリアがアクティブなときに、エラーtrackingAreaオプション0x3が発生した場合を除いて、すべてが機能しているように見えます(このようなことは、ブラウザで入力したものです)。何か案は?ありがとう! –

+0

nevermind、もっとオプションを指定しなければならなかった、ありがとう! –

+0

ここでヘルパービューが必要なのはなぜですか? NSViewの所有者がマウスを個別に追跡するのはなぜですか? –

関連する問題