mouseUp无法在Cocoa中启动? [英] mouseUp not firing in Cocoa?
问题描述
我试图在我的应用程序中实现拖放功能,但遇到了mouseUp事件无法触发的问题.如果我只是单击并释放视图,则射击就很好了,但是如果我拖动,鼠标就会拖动事件触发,仅此而已.
I'm trying to implement drag&drop functionality in my app and I ran into a problem of my mouseUp event not firing. It fires just fine if I simply click and release on my view, but if I do drag, my mouseDragged event fires and that's it.
通过尝试不同的方法,我发现问题出在我的dragImage调用中,但是我暂时不知道如何解决它.
By trying different things I figured out that the problem is in my dragImage call, but I don't know how to solve it for now.
这是我的代码:
-(void) mouseDragged:(NSEvent *)theEvent
{
isDragging = YES;
if ([selectedCellRowIndex longValue] >= 0)
{
NSImage *im = [[NSImage alloc]initWithContentsOfFile:@"/Users/UserName/Desktop/drag.png"];
NSSize dragOffset = NSMakeSize(0.0, 0.0);
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:self];
[pboard setData:[im TIFFRepresentation] forType:NSTIFFPboardType];
[self dragImage:im
at:downPointRelativeToTable
offset:dragOffset
event:theEvent
pasteboard:pboard
source:self
slideBack:YES];
}
}
有人知道我的问题在哪里吗?!谢谢!
Does anyone know where my problem can be?! Thank you!
推荐答案
在您的代码段中,您将获得 NSPasteboard
实例,但未将数据复制到该实例.这是必需的,否则您的拖动操作将无法正确启动-这可能就是为什么您永远不会收到 mouseUp:
事件的原因.
In your code snippet you're getting the NSPasteboard
instance but not copying your data to it. That's required, otherwise your drag won't be initiated properly - that's probably why you'll never get the mouseUp:
event.