iOS开发Drag and Drop简介
生活随笔
收集整理的這篇文章主要介紹了
iOS开发Drag and Drop简介
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、Drag and Drop簡(jiǎn)介
Drag and Drop是iOS11的新特性,可以將文本、圖片進(jìn)行拖拽到不同app中,實(shí)現(xiàn)數(shù)據(jù)的傳遞。只不過只能在iPad上使用,iPhone上只能app內(nèi)部拖拽!
?
2、簡(jiǎn)單使用
相關(guān)代碼:
#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(20, 40, CScreenWidth-40, 200);self.img.userInteractionEnabled = YES;[self.view addSubview:self.img];//Drag發(fā)送數(shù)據(jù),Drop接收數(shù)據(jù)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(20, CScreenHeight-250, CScreenWidth-40, 200);self.img2.userInteractionEnabled = YES;[self.view addSubview:self.img2];} #pragma mark -UIDragInteractionDelegate //提供數(shù)據(jù)源 開始拖拽 - (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相關(guān)信息 - (nullable UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForLiftingItem:(UIDragItem *)item session:(id<UIDragSession>)session{NSLog(@"22222----");UIDragPreviewParameters *parameters = [[UIDragPreviewParameters alloc] init];//設(shè)置蒙版maskparameters.visiblePath = [UIBezierPath bezierPathWithRoundedRect:self.img.bounds cornerRadius:20];//設(shè)置在哪個(gè)父視圖和哪個(gè)位置展示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進(jìn)行時(shí) - (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將要結(jié)束時(shí)調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session willEndWithOperation:(UIDropOperation)operation{NSLog(@"44444----");[UIView animateWithDuration:0.5 animations:^{self.img.alpha = 1;}]; } //drag動(dòng)畫將要取消時(shí) - (void)dragInteraction:(UIDragInteraction *)interaction item:(UIDragItem *)item willAnimateCancelWithAnimator:(id<UIDragAnimating>)animator{NSLog(@"55555----");[animator addAnimations:^{self.img.alpha = .1;}]; } //drag已經(jīng)結(jié)束時(shí)調(diào)用 - (void)dragInteraction:(UIDragInteraction *)interaction session:(id<UIDragSession>)session didEndWithOperation:(UIDropOperation)operation{NSLog(@"66666----");[UIView animateWithDuration:0.5 animations:^{self.img.alpha = 1;}]; }#pragma mark --UIDropInteractionDelegate //是否可以接收來自Drag數(shù)據(jù) - (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session{if(session.localDragSession == nil){//說明數(shù)據(jù)來自外界 }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的數(shù)據(jù) - (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session{if (session.localDragSession) {NSProgress *pregesss = [session loadObjectsOfClass:[UIImage class]completion:^(NSArray<__kindof id<NSItemProviderReading>> * _Nonnull objects) {// 回調(diào)的代碼塊默認(rèn)就在主線程for (id objc in objects) {UIImage *image = (UIImage *)objc;if (image) {self.img2.image = image;}}}];} }@end DragAndDrop.m文件?
3、有關(guān)方法簡(jiǎn)介
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; @endUIKIT_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; @endAPI_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDragInteractionDelegate <NSObject> @required //提供數(shù)據(jù)源 長(zhǎng)按UI開始拖拽 - (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session; @optional //提供UITargetedDragPreview的相關(guān)信息 長(zhǎng)按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;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 = 0,UIDropOperationForbidden = 1,UIDropOperationCopy = 2,UIDropOperationMove = 3, } 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; @endAPI_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ù)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; @end
?
轉(zhuǎn)載于:https://www.cnblogs.com/xianfeng-zhang/p/8951805.html
總結(jié)
以上是生活随笔為你收集整理的iOS开发Drag and Drop简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支付系统信息流和资金流
- 下一篇: 26.删除排序数组中的重复项