0
我想要一个简单的拖放应用程序拖放应用程序:简单的拖放和不工作
- 我创建一个CameraIconView(的NSView的子类,包含了一些图像视图,文本框和一个弹出按钮) ,在运行时。
- 此视图包含在CameraIconEnclosingBox(NSBox的子类)内
- 要求是:用户应该能够将CameraIconView拖放到CameraIconEnclosingBox中的其他位置。
要实现我的要求我这样做:
- 在CameraIconView实现以下方法接收机类
- (无效)鼠标按下:(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.在CameraIconEnclosingBox类中实现以下方法 -
- (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)];
}
实现这些方法后,我能够执行拖动但拖放操作不起作用,虽然所有拖动CameraIconEnclosingBox中的委托方法都被调用。
任何人都可以提出我可能错在哪里或其他更好的方式来实现我的要求吗?
感谢,
Miraaj