Cocoa的NSView疯狂的mouseDown事件?

时间:2010-01-16 04:58:41

标签: cocoa mouse nsview nsevent

Hullo all - 我有一个NSView子类,我在mouseDown上以编程方式移动。哪个有效,但有一个奇怪的副作用:

  1. 点击子视图。子视图移开 [GOOD]
  2. 我等一会儿。我不动我的鼠标。由于子视图已移动,因此不再在我的光标下。
  3. 我再次点击鼠标。
    • 我希望底层窗口获取mouseDown事件(因为子视图不再在我的光标下),但是我的子视图以某种方式获取此事件 [ODD]
    • mouseDown事件清楚地显示点击超出了我的子类 [ODD]
    • 的范围
    • mouseDown事件还清楚地显示点击次数已增加,即使我在鼠标点击之间等了几秒钟 [ODD]
  4. ......对于我所看到的内容,我必须有一个解释。这是我的代码 - 只需创建一个名为“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
    

1 个答案:

答案 0 :(得分:0)

吓坏了 - 事实证明这是双击速度不正常的事情 - 改变了pref做了SFA,但重启已经解决了......