Cocoa 的 NSView 发生疯狂的 mouseDown 事件?
大家好 - 我有一个 NSView 子类,我可以在 mouseDown 上以编程方式移动它。它有效,但有一个奇怪的副作用:
- 我单击子视图。子视图移开[GOOD]
- 我等一下。我不动鼠标。由于子视图已移动,它不再位于我的光标下方。
- 我再次点击鼠标。
- 我希望底层窗口能够获取 mouseDown 事件(因为子视图不再位于我的光标下方),但是我的子视图以某种方式获取此事件[ODD]
- mouseDown 事件清楚地表明该点击超出了我的子类的范围[ODD]
- mouseDown 事件还清楚地显示点击计数已增加,即使我在两次鼠标点击之间等待了几秒钟[ODD]
...必须对我所看到的进行解释。这是我的代码 - 只需创建一个名为“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分配]初始化]]; } @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
Hullo all - I've got a NSView subclass that I move programmatically on mouseDown. Which works, but has an odd side effect:
- I click the subview. The subview moves away [GOOD]
- I wait a while. I do not move my mouse. Since the subview has moved it's no longer under my cursor.
- I click my mouse again.
- I expect the underlying window to get the mouseDown event (since the subview is no longer under my cursor), however my subview somehow gets this event [ODD]
- The mouseDown event clearly shows that the click was outside the bounds of my subclass [ODD]
- The mouseDown event also clearly shows that the click count has incremented, even though I've waited several seconds between mouse clicks [ODD]
... there's gotta be an explanation for what I'm seeing. Here's my code - simply create a new Cocoa Application project called "OddMouse", and copy the following into the OddMouseAppDelegate.h file:
#import <Cocoa/Cocoa.h>
@interface OddMouseAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
@property (assign) IBOutlet NSWindow *window;
@end
@interface OddView : NSView {
}
@end
... and the following into the OddMouseAppDelegate.m file:
#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
... then build, then run, then witness ! FWIW, here's what I see in the console:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
太可怕了 - 事实证明这是双击速度的问题 - 更改首选项做了 SFA,但重新启动解决了......
Freaking heck - turns out this was something misbehaving with the double-click speed - changing the prefs did SFA, but a reboot resolved ...