if (IOS11) {
UIDragInteraction* drag = [[UIDragInteraction alloc] initWithDelegate:self];
[self addInteraction:drag];
self.userInteractionEnabled = true;
}
**************代理
API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDragInteractionDelegate <NSObject>
@required
//提供數(shù)據(jù)源 長按UI開始拖拽
- (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session;
@optional
//提供UITargetedDragPreview的相關(guān)信息 長按UI是有個(gè)lift(UI舉起效果恩溅,系統(tǒng)自動(dòng)生成)
//返回nil 沒有效果说莫; 不實(shí)現(xiàn)該方法 interaction.view自動(dòng)生成UITargetedDragPreview - (nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForLiftingItem:(UIDragItem *)item session:(id<UIDragSession>)session;
//drag發(fā)生時(shí)調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction willAnimateLiftWithAnimator:(id<UIDragAnimating>)animator session:(id<UIDragSession>)session;
//將要開始drag - (void)dragInteraction:(UIDragInteraction *)interaction sessionWillBegin:(id<UIDragSession>)session;
//是否允許數(shù)據(jù)移動(dòng) app內(nèi)移動(dòng)有效摧莽,跨app總是復(fù)制數(shù)據(jù) - (BOOL)dragInteraction:(UIDragInteraction *)interaction sessionAllowsMoveOperation:(id<UIDragSession>)session;
//是否允許跨應(yīng)用程序進(jìn)行drag ipad - (BOOL)dragInteraction:(UIDragInteraction *)interaction sessionIsRestrictedToDraggingApplication:(id<UIDragSession>)session;
//設(shè)置預(yù)覽視圖是否顯示原始大小 - (BOOL)dragInteraction:(UIDragInteraction *)interaction prefersFullSizePreviewsForSession:(id<UIDragSession>)session;
//已經(jīng)結(jié)束移動(dòng) - (void)dragInteraction:(UIDragInteraction *)interaction sessionDidMove:(id<UIDragSession>)session;
//drag將要結(jié)束時(shí)調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session willEndWithOperation:(UIDropOperation)operation;
//drag已經(jīng)結(jié)束時(shí)調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session didEndWithOperation:(UIDropOperation)operation;
//拖拽源進(jìn)行了放置操作后調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction sessionDidTransferItems:(id<UIDragSession>)session;
//設(shè)置是否允許向拖拽中的項(xiàng)目添加數(shù)據(jù) 返回?cái)?shù)據(jù)載體數(shù)組 drag時(shí)點(diǎn)擊拖拽的組件調(diào)用 - (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForAddingToSession:(id<UIDragSession>)session withTouchAtPoint:(CGPoint)point;
//設(shè)置允許進(jìn)行拖拽中追加數(shù)據(jù)的拖拽行為會(huì)話 - (nullable id<UIDragSession>)dragInteraction:(UIDragInteraction *)interaction sessionForAddingItems:(NSArray<id<UIDragSession>> *)sessions withTouchAtPoint:(CGPoint)point;
//將要向拖拽組件中追加數(shù)據(jù)時(shí)調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session willAddItems:(NSArray<UIDragItem *> *)items forInteraction:(UIDragInteraction *)addingInteraction;
//設(shè)置拖拽動(dòng)作取消的視圖動(dòng)畫 返回nil則消除動(dòng)畫 - (nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForCancellingItem:(UIDragItem *)item withDefault:(UITargetedDragPreview *)defaultPreview;
//drag動(dòng)畫將要取消時(shí)調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction item:(UIDragItem *)item willAnimateCancelWithAnimator:(id<UIDragAnimating>)animator;
API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDropInteractionDelegate <NSObject>
@optional
//是否可以處理來自Drag的數(shù)據(jù)
- (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session;
//Drag的UI元素進(jìn)入Drop的區(qū)域 - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnter:(id<UIDropSession>)session;
//第二次判斷是否可以接收 無法接收用UIDropOperationCancel - (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session;
//Drag的UI元素離開Drop的區(qū)域 - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidExit:(id<UIDropSession>)session;
//取出來自drag的數(shù)據(jù)
1
NSLog(@"%f,%f",[session locationInView:self.view].x,[session locationInView:self.view].y) - (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session;
//drop結(jié)束 - (void)dropInteraction:(UIDropInteraction *)interaction concludeDrop:(id<UIDropSession>)session;
//整個(gè)Drag和Drop結(jié)束 - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnd:(id<UIDropSession>)session;
//手指松開,控制Drag Preview 如何自然的過渡到Drop之后的Preview - (nullable UITargetedDragPreview *)dropInteraction:(UIDropInteraction *)interaction previewForDroppingItem:(UIDragItem *)item withDefault:(UITargetedDragPreview *)defaultPreview;
//手指松開Drop時(shí),控制Drop區(qū)域的其他UI元素如何展示動(dòng)畫 - (void)dropInteraction:(UIDropInteraction *)interaction item:(UIDragItem *)item willAnimateDropWithAnimator:(id<UIDragAnimating>)animator;