iOS开发Drag and Drop简介
1、Drag and Drop简介
Drag and Drop是iOS11的新特性,可以将文本、图片进行拖拽到不同app中,实现数据的传递。只不过只能在iPad上使用,iPhone上只能app内部拖拽!
2、简单使用
相关代码:
#import "ViewController.h"
#define CScreenWidth [[UIScreen mainScreen] bounds].size.width
#define CScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenHeight self.view.frame.size.height
#define ScreenWidth self.view.frame.size.width @interface ViewController ()<UIDragInteractionDelegate,UIDropInteractionDelegate> @property (nonatomic, strong)UIImageView *img;
@property (nonatomic, strong)UIImageView *img2; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.img =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic4"]];
self.img.frame = CGRectMake(, , CScreenWidth-, );
self.img.userInteractionEnabled = YES;
[self.view addSubview:self.img];
//Drag发送数据,Drop接收数据
UIDragInteraction *dragInter = [[UIDragInteraction alloc] initWithDelegate:self];
dragInter.enabled = YES;
[self.img addInteraction:dragInter];
[self.view addInteraction:[[UIDropInteraction alloc] initWithDelegate:self]]; self.img2 =[[UIImageView alloc] init];
self.img2.frame = CGRectMake(, CScreenHeight-, CScreenWidth-, );
self.img2.userInteractionEnabled = YES;
[self.view addSubview:self.img2]; }
#pragma mark -UIDragInteractionDelegate
//提供数据源 开始拖拽
- (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session{
NSLog(@"11111----");
self.img2.image = nil;
NSItemProvider *item = [[NSItemProvider alloc] initWithObject:self.img.image];
UIDragItem *dragItem = [[UIDragItem alloc] initWithItemProvider:item];
dragItem.localObject = self.img.image;
return @[dragItem];
}
//提供preview相关信息
- (nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForLiftingItem:(UIDragItem *)item session:(id<UIDragSession>)session{
NSLog(@"22222----");
UIDragPreviewParameters *parameters = [[UIDragPreviewParameters alloc] init];
//设置蒙版mask
parameters.visiblePath = [UIBezierPath bezierPathWithRoundedRect:self.img.bounds cornerRadius:];
//设置在哪个父视图和哪个位置展示
UIDragPreviewTarget *target = [[UIDragPreviewTarget alloc] initWithContainer:self.img.superview center:self.img.center];
UITargetedDragPreview *dragePreview = [[UITargetedDragPreview alloc] initWithView:interaction.view parameters:parameters target:target];
return dragePreview;
}
//drag进行时
- (void)dragInteraction:(UIDragInteraction *)interaction willAnimateLiftWithAnimator:(id<UIDragAnimating>)animator session:(id<UIDragSession>)session{
NSLog(@"33333----");
[animator addCompletion:^(UIViewAnimatingPosition finalPosition) {
if (finalPosition == UIViewAnimatingPositionEnd) {
self.img.alpha = 0.5;
}
}];
}
//drag将要结束时调用
- (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session willEndWithOperation:(UIDropOperation)operation{
NSLog(@"44444----");
[UIView animateWithDuration:0.5 animations:^{
self.img.alpha = ;
}];
}
//drag动画将要取消时
- (void)dragInteraction:(UIDragInteraction *)interaction item:(UIDragItem *)item willAnimateCancelWithAnimator:(id<UIDragAnimating>)animator{
NSLog(@"55555----");
[animator addAnimations:^{
self.img.alpha = .;
}];
}
//drag已经结束时调用
- (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session didEndWithOperation:(UIDropOperation)operation{
NSLog(@"66666----");
[UIView animateWithDuration:0.5 animations:^{
self.img.alpha = ;
}];
} #pragma mark --UIDropInteractionDelegate
//是否可以接收来自Drag数据
- (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session{
if(session.localDragSession == nil){//说明数据来自外界
}
return [session canLoadObjectsOfClass:[UIImage class]];
} //第二次判断是否可以接收 无法接收用UIDropOperationCancel
- (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session{
UIDropOperation opera = session.localDragSession? UIDropOperationCopy:UIDropOperationCancel;
return [[UIDropProposal alloc] initWithDropOperation:opera];
} //取出来自drag的数据
- (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session{
if (session.localDragSession) {
NSProgress *pregesss = [session loadObjectsOfClass:[UIImage class]
completion:^(NSArray<__kindof id<NSItemProviderReading>> * _Nonnull objects) {
// 回调的代码块默认就在主线程
for (id objc in objects) {
UIImage *image = (UIImage *)objc;
if (image) {
self.img2.image = image;
}
}
}];
}
} @end
DragAndDrop.m文件
3、有关方法简介
UIDragInteraction和Delegate方法:
@protocol UIDragInteractionDelegate, UIDragSession;
@class UIDragItem, UITargetedDragPreview;
API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDragAnimating <NSObject>
- (void)addAnimations:(void (^)(void))animations;
- (void)addCompletion:(void (^)(UIViewAnimatingPosition finalPosition))completion;
@end UIKIT_EXTERN API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @interface UIDragInteraction : NSObject <UIInteraction>
- (instancetype)initWithDelegate:(id<UIDragInteractionDelegate>)delegate NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@property (nonatomic, nullable, readonly, weak) id<UIDragInteractionDelegate> delegate;
@property (nonatomic) BOOL allowsSimultaneousRecognitionDuringLift;
@property (nonatomic, getter=isEnabled) BOOL enabled;
@property (class, nonatomic, readonly, getter=isEnabledByDefault) BOOL enabledByDefault;
@end API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDragInteractionDelegate <NSObject>
@required
//提供数据源 长按UI开始拖拽
- (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session;
@optional
//提供UITargetedDragPreview的相关信息 长按UI是有个lift(UI举起效果,系统自动生成)
//返回nil 没有效果; 不实现该方法 interaction.view自动生成UITargetedDragPreview
- (nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForLiftingItem:(UIDragItem *)item session:(id<UIDragSession>)session;
//drag发生时调用
- (void)dragInteraction:(UIDragInteraction *)interaction willAnimateLiftWithAnimator:(id<UIDragAnimating>)animator session:(id<UIDragSession>)session;
//将要开始drag
- (void)dragInteraction:(UIDragInteraction *)interaction sessionWillBegin:(id<UIDragSession>)session;
//是否允许数据移动 app内移动有效,跨app总是复制数据
- (BOOL)dragInteraction:(UIDragInteraction *)interaction sessionAllowsMoveOperation:(id<UIDragSession>)session;
//是否允许跨应用程序进行drag ipad
- (BOOL)dragInteraction:(UIDragInteraction *)interaction sessionIsRestrictedToDraggingApplication:(id<UIDragSession>)session;
//设置预览视图是否显示原始大小
- (BOOL)dragInteraction:(UIDragInteraction *)interaction prefersFullSizePreviewsForSession:(id<UIDragSession>)session;
//已经结束移动
- (void)dragInteraction:(UIDragInteraction *)interaction sessionDidMove:(id<UIDragSession>)session;
//drag将要结束时调用
- (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session willEndWithOperation:(UIDropOperation)operation;
//drag已经结束时调用
- (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session didEndWithOperation:(UIDropOperation)operation;
//拖拽源进行了放置操作后调用
- (void)dragInteraction:(UIDragInteraction *)interaction sessionDidTransferItems:(id<UIDragSession>)session;
//设置是否允许向拖拽中的项目添加数据 返回数据载体数组 drag时点击拖拽的组件调用
- (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForAddingToSession:(id<UIDragSession>)session withTouchAtPoint:(CGPoint)point;
//设置允许进行拖拽中追加数据的拖拽行为会话
- (nullable id<UIDragSession>)dragInteraction:(UIDragInteraction *)interaction sessionForAddingItems:(NSArray<id<UIDragSession>> *)sessions withTouchAtPoint:(CGPoint)point;
//将要向拖拽组件中追加数据时调用
- (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session willAddItems:(NSArray<UIDragItem *> *)items forInteraction:(UIDragInteraction *)addingInteraction;
//设置拖拽动作取消的视图动画 返回nil则消除动画
- (nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForCancellingItem:(UIDragItem *)item withDefault:(UITargetedDragPreview *)defaultPreview;
//drag动画将要取消时调用
- (void)dragInteraction:(UIDragInteraction *)interaction item:(UIDragItem *)item willAnimateCancelWithAnimator:(id<UIDragAnimating>)animator;
UIDropInteraction和Delegate方法:
@protocol UIDragAnimating, UIDropInteractionDelegate, UIDropSession;
@class UIDragItem, UITargetedDragPreview; UIKIT_EXTERN API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @interface UIDropInteraction : NSObject <UIInteraction> - (instancetype)initWithDelegate:(id<UIDropInteractionDelegate>)delegate NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; @property (nonatomic, nullable, readonly, weak) id<UIDropInteractionDelegate> delegate;
@property (nonatomic, assign) BOOL allowsSimultaneousDropSessions;
@end
typedef NS_ENUM(NSUInteger, UIDropOperation) {
UIDropOperationCancel = ,
UIDropOperationForbidden = ,
UIDropOperationCopy = ,
UIDropOperationMove = ,
} API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos); UIKIT_EXTERN API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @interface UIDropProposal : NSObject <NSCopying>
- (instancetype)initWithDropOperation:(UIDropOperation)operation NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@property (nonatomic, readonly) UIDropOperation operation;
@property (nonatomic, getter=isPrecise) BOOL precise;
@property (nonatomic) BOOL prefersFullSizePreview;
@end API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDropInteractionDelegate <NSObject> @optional
//是否可以处理来自Drag的数据
- (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session;
//Drag的UI元素进入Drop的区域
- (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnter:(id<UIDropSession>)session;
//第二次判断是否可以接收 无法接收用UIDropOperationCancel
- (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session;
//Drag的UI元素离开Drop的区域
- (void)dropInteraction:(UIDropInteraction *)interaction sessionDidExit:(id<UIDropSession>)session;
//取出来自drag的数据
NSLog(@"%f,%f",[session locationInView:self.view].x,[session locationInView:self.view].y)
- (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session;
//drop结束
- (void)dropInteraction:(UIDropInteraction *)interaction concludeDrop:(id<UIDropSession>)session;
//整个Drag和Drop结束
- (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnd:(id<UIDropSession>)session;
//手指松开,控制Drag Preview 如何自然的过渡到Drop之后的Preview
- (nullable UITargetedDragPreview *)dropInteraction:(UIDropInteraction *)interaction previewForDroppingItem:(UIDragItem *)item withDefault:(UITargetedDragPreview *)defaultPreview;
//手指松开Drop时,控制Drop区域的其他UI元素如何展示动画
- (void)dropInteraction:(UIDropInteraction *)interaction item:(UIDragItem *)item willAnimateDropWithAnimator:(id<UIDragAnimating>)animator;
@end
iOS开发Drag and Drop简介的更多相关文章
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
- HTML 5 drag and drop 简介
Html 5 drag and drop 简介 HTML5提供了专门拖拽和拖放的API draggable属性 启用拖拽 draggable属性是否可被拖拽, 可选值: true, false, au ...
- iOS开发多线程篇—多线程简介
iOS开发多线程篇-多线程简介 一.进程和线程 1.什么是进程 进程是指在系统中正在执行的一个应用程序 每一个进程之间是独立的.每一个进程均执行在其专用且受保护的内存空间内 比方同一时候打开QQ.Xc ...
- iOS开发进阶--1.多线程简介
学习是由已知的知识模型推理未知的知识模型的的过程. 本文适合学习完objective-c基础,想进一步提高做iOS开发的同学阅读. 在说线程的时候,我们先看看进程. 1.进程 每一个运行在系统中的应用 ...
- iOS开发多线程篇—GCD简介
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
- iOS开发UITouch触摸API简介
1.UITouch简介 当用户触摸屏幕时,会创建一个UITouch对象: UITouch的作用保存着触摸相关的信息,比如触摸的位置.时间.阶段等: 当从开始到结束,系统会更新UITouch对象,结束时 ...
- iOS开发:父子控制器简介:
#import "ViewController.h" #import "ScoietyViewController.h" #import "HotVi ...
- 文顶顶iOS开发博客链接整理及部分项目源代码下载
文顶顶iOS开发博客链接整理及部分项目源代码下载 网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...
- IOS开发——使用数据库
IOS开发——使用FMDB数据库 简介 需求作用: 如果需要保存大量的结构较为复杂的数据的时候,使用数据库,例如交规考试项目 1.数据库的基本介绍 数据库(DB)是一种数据模型组织起来并存放存储管理的 ...
随机推荐
- 杭电多校第一场-M-Code
题目描述 After returning with honour from ICPC(International Cat Programming Contest) World Finals, Tom ...
- 笔记34 Spring MVC的高级技术——处理multipart形式的数据
一.需求介绍: Spittr应用在两个地方需要文件上传.当新用户注册应用的时候,我 们希望他们能够上传一张图片,从而与他们的个人信息相关联.当用 户提交新的Spittle时,除了文本消息以外,他们可能 ...
- java 面试2019
[第一部分] 面试要领[第1题] 流程必知必会[第2题] JDK源码[第二部分] 类和对象[第二篇] 面向对象基础[第1题] 面向对象是什么?[第2题] 类加载的过程[第3题] 类加载器有哪些[第4题 ...
- vue组件基础之创建与使用
一.创建组件 <script src="vue.js"></script> <!--引入vue.js文件--> <div id=" ...
- Redmine 和GitBlit仓库服务器整合
运行环境: RedMine: 4.0.4 Git 仓库: Gitbilt V1.8.0 必须: Redmine 安装并可运行 Redmine运行的主机里面已经安装了 Git,因需要在命令行中运行 gi ...
- java web应用用户上传图片的存储地址
原来工程的上传图片存储地址在web应用的目录下,并且是硬编码到其中的: 每次使用maven tomcat:redeploy以后,这个目录就没有了. 现在想要把上传图片的位置移动到tomcat的weba ...
- 报错 DOMDocument not found
php -m 查看有没有dom扩展 没有安装扩展 yum install php-dom php 常用扩展有 yum install php-solr php-opcache php-seasLog ...
- ps切图的基本操作
参考线和辅助线 ctrl+r呼出标尺,只有在移动工具(快捷键v)下,鼠标左键从标尺上可以拖出来新的参考线.将参考线拖回标尺即是删除. 导出切片 快捷键ctrl+alt+shift+s ,选择png-2 ...
- IDEA工具下Mybaties框架快速入门程序
本篇文章介绍在IDEA工具下mybatis快速入门程序分为以下五步 1 添加依赖包 2 编写pojo对象 3 编写映射文件 4 编写核心配置文件 5 测试框架 详细如下 建立Mod ...
- zjoi 2008 树的统计——树链剖分
比较基础的一道树链剖分的题 大概还是得说说思路 树链剖分是将树剖成很多条链,比较常见的剖法是按儿子的size来剖分,剖分完后对于这课树的询问用线段树维护——比如求路径和的话——随着他们各自的链向上走, ...