剪切板实现拖拽代码

//

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

}

}

@end

时间: 2024-10-14 18:51:16

剪切板实现拖拽代码的相关文章

Xcode如何拖拽选中文字、拖拽代码

不管是文本编辑,还是代码工具,一般都提供了用鼠标拖拽选中文字到指定地方的功能,但是在Xcode里貌似这样有点儿难,你会发现当想拖动的时候会有时候成功,但是大部分时间都是又处于选择状态.一开始我以为是Mac系统的原因,后来发现在其他地方:浏览器就可以拖动,我想这可能是Xcode里去掉了,但是有时候能拖拽是怎么回事.好吧,我不coding了,非得找出来. 废话不多说了: 点击选中的代码(文字),不要移动和松开鼠标左键,当竖线变成箭头之后就可以拖动了,其实不需要多少时间,基本上就是:不要直接点鼠标左键

原生弹窗拖拽代码demo+简单的抽奖

拖拽效果 效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>弹窗拖拽</title> <style> *{margin:0;padding:0;} .box{position: absolute;width: 400px;height: 300px;top:100px;left:

storyboard之拖拽代码到你的工程

自己封装的组件要如何高效的保存和被下次使用呢?答案我来告诉你: 使用storyboard 的  Code Snippet(代码片段) 你也许经常看到storyboard上的这个东西,却从来没有用过这个高级的玩意儿,今天我来给大家演示一下,如何封装自定义的代码到 storyboard中去,已便下次快速的使用: 1.如何使用?: 上面已经讲到,只需要拖拽你想用的代码块(例如一个block块,一个GCD单例块,一个 FRC块)到工程即可,苹果为你提供了一些,你可能会用到的 code snippet 2

Unity的拖拽代码

using UnityEngine; using System.Collections; public class MyDragDropItem :UIDragDropItem{ protected override void OnDragDropRelease(GameObject surface){ base.OnDragDropRelease(surface); //进行我们的处理代码了 print(surface); } }

简洁易行的JS拖拽代码

var x, y var drag_ = false var D = new Function('obj', 'return document.getElementById(obj);') var IE = new Function( '', 'if(navigator.appName.indexOf("Explorer")>=0)return 1;if(navigator.appName.indexOf("Netscape")>=0)return 2'

通过Ztree生成页面html元素Dom树,以及拖拽改变元素的位置

zTree 是一款依靠 jQuery 实现的多功能 "树插件",http://www.treejs.cn/v3/main.php#_zTreeInfo,功能强大,不多赘述. 下面我将介绍如何实现使用该插件生成HTML元素Dom树,并对其进行多样操作. 先贴上一个简单的HTML页面(直接拿的ztree的用的,画面简单实用,里面的文字内容不用在意) 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-e

HTML页面居中弹出自定义窗口层(实现可拖拽)

使用DIV弹出窗口来动态显示内容的原理:首先采用CSS和HTML隐藏弹窗中的内容,然后利用JavaScript(本教程中是JQuery)来动态显示它们.这种效果不仅能够充分利用有限的版面空间,而且能够提高用户体验:更重要的是,它并不影响SEO效果(因为它实际存在于页面中,只是初始为不可见状态) 1.在html页面中定义一个div,并在div实现我们需要展示的内容. <body> <div id="login"> <h2><img src=&qu

拖拽系列二、利用JS面向对象OOP思想实现拖拽封装

接着上一篇拖拽系列一.JavaScript实现简单的拖拽效果这一篇博客将接着对上一节实现代码利用JS面向对象(OOP)思维对上一节代码进行封装; 使其模块化.避免全局函数污染.方便后期维护和调用:写到这里突然想起一句话“没有任何一个题目是彻底完成的.总还会有很多事情可做......” 我想这句话程序开发大概也适用吧,前端开发人员总是可以结合自己之前学到“拖拽”相关知识,不断扩展.完善.无穷无尽.......     利用匿名函数自执行实现封装 ;(function(){ //do somethi

canvasn拖拽效果

canvas拖拽和平时用的js拖拽是有区别的 普通的js是设置目标为绝对定位,再根据鼠标的移动来改变left和top的值 canvas是获得了鼠标的位置,直接在目标点进行重新绘制 下面给一个简单的拖拽代码 <canvas id="can" width="400" height="400"></canvas> <script type="text/javascript"> var can =