剪切板实现拖拽代码

//

//  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 = [NSData dataWithContentsOfFile:@"/Users/wangqiao/Documents/123.pdf"];

     

        [pasteboard setData:data forType:NSPasteboardTypePDF];

    }

    

}

 

 

 

#pragma mark -

#pragma mark 方式1

-(void)mouseDown:(NSEvent *)theEvent

{

    [self dragFile:@"/Users/longxs/Desktop/PJXPasteboard/PJXPasteboardTests/Info.plist" fromRect:self.bounds slideBack:YES event:theEvent];

}

#pragma mark -

#pragma mark 方式2

-(void)mouseDown1:(NSEvent *)theEvent

{

    

    [self dragPromisedFilesOfTypes: [NSArray arrayWithObject:NSFilenamesPboardType] fromRect:self.bounds source:self slideBack:YES event:theEvent];

}

 

- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context

{

 

    return NSDragOperationCopy;

}

 

- (void)dragImage:(NSImage *)anImage at:(NSPoint)viewLocation offset:(NSSize)initialOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pboard source:(id)sourceObj slideBack:(BOOL)slideFlag

{

    NSImage* image =  [NSImage imageNamed:@"1"];

    [super dragImage:image at:viewLocation offset:initialOffset event:event pasteboard:pboard source:sourceObj slideBack:slideFlag];

}

 

- (NSArray*)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination

{

    NSMutableArray* array = [NSMutableArray array];

    

    

    

    NSString* finderPath = [dropDestination path];

    

    NSString* desFile = [finderPath stringByAppendingPathComponent:@"test.tiff"];

    

    NSImage* image =  [NSImage imageNamed:@"1"];

    NSData* data = [image  TIFFRepresentation];

    [data writeToFile:desFile atomically:YES];

    

    [array addObject:desFile];

    return array;

    

}

 

 

 

@end

 

转载于:https://www.cnblogs.com/PJXWang/p/5820091.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值