zoukankan      html  css  js  c++  java
  • 剪切板实现拖拽代码

    //

    //  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

  • 相关阅读:
    8款超好用的SVG编辑工具用起来
    连接数据后,当执行查询语句报错:ORA-01219: 数据库未打开: 仅允许在固定表/视图中查询
    idea注册码
    chrome 打开本地 pdf 会自动开启下载
    exp/imp 注释乱码问题或Oracle EXP-00091的解决方法
    帆软报表 异常汇总及方案.
    Intellij IDEA的Facets和Artifacts
    Diamond types are not supported at language level '5‘
    IDEA Error:java: Compilation failed: internal java compiler error
    IdentityServer Topics(5)- 使用第三方登录
  • 原文地址:https://www.cnblogs.com/PJXWang/p/5820091.html
Copyright © 2011-2022 走看看