我有一个nsview子类,可以在mousedown上以编程方式移动。有效,但有一个奇怪的副作用:
-
我单击子视图。子视图移开
[好]
-
我等了一会儿。我不移动鼠标。由于子视图已移动,它不再位于我的光标下。
-
我再次点击鼠标。
-
我期望底层窗口得到mousedown事件(因为子视图不再在我的光标下),但是我的子视图不知何故得到了这个事件。
[奇]
-
mousedown事件清楚地表明单击超出了我的子类的界限。
[奇]
-
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]];
}
@结束
@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