开发者

Cocoa NSView subview blocking drag/drop

I have an NSView subclass which registers for drag files in init method like this:

    [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];

The drag drop works perfectly fine, but if I add a subview to this view with the exact same frame, it doesn't work any more. My guess is that the subview is block the drag event to go to super vie开发者_Python百科w. Can can I avoid that? Thanks

Also, I know I am asking two questions, but I don't want to create a new topic just for this: When I am dragging, my cursor doesn't change to the "+" sign like with other drags, how can I do that? Thanks again.

UPDATE: Here's the how I have it set up in my IB:

Cocoa NSView subview blocking drag/drop

The DrawView is the custom class I was talking about that registered for draggedtypes. And the Image view simply is a subview, I dragged an image from the media section... If it helps, here's my relevant code for DragView:

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
    return NSDragOperationCopy;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
    NSPasteboard *pboard;
    pboard = [sender draggingPasteboard];
    NSArray *list = [pboard propertyListForType:NSFilenamesPboardType];
    if ([list count] == 1) {
        BOOL isDirectory = NO;
        NSString *fileName = [list objectAtIndex:0];
        [[NSFileManager defaultManager] fileExistsAtPath:fileName
                                             isDirectory: &isDirectory];
        if (isDirectory) {
            NSLog(@"AHH YEA");
        } else {
            NSLog(@"NOO");
        }
    }
    return YES;
}


The answer to the second part of your question is this:

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
    return NSDragOperationCopy;
}

if you return NSDragOperationCopy, you get the mouse badge for a copy operation. (You can and should, of course, not just return NSDragOperationCopy unconditionally; check the objects on the pasteboard to see if you can accept them.)

I'm not sure what the answer to the first part of your question is, because I'm unable to recreate the subview blocking effect.

Okay, the answer is unfortunately that you can't. The image you dragged is contained in an NSImageView, and NSImageViews accept drag events themselves, so it's grabbing the drag event and not doing anything with it. If your subview was a custom class, you could either a) not implement drag and drop, in which case the drags would be passed through; b) implement drag and drop to accept drags for the subview. In this case, you're using a class over which you don't have any control. If all you want it to do is display an image, you should be able to make another NSView subclass that does nothing but draw the image in drawRect:


As mentioned in the comments, NSImageViews have their own drag and drop enabled by default (used for accepting images that are dragged onto the NSImageView). If you don't want to use this behavior and instead want to use the super view's drag and drop behavior, you'll want to unregister the dragging behavior in the NSImageView.

Objc:

[imageView unregisterDraggedTypes];

Swift:

imageView.unregisterDraggedTypes()

(in case anyone else stumbled across this question first and not the linked one in the comments)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜