美文网首页
MacOs监听文件拖拽入窗口

MacOs监听文件拖拽入窗口

作者: xiaosecond | 来源:发表于2018-11-20 23:02 被阅读0次

    ********************************************************************

    #import <Cocoa/Cocoa.h>

    @interface DragView : NSView

    @property(nonatomic,assign)BOOL isDragIn;

    @end

    *************************************************************************

    ***************************************************************************

    #import "DragView.h"

    @implementation DragView

    - (void)drawRect:(NSRect)dirtyRect {

        [super drawRect:dirtyRect];

        [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,nil]];

        if(_isDragIn) {

            NSLog(@"拖拽了");

        }

        // Drawing code here.

    }

    - (NSDragOperation)draggingEntered:(id)sender{

        _isDragIn=YES;

        [self setNeedsDisplay:YES];

        return NSDragOperationCopy;

    }

    - (void)draggingExited:(id)sender{

        _isDragIn=NO;

        [self setNeedsDisplay:YES];

    }

    - (BOOL)prepareForDragOperation:(id)sender{

        _isDragIn=NO;

        [self setNeedsDisplay:YES];

        return YES;

    }

    - (BOOL)performDragOperation:(id)sender{

        if([sender draggingSource] !=self){

            NSArray* filePaths = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];

            NSLog(@"文件地址%@",filePaths);

        }

        return YES;

    }

    @end

    ***************************************************************************************

    相关文章

      网友评论

          本文标题:MacOs监听文件拖拽入窗口

          本文链接:https://www.haomeiwen.com/subject/wlvfqqtx.html