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