简单的拖放应用程序不起作用

发布于 2024-09-09 09:31:45 字数 2428 浏览 11 评论 0原文

我正在尝试一个简单的拖放应用程序:

  1. 我正在运行时创建一个 CameraIconView (NSView 的子类,包含一些图像视图、文本字段和弹出按钮)。
  2. 该视图包含在 CameraIconEnendingBox (NSBox 的子类)中。
  3. 要求是:用户应该能够将 CameraIconView 拖动到 CameraIconEnendingBox 中的其他位置。

为了实现我的要求,我这样做:

  1. 在CameraIconView类中实现以下方法

- -(void)mouseDown:(NSEvent *)e{

    NSPoint location; 
    NSSize size;
    NSPasteboard *pb = [NSPasteboard pasteboardWithName:@"CameraIconContainer"];

    location.x =  ([self bounds].size.width - size.width)/2;
    location.y =  ([self bounds].size.height - size.height)/2;

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    [pb declareTypes:[NSArray arrayWithObject:IconDragDataType] owner:self];
    [pb setData:data forType:IconDragDataType];
    [self dragImage:[NSImage imageNamed:@"camera_icon.png"] at:location offset:NSZeroSize event:e pasteboard:pb source:self slideBack:NO];
}

2.在CameraIconEnlookingBox类中实现以下方法-

- (void)awakeFromNib{
    [self registerForDraggedTypes:[NSArray arrayWithObject:IconDragDataType]];
}
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender{
    return NSDragOperationEvery;
}
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender{
    return YES;
}
- (void)concludeDragOperation:(id < NSDraggingInfo >)sender{
    NSPoint dropLocation = [sender draggingLocation];

    float x = dropLocation.x;
    float y = dropLocation.y;
    NSLog(@"dragOperationConcluded! draggingLocation: (%f, %f)",x,y);

    NSPasteboard *pb = [sender draggingPasteboard];
    NSLog(@"Pasteboard name- %@",[pb name]);
    NSData *draggedData = [pb dataForType:IconDragDataType];

    CameraIconView *object = [NSKeyedUnarchiver unarchiveObjectWithData:draggedData];
    NSLog(@"object - %@ / cameraNo : %@/ operatorName : %@",object,[[object cameraNo] stringValue],[[object operatorName] stringValue]);
    // the cameraNo and operatorName are properties defined within CameraIconView class
    // the above log is returning (null) for both properties

    float width  = [object frame].size.width;
    float height = [object frame].size.height;

    NSLog(@"width - %f / height - %f",width,height);
    [object setFrame:NSMakeRect(x, y, width, height)];
}

实现这些方法后,我能够执行拖放操作尽管调用了 CameraIconEnendingBox 中的所有拖动委托方法,但不起作用。

谁能建议我可能错在哪里或其他更好的方法来实现我的要求?

谢谢,

米拉杰

I am trying a simple drag and drop application:

  1. I am creating a CameraIconView (subclass of NSView, containing some image views, text fields and a pop-up button), at run time.
  2. This view is enclosed within CameraIconEnclosingBox (subclass of NSBox)
  3. Requirement is: user should be able to drag CameraIconView at some other location in CameraIconEnclosingBox.

To implement my requirements I am doing this:

  1. Implemented following method in CameraIconView class-

-(void)mouseDown:(NSEvent *)e{

    NSPoint location; 
    NSSize size;
    NSPasteboard *pb = [NSPasteboard pasteboardWithName:@"CameraIconContainer"];

    location.x =  ([self bounds].size.width - size.width)/2;
    location.y =  ([self bounds].size.height - size.height)/2;

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    [pb declareTypes:[NSArray arrayWithObject:IconDragDataType] owner:self];
    [pb setData:data forType:IconDragDataType];
    [self dragImage:[NSImage imageNamed:@"camera_icon.png"] at:location offset:NSZeroSize event:e pasteboard:pb source:self slideBack:NO];
}

2. Implemented following method in CameraIconEnclosingBox class-

- (void)awakeFromNib{
    [self registerForDraggedTypes:[NSArray arrayWithObject:IconDragDataType]];
}
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender{
    return NSDragOperationEvery;
}
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender{
    return YES;
}
- (void)concludeDragOperation:(id < NSDraggingInfo >)sender{
    NSPoint dropLocation = [sender draggingLocation];

    float x = dropLocation.x;
    float y = dropLocation.y;
    NSLog(@"dragOperationConcluded! draggingLocation: (%f, %f)",x,y);

    NSPasteboard *pb = [sender draggingPasteboard];
    NSLog(@"Pasteboard name- %@",[pb name]);
    NSData *draggedData = [pb dataForType:IconDragDataType];

    CameraIconView *object = [NSKeyedUnarchiver unarchiveObjectWithData:draggedData];
    NSLog(@"object - %@ / cameraNo : %@/ operatorName : %@",object,[[object cameraNo] stringValue],[[object operatorName] stringValue]);
    // the cameraNo and operatorName are properties defined within CameraIconView class
    // the above log is returning (null) for both properties

    float width  = [object frame].size.width;
    float height = [object frame].size.height;

    NSLog(@"width - %f / height - %f",width,height);
    [object setFrame:NSMakeRect(x, y, width, height)];
}

After implementing these methods I am able to perform drag but drop operation is not working, although all dragging delegate methods in CameraIconEnclosingBox are called.

Can anyone suggest where I may be wrong or some other better way to implement my requirements?

Thanks,

Miraaj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-09-16 09:31:45

我可以通过添加这行代码来解决它:

[self addSubview:object];

在此代码之前:

[object setFrame:NSMakeRect(x, y, width, height)];

所以 CameraIconView 现在出​​现在放置的位置。 :)

...但现在我面临一个新问题-

每次CameraIconView
拖放,它不是
定位拖动的CameraIconView
到新位置,但正在创建和
显示其中的新实例
CameraIconEnclosureBox 位于新位置,即。为
首先拖放两个图标开始
出现相同的 CameraIconView
...第二次拖放三个
图标开始出现等等......

任何人都可以建议我一些方法来解决它吗?

谢谢,

米拉杰

I am able to resolve it by adding this line of code:

[self addSubview:object];

before this code:

[object setFrame:NSMakeRect(x, y, width, height)];

So CameraIconView now is appearing at dropped location. :)

... but now I am facing a new problem-

each time the CameraIconView is
dragged and dropped, it is not
positioning the dragged CameraIconView
to new location, but is creating and
showing a new instance within
CameraIconEnclosingBox at new location, ie. for the
first drag and drop two icons start
appearing for the same CameraIconView
... for second drag and drop three
icons start appearing and so on....

Can anyone suggest me some way to resolve it?

Thanks,

Miraaj

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文