Created
January 25, 2014 18:43
-
-
Save valexa/8621322 to your computer and use it in GitHub Desktop.
NSDraggingSession
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)mouseDown:(NSEvent*)event | |
{ | |
//create a NSPasteboardItem | |
NSPasteboardItem *pbItem = [NSPasteboardItem new]; | |
[pbItem setDataProvider:self forTypes:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, nil]]; | |
//create a new NSDraggingItem with our pasteboard item. | |
NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pbItem]; | |
NSPoint dragPosition = [self convertPoint:[event locationInWindow] fromView:nil]; | |
dragPosition.x -= self.image.size.width/5/2; | |
dragPosition.y -= self.image.size.height/5/2; | |
NSRect draggingRect = NSMakeRect(dragPosition.x, dragPosition.y, self.image.size.width/5, self.image.size.height/5); | |
[dragItem setDraggingFrame:draggingRect contents:self.image]; | |
//create a dragging session with our drag item and ourself as the source. | |
NSDraggingSession *draggingSession = [self beginDraggingSessionWithItems:[NSArray arrayWithObject:dragItem] event:event source:self]; | |
//causes the dragging item to slide back to the source if the drag fails. | |
draggingSession.animatesToStartingPositionsOnCancelOrFail = YES; | |
} | |
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context | |
{ | |
if (context == NSDraggingContextOutsideApplication) { | |
return NSDragOperationCopy; | |
} | |
return NSDragOperationNone; | |
} | |
-(void) draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint) screenPoint | |
{ | |
for (NSPasteboardItem *item in [[NSPasteboard pasteboardWithName:NSDragPboard] pasteboardItems]) { | |
NSLog(@"%@",[item types]); | |
} | |
} | |
- (void)draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation | |
{ | |
NSLog(@"%@",session); | |
} | |
- (void)pasteboard:(NSPasteboard *)sender item:(NSPasteboardItem *)item provideDataForType:(NSString *)type | |
{ | |
NSLog(@"FIRED ! %@",type); | |
//sender has accepted the drag and now we need to send the data for the type we promised | |
if ( [type compare: NSPasteboardTypeTIFF] == NSOrderedSame ) { | |
[sender setData:[[self image] TIFFRepresentation] forType:NSPasteboardTypeTIFF]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment