简单的拖放应用程序不起作用
我正在尝试一个简单的拖放应用程序:
- 我正在运行时创建一个 CameraIconView (NSView 的子类,包含一些图像视图、文本字段和弹出按钮)。
- 该视图包含在 CameraIconEnendingBox (NSBox 的子类)中。
- 要求是:用户应该能够将 CameraIconView 拖动到 CameraIconEnendingBox 中的其他位置。
为了实现我的要求,我这样做:
- 在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:
- I am creating a CameraIconView (subclass of NSView, containing some image views, text fields and a pop-up button), at run time.
- This view is enclosed within CameraIconEnclosingBox (subclass of NSBox)
- Requirement is: user should be able to drag CameraIconView at some other location in CameraIconEnclosingBox.
To implement my requirements I am doing this:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以通过添加这行代码来解决它:
在此代码之前:
所以 CameraIconView 现在出现在放置的位置。 :)
...但现在我面临一个新问题-
任何人都可以建议我一些方法来解决它吗?
谢谢,
米拉杰
I am able to resolve it by adding this line of code:
before this code:
So CameraIconView now is appearing at dropped location. :)
... but now I am facing a new problem-
Can anyone suggest me some way to resolve it?
Thanks,
Miraaj