爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      iOS11新特性:新增拖拽交互体验

      首页 知识中心 其他 文章详情页

      iOS11新特性:新增拖拽交互体验

      2024-06-18 07:22:41 阅读次数:43

      组件

      iOS11新特性:新增拖拽交互体验

      一、引言

          在使用PC进行操作时,你一定遇到过这样的场景,可以将图片直接拖入聊天软件进行发送,可以将文档、音乐、视频文件等文件拖入相应应用程序直接进行使用。这种拖拽操作交互极大的方便了电脑的使用。在iOS11中,你可以在iPhone或iPad上构建这种交互体验!

      说在前面的话:

            拖拽操作在iPad上是支持跨应用程序的,你可以从一个应用中拖取项目,通过Home键回到主界面并且打开另一个应用程序,然后将被拖拽的项目传递给这个应用程序中。在iPhone上,拖拽操作只支持当前应用程序内,你可以将某个元素从一个界面拖拽到另一个,这种维度的操作可以给设计人员更大的灵活性。

            拖拽操作被设计成系统管理,开发者不需要为App申请特殊的用户权限。    

      二、拖拽源

          对于拖拽操作,至少要有两个组件,一个组件作为拖拽源用来提供数据,一个组件作为拖拽目的用来接收数据,当前,同一个组件既可以是拖拽源也可以是拖拽目的。首先我们先来看拖拽源,在UIKit框架中,iOS11默认实现了一些组件可以作为拖拽源, 例如UITextField、UITextView、UITableView和UICollectionView等。文本组件默认支持拖拽操作进行文本的传递,对于列表组件则默认支持元素的拖拽。例如,在UITextField选中的文案中进行拖拽,可以将文字拖拽出来,效果如下图:

      iOS11新特性:新增拖拽交互体验

      - (void)viewDidLoad {
      [super viewDidLoad];
      [self.view addSubview:self.dragView];
      }
      //创建View
      -(UIView *)dragView{
      if (!_dragView) {
      _dragView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
      _dragView.backgroundColor = [UIColor redColor];
      [_dragView addInteraction:self.dragInteraction];
      }
      return _dragView;
      }
      //创建拖拽行为对象
      -(UIDragInteraction *)dragInteraction{
      if (!_dragInteraction) {
      _dragInteraction = [[UIDragInteraction alloc]initWithDelegate:self];
      //要设置可用 注意!!!
      [_dragInteraction setEnabled:YES];
      }
      return _dragInteraction;
      }

      //实现提供数据的代理方法
      - (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session{
      //数据提供者
      NSItemProvider * provider = [[NSItemProvider alloc]initWithObject:@"Hello World"];
      UIDragItem * item = [[UIDragItem alloc]initWithItemProvider:provider];
      return @[item];
      }

      上面的dragInteraction:代理方法用来提供要传递的数据,传递的数据必须遵守相应的承诺协议,后面会给大家介绍,这里只是简单返回了一个字符串数据Hello World,运行工程,你可以试验下,可以直接将我们自定义的视图拖拽进UITextField并在其中显示Hello World。

      三、关于UIDragInteraction类

          所有可以接收拖拽行为的组件都必须通过这个类实现,这个类中属性意义列举如下:

      //初始化方法
      - (instancetype)initWithDelegate:(id<UIDragInteractionDelegate>)delegate;
      //代理
      @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;

      四、UIDragInteractionDelegate协议

          UIDragInteractionDelegate用来处理拖拽源的行为与数据。其中定义了一个必须实现的方法和许多可选实现的方法。解析如下:

      /*
      这个方法是必须实现的用来返回拖拽源提供的数据
      需要注意,这个函数需要返回一个数组,数组中可以有多个数据源
      如果返回空数组,则拖拽行为不会开始
      */
      - (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session;
      /*
      这个方法用来自定义拖拽效果的预览视图 关于预览视图,后面会介绍
      需要注意,系统默认会提供一个预览视图,不实现这个方法即是使用系统默认的
      如果返回nil,则会去除预览动画
      */
      - (nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForLiftingItem:(UIDragItem *)item session:(id<UIDragSession>)session;
      /*
      拖拽动画即将开始时会调用此函数
      */
      - (void)dragInteraction:(UIDragInteraction *)interaction willAnimateLiftWithAnimator:(id<UIDragAnimating>)animator session:(id<UIDragSession>)session;
      //拖拽行为会话即将开始时调用的方法
      - (void)dragInteraction:(UIDragInteraction *)interaction sessionWillBegin:(id<UIDragSession>)session;
      //这个方法设置数据的防止是否允许数据的 移动操作,需要注意,这个只有在app内有效,跨app的操作会总是复制数据
      - (BOOL)dragInteraction:(UIDragInteraction *)interaction sessionAllowsMoveOperation:(id<UIDragSession>)session;
      //设置是否允许跨应用程序进行拖拽 ipad
      - (BOOL)dragInteraction:(UIDragInteraction *)interaction sessionIsRestrictedToDraggingApplication:(id<UIDragSession>)session;
      //设置预览视图是否显示原始大小
      - (BOOL)dragInteraction:(UIDragInteraction *)interaction prefersFullSizePreviewsForSession:(id<UIDragSession>)session;
      /*
      当拖拽源被移动时调用,可以用如下方法获取其坐标
      NSLog(@"%f,%f",[session locationInView:self.view].x,[session locationInView:self.view].y);
      */
      - (void)dragInteraction:(UIDragInteraction *)interaction sessionDidMove:(id<UIDragSession>)session;
      //拖拽行为将要结束时调用
      - (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session willEndWithOperation:(UIDropOperation)operation;
      //拖拽行为已经结束时调用
      - (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session didEndWithOperation:(UIDropOperation)operation;
      //拖拽源进行了放置操作后调用
      - (void)dragInteraction:(UIDragInteraction *)interaction sessionDidTransferItems:(id<UIDragSession>)session;
      //设置拖拽动作取消的视图动画 返回nil则消除动画
      -(nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForCancellingItem:(UIDragItem *)item withDefault:(UITargetedDragPreview *)defaultPreview;
      //拖拽动作即将取消时调用的方法
      - (void)dragInteraction:(UIDragInteraction *)interaction item:(UIDragItem *)item willAnimateCancelWithAnimator:(id<UIDragAnimating>)animator;
      //设置是否允许向拖拽中的项目添加数据
      /*
      可以返回数据载体数组 当拖拽过程中 点击可拖拽的组件时会触发
      */
      - (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;

      上面列举的协议方法中有关联到其他许多iOS11中新增的类,后面会一一介绍。其实,完成了以上内容的了解,你就已经可以完全随心所欲的定制拖拽源组件了。

      五、放置目的地

          拖拽源是数据的提供者,放置目的地就是数据的接收者。前面我们也实验过,将自定义的拖拽源拖拽进UITextField后,文本框中会自动填充我们提供的文本数据。同样,对于任何自定义的UIView视图,我们也可以让其成为放置目的地,需要完成如下3步:

      1.创建一个UIDropInteraction行为对象。

      2.设置UIDropInteraction对象的代理并实现协议方法。

      3.将其添加到自定义的视图中。

          例如,我们将自定义的UILabel组件用来显示拖拽的文案:

      //添加视图
      - (void)viewDidLoad {
      [super viewDidLoad];
      //有关拖拽源的代码 前面已经列举过 这里不再重复
      [self.view addSubview:self.dragView];
      [self.view addSubview:self.dropLabel];
      }

      -(UILabel *)dropLabel{
      if (!_dropLabel) {
      _dropLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 300, 300, 30)];
      _dropLabel.backgroundColor = [UIColor greenColor];
      _dropLabel.userInteractionEnabled = YES;
      [_dropLabel addInteraction:self.dropInteraction];
      }
      return _dropLabel;
      }
      //放置目的地行为对象
      -(UIDropInteraction*)dropInteraction{
      if (!_dropInteraction) {
      _dropInteraction = [[UIDropInteraction alloc]initWithDelegate:self];
      }
      return _dropInteraction;
      }

      //这个方法返回是否响应此放置目的地的放置请求
      -(BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session{
      return YES;
      }
      //设置以何种方式响应拖放会话行为
      -(UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session{
      return [[UIDropProposal alloc]initWithDropOperation:UIDropOperationCopy];
      }
      //已经应用拖放行为后执行的操作
      -(void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session{

      [session loadObjectsOfClass:[NSString class] completion:^(NSArray<__kindof id<NSItemProviderReading>> * _Nonnull objects) {
      self.dropLabel.text = objects.firstObject;
      }];
      }

      上面的代码将我们自定义的拖拽源提供的Hello World拖放进了UILabel组件中。

      六、关于UIDropInteraction类

          与UIDragInteraction类类似,这个类的作用是让组件有相应放置操作的能力。其中属性如下:

      //初始化方法
      - (instancetype)initWithDelegate:(id<UIDropInteractionDelegate>)delegate;
      //代理对象
      @property (nonatomic, nullable, readonly, weak) id<UIDropInteractionDelegate> delegate;
      //是否允许多个交互行为
      @property (nonatomic, assign) BOOL allowsSimultaneousDropSessions;

      七、UIDropInteractionDelegate协议

          UIDropInteractionDelegate协议中所定义的方法全部是可选实现的,其用来处理用户放置交互行为。

      //放置行为即将响应时触发的方法 返回值确定是否响应此次行为
      - (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session;
      //当上面的协议方法返回YES时会接着调用这个函数
      - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnter:(id<UIDropSession>)session;
      //将要处理数据时回调的方法
      /*
      当数据源数据添加时,这个方法也会被重新调用
      这个函数需要返回一个处理行为方式UIDropProposal对象,这个我们后面再说
      */
      - (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session;
      //放置行为相应结束的时候会调用此方法
      - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidExit:(id<UIDropSession>)session;
      //这个方法当用户进行放置时会调用,可以从session中获取被传递的数据
      - (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session;
      //放置动画完成后会调用这个方法
      - (void)dropInteraction:(UIDropInteraction *)interaction concludeDrop:(id<UIDropSession>)session;
      //整个拖放行为结束后会调用
      - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnd:(id<UIDropSession>)session;

      //下面这些方法用来自定义放置动画
      //设置放置预览动画
      - (nullable UITargetedDragPreview *)dropInteraction:(UIDropInteraction *)interaction previewForDroppingItem:(UIDragItem *)item withDefault:(UITargetedDragPreview *)defaultPreview;
      //这个函数每当有一个拖拽数据项放入时都会调用一次 可以进行动画
      - (void)dropInteraction:(UIDropInteraction *)interaction item:(UIDragItem *)item willAnimateDropWithAnimator:(id<UIDragAnimating>)animator;

      需要注意,UIDropProposal类用来进行处理回执,属性方法解析如下:

      //初始化方法
      /*
      typedef NS_ENUM(NSUInteger, UIDropOperation) {
      //取消这次行为
      UIDropOperationCancel = 0,
      //拒绝行为
      UIDropOperationForbidden = 1,
      //接收拷贝数据
      UIDropOperationCopy = 2,
      //接收移动数据
      UIDropOperationMove = 3,
      }
      */
      - (instancetype)initWithDropOperation:(UIDropOperation)operation;
      //处理方式
      @property (nonatomic, readonly) UIDropOperation operation;
      //精准定位
      @property (nonatomic, getter=isPrecise) BOOL precise;
      //设置是否展示完整的预览尺寸
      @property (nonatomic) BOOL prefersFullSizePreview;

      八、拖拽数据载体UIDragItem类

          UIDragItem类用来承载要传递的数据。其通过NSItemProvider类来进行构建,传递的数据类型是有严格规定的,必须遵守一定的协议,系统的NSString,NSAttributeString,NSURL,UIColor和UIImage是默认支持的,你可以直接传递这些数据。

          UIDragItem中提供的属性方法:

      //初始化方法
      - (instancetype)initWithItemProvider:(NSItemProvider *)itemProvider;
      //数据提供者实例
      @property (nonatomic, readonly) __kindof NSItemProvider *itemProvider;
      //用来传递一些额外的关联信息
      @property (nonatomic, strong, nullable) id localObject;
      //用来自定义每个item添加时的预览动画
      @property (nonatomic, copy, nullable) UIDragPreview * _Nullable (^previewProvider)(void);

      九、UIDropSession与UIDragSession

          在与拖拽交互相关的接口中,这两个是面向协议编程的绝佳范例,首先在UIKit框架中只定义了这两个协议,而并没有相关的实现类,在拖拽行为的相关回调接口中,很多id类型的参数都遵守了这个协议,我们无需知道是哪个类实现的,直接进行使用即可:

      UIDropSession:

      //继承于UIDragDropSession(提供基础数据), NSProgressReporting(提供数据读取进度)
      @protocol UIDropSession <UIDragDropSession, NSProgressReporting>
      //原始的dragSesstion会话 如果是跨应用的 则为nil
      @property (nonatomic, readonly, nullable) id<UIDragSession> localDragSession;
      //设置进度风格
      /*
      typedef NS_ENUM(NSUInteger, UIDropSessionProgressIndicatorStyle) {
      UIDropSessionProgressIndicatorStyleNone, // 无
      UIDropSessionProgressIndicatorStyleDefault, // 默认的
      } API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos);

      */
      @property (nonatomic) UIDropSessionProgressIndicatorStyle progressIndicatorStyle;
      //进行数据的加载
      - (NSProgress *)loadObjectsOfClass:(Class<NSItemProviderReading>)aClass completion:(void(^)(NSArray<__kindof id<NSItemProviderReading>> *objects))completion;

      @end

      UIDragSession:

      API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDragSession <UIDragDropSession>
      //设置要传递的额外信息 只有在同个APP内可见
      @property (nonatomic, strong, nullable) id localContext;

      @end

      UIDragDropSession:

      //传递的数据数组
      @property (nonatomic, readonly) NSArray<UIDragItem *> *items;
      //当前操作行为的坐标
      - (CGPoint)locationInView:(UIView *)view;
      //此次行为是否允许移动操作
      @property (nonatomic, readonly) BOOL allowsMoveOperation;
      //是否支持应用程序层面的拖拽
      @property (nonatomic, readonly, getter=isRestrictedToDraggingApplication) BOOL restrictedToDraggingApplication;
      //验证传递的数据是否支持某个数据类型协议
      - (BOOL)hasItemsConformingToTypeIdentifiers:(NSArray<NSString *> *)typeIdentifiers;
      //验证传递的数据是否可以加载某个类
      - (BOOL)canLoadObjectsOfClass:(Class<NSItemProviderReading>)aClass;

      十、交互预览类UITargetedDragPreview

          UITargetedDragPreview专门用来处理拖放交互过程中的动画与预览视图。方法解析如下:

      //创建一个预览对象 
      /*
      view:要创建的预览视图 需要注意,这个视图必须在window上
      param:配置参数
      target:容器视图,用来展示预览,一般设置为view的父视图
      */
      - (instancetype)initWithView:(UIView *)view parameters:(UIDragPreviewParameters *)parameters target:(UIDragPreviewTarget *)target;
      //同上
      -(instancetype)initWithView:(UIView *)view parameters:(UIDragPreviewParameters *)parameters;
      //同上
      - (instancetype)initWithView:(UIView *)view;
      //动画承载者
      @property (nonatomic, readonly) UIDragPreviewTarget* target;
      //动画视图
      @property (nonatomic, readonly) UIView *view;
      //配置参数
      @property (nonatomic, readonly, copy) UIDragPreviewParameters *parameters;
      //尺寸
      @property (nonatomic, readonly) CGSize size;
      //返回新的对象
      - (UITargetedDragPreview *)retargetedPreviewWithTarget:(UIDragPreviewTarget *)newTarget;

      UIDragPreviewTarget主要用来设置动画的起始视图与结束时回归的视图,其中属性方法如下:

      /*
      初始化方法
      container:必须是在window上的view
      center:动画起点与终点
      transform:进行变换
      */
      - (instancetype)initWithContainer:(UIView *)container center:(CGPoint)center transform:(CGAffineTransform)transform;
      //同上
      - (instancetype)initWithContainer:(UIView *)container center:(CGPoint)center;
      //对应属性
      @property (nonatomic, readonly) UIView *container;
      @property (nonatomic, readonly) CGPoint center;
      @property (nonatomic, readonly) CGAffineTransform transform;

      UIDragPreviewParameters用来进行拖拽动画的配置,解析如下:

      //构造方法并设置路径矩形
      - (instancetype)initWithTextLineRects:(NSArray<NSValue /* CGRect */ *> *)textLineRects;
      //显示的路径
      @property (nonatomic, copy, nullable) UIBezierPath *visiblePath;
      //背景色
      @property (nonatomic, copy, null_resettable) UIColor *backgroundColor;

      我们可以使用任意自定义的视图来展现这个预览动画,如下图所示:

      iOS11新特性:新增拖拽交互体验

      十一、使用拖拽操作进行自定义数据的传递

          本篇文章到这里,其实基本的内容都已经说完了,虽然比较详细,也可能难免冗余,如果你耐着性子看到了这里,那么我首先钦佩你的毅力并且感谢你的耐心。其实,拖拽交互如果进行只能对系统的提供的数据类型进行操作则应用就局限太多。试想一下,如果我们可以通过拖拽商品来进行购买,拖拽联系人来进行发送,或者在游戏中,拖拽进行卡片的融合,装备的提炼等等这种交互操作是不是会很畅快。最后,我们就来看看如何让自定义的数据类型支持拖拽操作。

          首先你需要关注两个协议,NSItemProviderWriting与NSItemProviderReading。Writing协议用来让数据支持提供给数据源,Reading协议让数据支持从数据源读出,用自定义的Person类为例:

      #import <Foundation/Foundation.h>
      //遵守协议
      @interface Person : NSObject<NSItemProviderWriting,NSItemProviderReading>
      //自定义内容
      @property(nonatomic,strong)NSString * name;

      @property(nonatomic,assign)NSUInteger age;

      @end

      //.m文件
      @implementation Person
      //数据归档
      - (nullable NSProgress *)loadDataWithTypeIdentifier:(NSString *)typeIdentifier
      forItemProviderCompletionHandler:(void (^)(NSData * _Nullable data, NSError * _Nullable error))completionHandler{
      NSProgress * pro = [NSProgress new];
      NSData * data = [NSKeyedArchiver archivedDataWithRootObject:self];
      completionHandler(data,nil);
      return pro;
      }

      +(NSItemProviderRepresentationVisibility)itemProviderVisibilityForRepresentationWithTypeIdentifier:(NSString *)typeIdentifier{
      return NSItemProviderRepresentationVisibilityAll;
      }

      - (NSItemProviderRepresentationVisibility)itemProviderVisibilityForRepresentationWithTypeIdentifier:(NSString *)typeIdentifier{
      return NSItemProviderRepresentationVisibilityAll;
      }
      //提供一个标识符
      +(NSArray<NSString *> *)writableTypeIdentifiersForItemProvider{
      return @[@"object"];
      }
      -(NSArray<NSString *> *)writableTypeIdentifiersForItemProvider{
      return @[@"object"];
      }

      - (instancetype)initWithCoder:(NSCoder *)coder
      {
      self = [super init];
      if (self) {
      = [coder decodeObjectForKey:@"name"];
      self.age = [coder decodeIntegerForKey:@"age"];
      }
      return self;
      }

      - (void)encodeWithCoder:(NSCoder *)aCoder{
      [aCoder encodeObject: forKey:@"name"];
      [aCoder encodeInteger:self.age forKey:@"age"];
      }

      //这两个是读协议
      +(NSArray<NSString *> *)readableTypeIdentifiersForItemProvider{
      return @[@"object"];
      }
      //解归档返回
      + (nullable instancetype)objectWithItemProviderData:(NSData *)data
      typeIdentifier:(NSString *)typeIdentifier
      error:(NSError **)outError{
      Person * p = [NSKeyedUnarchiver unarchiveObjectWithData:data];
      return p;
      }

      @end

      需要注意,在拖放行为读取数据时的类型要对应,如下:

      -(void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session{
      NSLog(@"%@",session.items.lastObject.localObject);
      [session loadObjectsOfClass:[Person class] completion:^(NSArray<__kindof id<NSItemProviderReading>> * _Nonnull objects) {
      self.dropLabel.text = ((Person*)objects.firstObject).name;
      }];
      }
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/u_11643026/4284110,作者:珲少,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:c++宾馆房间(差分数组)

      下一篇:js:金额显示转换 分转元 去除尾部的0

      相关文章

      2025-05-19 09:04:44

      js原生手写一个拖拽小功能

      js原生手写一个拖拽小功能

      2025-05-19 09:04:44
      js , 手写
      2025-05-09 08:50:35

      Element学习(布局组件、案例操作)(4)

      Element学习(布局组件、案例操作)(4)

      2025-05-09 08:50:35
      展示 , 布局 , 效果 , 查询 , 组件 , 表单 , 表示
      2025-04-23 08:18:27

      结构型模式---装饰模式

      装饰模式是一种结构形模式,允许通过将对象放入包含行为的特殊封装对象(装饰器)中来为原来对象(组件)绑定新的行为。

      2025-04-23 08:18:27
      对象 , 模式 , 组件 , 行为 , 装饰
      2025-04-22 09:40:08

      【ETL工具】Kettle 调优 (使用阻塞组件的同时数据量大)

      【ETL工具】Kettle 调优 (使用阻塞组件的同时数据量大)

      2025-04-22 09:40:08
      组件 , 缓存 , 队列
      2025-04-22 09:28:19

      Vue3中如何实现动态菜单递归

      Vue3中如何实现动态菜单递归

      2025-04-22 09:28:19
      实现 , 组件 , 菜单 , 路由 , 递归
      2025-04-18 07:11:32

      golang实战项目log2metrics架构说明

      golang实战项目log2metrics架构说明

      2025-04-18 07:11:32
      code , 日志 , 组件
      2025-04-18 07:10:38

      设计模式-适配器模式

      适配器模式(Adapter Pattern)是一种结构型设计模式,它允许不兼容的接口能够一起工作。这种模式通常用于使现有的类与其他类一起工作,而无需修改它们的源代码。在 JavaScript 中,适配器模式可以用来确保不同的类或对象之间能够通过一个统一的接口进行交互。

      2025-04-18 07:10:38
      接口 , 模式 , 组件 , 适配 , 适配器
      2025-04-18 07:10:38

      设计模式-组合模式

      组合模式(Composite Pattern)在设计中的应用是为了简化对复杂树形结构的管理和操作。这个模式能够让客户端通过一个统一的接口操作单个对象和组合对象。我们可以通过一个具体的例子来进一步理解组合模式在实际应用中的作用和实现。

      2025-04-18 07:10:38
      客户端 , 接口 , 操作 , 模式 , 组件 , 组合
      2025-04-18 07:10:30

      React之withRouter的作用和应用

      React之withRouter的作用和应用

      2025-04-18 07:10:30
      history , math , props , 组件 , 路由 , 跳转
      2025-04-16 09:26:39

      prometheus为k8s做的4大适配工作

      prometheus为k8s做的4大适配工作

      2025-04-16 09:26:39
      k8s , pod , 指标 , 组件 , 适配
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5241238

      查看更多

      最新文章

      js原生手写一个拖拽小功能

      2025-05-19 09:04:44

      Element学习(布局组件、案例操作)(4)

      2025-05-09 08:50:35

      结构型模式---装饰模式

      2025-04-23 08:18:27

      Vue3中如何实现动态菜单递归

      2025-04-22 09:28:19

      React之withRouter的作用和应用

      2025-04-18 07:10:30

      prometheus为k8s做的4大适配工作

      2025-04-16 09:26:39

      查看更多

      热门文章

      【DockerImage】修复Docker镜像的组件安全漏洞(原创)

      2024-07-01 01:32:37

      React-组件-setState

      2023-06-16 06:09:26

      Vue封装组件库-input组件

      2023-06-08 06:21:43

      Window组件

      2023-05-25 14:43:15

      Vue番外之.sync修饰符

      2024-09-24 06:30:37

      React-生命周期-其它方法

      2023-06-16 06:08:43

      查看更多

      热门标签

      linux java python javascript 数组 前端 docker Linux vue 函数 shell git 节点 容器 示例
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      React之withRouter的作用和应用

      Vue的优化

      Vue中mixins(混入)的介绍和使用

      HarmonyOS 学习笔记-HarmonyOS中的页面导航机制

      Vue封装组件库-input组件

      详细分析Vue3中的emit用法(子传父)

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号