- (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];
}
//初始化方法 - (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;
/* 这个方法是必须实现的用来返回拖拽源提供的数据 需要注意,这个函数需要返回一个数组,数组中可以有多个数据源 如果返回空数组,则拖拽行为不会开始 */ - (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;
//添加视图
- (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;
}];
}
//初始化方法 - (instancetype)initWithDelegate:(id<UIDropInteractionDelegate>)delegate; //代理对象 @property (nonatomic, nullable, readonly, weak) id<UIDropInteractionDelegate> delegate; //是否允许多个交互行为 @property (nonatomic, assign) BOOL allowsSimultaneousDropSessions;
//放置行为即将响应时触发的方法 返回值确定是否响应此次行为 - (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;
//初始化方法
/*
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;
//初始化方法 - (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);
//继承于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
API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos) @protocol UIDragSession <UIDragDropSession> //设置要传递的额外信息 只有在同个APP内可见 @property (nonatomic, strong, nullable) id localContext; @end
//传递的数据数组 @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;
//创建一个预览对象 /* 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;
/* 初始化方法 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;
//构造方法并设置路径矩形 - (instancetype)initWithTextLineRects:(NSArray<NSValue /* CGRect */ *> *)textLineRects; //显示的路径 @property (nonatomic, copy, nullable) UIBezierPath *visiblePath; //背景色 @property (nonatomic, copy, null_resettable) UIColor *backgroundColor;
#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) {
self.name = [coder decodeObjectForKey:@"name"];
self.age = [coder decodeIntegerForKey:@"age"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name 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;
}];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有