2010-01-16 35 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,但重启解决...