#objective-c #drag-and-drop #draggable
#objective-c #перетаскивание #перетаскиваемый
Вопрос:
Я ищу объяснение метода перетаскивания в NSWindow.
- (void)dragImage:(NSImage *)image at:(NSPoint)imageLocation offset:(NSSize)pointerOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pasteboard source:(id)sourceObject slideBack:(BOOL)slideBack
Любая помощь?
Ответ №1:
Документация NSView по этому методу предоставляет гораздо более подробное объяснение, включая пример кода. Единственное различие между двумя методами заключается в системе координат, которая imageLocation
используется. Найдите это в http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/doc/uid/20000014-SW135
Редактировать: Вот пример кода по этой ссылке с добавленными комментариями.
- (void)mouseDown:(NSEvent *)theEvent {
// Create an offset for the offset parameter. Since it is ignored, you should just use 0,0.
NSSize dragOffset = NSMakeSize(0.0, 0.0);
NSPasteboard *pboard;
// Get the pasteboard used to hold drag-and-drop data
pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
// Declare the data type used on the pasteboard. This sample passes a TIFF representation of an image
[pboard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:self];
// Add the data to the pasteboard.
[pboard setData:[[self image] TIFFRepresentation] forType:NSTIFFPboardType];
// Call dragImage:... with:
// An image representing the object you are dragging. This could be a file's icon or a screenshot of a view, etc.
[self dragImage:[self image]
// The starting location of the image in this views coordinates. This should be close to the location of the object you are dragging.
at:[self imageLocation]
// An NSSize structure. This is not used.
offset:dragOffset
// The mousedown event
event:theEvent
// The pasteboard which contains the data
pasteboard:pboard
// The object providing the data
source:self
// Whether or not the image should slide to its starting location if the drag isn't completed.
slideBack:YES];
}
Исходный объект (в данном случае self) должен реализовать протокол NSDraggingSource. Вас также может заинтересовать ссылка на класс NSPasteboard.
Комментарии:
1. Отлично, это именно то, что я искал. Большое спасибо за то, что сделали все возможное.