2010-01-16 37 views
1

你好全部 - 我有一個NSView子類,我在mouseDown上以編程方式移動。哪個有效,但有副作用:可可的NSView得到瘋狂的mouseDown事件?

  1. 我點擊子視圖。子視圖移開[GOOD]
  2. 我等了一會兒。我不會移動我的鼠標。由於子視圖已移動,它不再位於我的光標下方。
  3. 我再次點擊鼠標。
    • 我期望下層窗口拿到mouseDown事件(因爲子視圖不再是我的光標所在),但我的子視圖以某種方式得到這個事件[奇]
    • MouseDown事件清楚地表明,點擊是我的子類[ODD]的範圍之外
    • MouseDown事件也清楚地表明,點擊次數已經增加,儘管我已經等了鼠標之間幾秒鐘點擊[奇]

...有一個解釋我所看到的。這裏是我的代碼 - 只需創建一個名爲「OddMouse」一個新的Cocoa應用程序的項目,並複製以下到OddMouseAppDelegate.h文件:

#import <Cocoa/Cocoa.h> 
@interface OddMouseAppDelegate : NSObject <NSApplicationDelegate> { 
    NSWindow *window; 
} 
@property (assign) IBOutlet NSWindow *window; 
@end 

@interface OddView : NSView { 
} 
@end 

...和下​​面進入OddMouseAppDelegate.m文件:

#import "OddMouseAppDelegate.h" 
@implementation OddMouseAppDelegate 
@synthesize window; 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 

[[window contentView] addSubview:[[OddView alloc] init]]; } @end

@implementation OddView 
- (id)init { 
    self = [super initWithFrame:NSMakeRect(100, 100, 100, 100)]; 
    return self; 
} 
- (void)drawRect:(NSRect)dirtyRect { 
    NSBezierPath *bz = [NSBezierPath bezierPathWithRoundedRect:[self bounds] 
               xRadius:9 yRadius:9]; 
    [[NSColor blueColor] set]; 
[bz fill]; 
} 
- (void)mouseDown:(NSEvent *)event { 
    NSPoint locationInMyself = [self convertPoint: [event locationInWindow] 
             fromView: nil]; 
    NSLog(@"MOUSE DOWN COORDS: x=%f y=%f, count=%i", 
      locationInMyself.x, locationInMyself.y, [event clickCount]); 
    float newX = [self frame].origin.x+100; 
    float newY = [self frame].origin.y+100; 
    [self setFrame:NSMakeRect(newX, newY, 100, 100)]; 
} 
@end 

...再建,再運行,然後見證! FWIW,這裏就是我在控制檯中看到:

10-01-15 11:38:24 PM OddMouse[4583] MOUSE DOWN COORDS: x=48.000000 y=56.000000, count=1 
10-01-15 11:38:37 PM OddMouse[4583] MOUSE DOWN COORDS: x=-52.000000 y=-44.000000, count=2 
10-01-15 11:38:44 PM OddMouse[4583] MOUSE DOWN COORDS: x=-152.000000 y=-144.000000, count=3 
10-01-15 11:38:52 PM OddMouse[4583] MOUSE DOWN COORDS: x=-252.000000 y=-244.000000, count=4 
10-01-15 11:39:03 PM OddMouse[4583] MOUSE DOWN COORDS: x=-352.000000 y=-344.000000, count=5 

回答

0

惱人的挫折感 - 原來這是一些與雙擊速度失常 - 改變首選項做了SFA,但重啓解決...