首页 > 代码库 > 剪切板实现拖拽代码
剪切板实现拖拽代码
//
// PJXDragAndDropView.m
// PJXPasteboard
//
// Created by 王俏 on 16/8/29.
// Copyright © 2016年 wangqiao. All rights reserved.
//
#import "PJXDragAndDropView.h"
NSString *kPrivateDragUTI = @"com.yourcompany.cocoadraganddrop";
@implementation PJXDragAndDropView{
NSImage *_image;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)event
{
/*------------------------------------------------------
accept activation click as click in window
--------------------------------------------------------*/
//so source doesn‘t have to be the active window
return YES;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
if (_image) {
[_image drawInRect:NSMakeRect(100, 100, _image.size.width, _image.size.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 ];
}
// Drawing code here.
}
-(void)awakeFromNib
{
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,NSFileContentsPboardType,nil]];
}
- (void)mouseDown:(NSEvent*)event
{
/*------------------------------------------------------
catch mouse down events in order to start drag
--------------------------------------------------------*/
/* Dragging operation occur within the context of a special pasteboard (NSDragPboard).
* All items written or read from a pasteboard must conform to NSPasteboardWriting or
* NSPasteboardReading respectively. NSPasteboardItem implements both these protocols
* and is as a container for any object that can be serialized to NSData. */
NSPasteboardItem *pbItem = [NSPasteboardItem new];
/* Our pasteboard item will support public.tiff, public.pdf, and our custom UTI (see comment in -draggingEntered)
* representations of our data (the image). Rather than compute both of these representations now, promise that
* we will provide either of these representations when asked. When a receiver wants our data in one of the above
* representations, we‘ll get a call to the NSPasteboardItemDataProvider protocol method –pasteboard:item:provideDataForType:. */
// [pbItem ]
[pbItem setDataProvider:self forTypes:[NSArray arrayWithObjects:NSPasteboardTypePDF,NSPasteboardTypeString,NSPasteboardTypeTIFF, nil]];//, NSPasteboardTypePDF, kPrivateDragUTI,
//create a new NSDraggingItem with our pasteboard item.
NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pbItem];
// [pbItem release];
/* The coordinates of the dragging frame are relative to our view. Setting them to our view‘s bounds will cause the drag image
* to be the same size as our view. Alternatively, you can set the draggingFrame to an NSRect that is the size of the image in
* the view but this can cause the dragged image to not line up with the mouse if the actual image is smaller than the size of the
* our view. */
NSRect draggingRect = NSMakeRect(100, 100, _image.size.width, _image.size.height);
/* While our dragging item is represented by an image, this image can be made up of multiple images which
* are automatically composited together in painting order. However, since we are only dragging a single
* item composed of a single image, we can use the convince method below. For a more complex example
* please see the MultiPhotoFrame sample. */
[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;
draggingSession.draggingFormation = NSDraggingFormationNone;
}
-(void)moveUp:(id)sender
{
}
#pragma mark -- NSDraggingSource
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context
{
switch (context) {
case NSDraggingContextOutsideApplication:
return NSDragOperationCopy;
//by using this fall through pattern, we will remain compatible if the contexts get more precise in the future.
case NSDraggingContextWithinApplication:
default:
return NSDragOperationCopy;
break;
}
}
-(NSImage* )image{
return _image;
}
#pragma mark -- NSDraggingDestination
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])==NSDragOperationGeneric) {
return NSDragOperationGeneric;
}else{
return NSDragOperationNone;
}
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
== NSDragOperationGeneric)
{
//this means that the sender is offering the type of operation we want
//return that we want the NSDragOperationGeneric operation that they
//are offering
return NSDragOperationGeneric;
}
else
{
//since they aren‘t offering the type of operation we want, we have
//to tell them we aren‘t interested
return NSDragOperationNone;
}
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
if ( [sender draggingSource] == self ) {
NSURL* fileURL;
fileURL=[NSURL URLFromPasteboard: [sender draggingPasteboard]];
NSLog(@"fileURL:%@",fileURL);
}else{
// NSPasteboard *zPasteboard = [sender draggingPasteboard];
// NSArray *list = [zPasteboard propertyListForType:NSFilenamesPboardType];
_image = [NSImage imageNamed:@"add"];
[self setNeedsDisplay:YES];
//add file
}
return YES;
}
#pragma mark -- NSPasteboardItemDataProvider
- (void)pasteboard:(nullable NSPasteboard *)pasteboard item:(NSPasteboardItem *)item provideDataForType:(NSString *)type
{
if ( [type compare: NSPasteboardTypeTIFF] == NSOrderedSame ) {
//set data for TIFF type on the pasteboard as requested
[pasteboard setData:[[self image] TIFFRepresentation] forType:NSPasteboardTypeTIFF];
} else if ( [type compare: NSPasteboardTypePDF] == NSOrderedSame ) {
//set data for PDF type on the pasteboard as requested
NSData *data = http://www.mamicode.com/[NSData dataWithContentsOfFile:@"/Users/wangqiao/Documents/123.pdf"];
[pasteboard setData:data forType:NSPasteboardTypePDF];
}
}
@end
剪切板实现拖拽代码