#import "ViewController.h"
@interface ViewController ()<UIDragInteractionDelegate,UIDropInteractionDelegate>{
UIImageView *img1;
UIImageView *img2;
UILabel *label;
}
@property (nonatomic,strong) UIDragInteraction *dragInteraction;
@property (nonatomic,strong) UIDropInteraction *dropInteraction;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 把拖拽行为加在拖拽源上,放置行为加在放置目标上,然后分别实现拖拽行为代理(UIDragInteractionDelegate)和放置行为代理方法(UIDropInteractionDelegate),就能实现拖拽效果了
//其他的代理方法没有写
img1 = [[UIImageViewalloc]initWithFrame:CGRectMake(10,100, 100, 100)];
img1.image = [UIImageimageNamed:@"123"];
[self.viewaddSubview:img1];
img1.userInteractionEnabled =YES;
label = [[UILabelalloc]initWithFrame:CGRectMake(10,220, 200, 30)];
label.backgroundColor = [UIColoryellowColor];
[self.viewaddSubview:label];
img2 = [[UIImageViewalloc]initWithFrame:CGRectMake(10,250, 100, 100)];
img2.image = [UIImageimageNamed:@"234"];
[self.viewaddSubview:img2];
img2.userInteractionEnabled =YES;
[img1 addInteraction:self.dragInteraction];
[img2 addInteraction:self.dropInteraction];
}
/**
* 必须实现的代理方法:
*/
- (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session {
NSItemProvider *provider = [[NSItemProvideralloc]initWithObject:@"拖拽了一个字符串过来"];
UIDragItem *dragItemString = [[UIDragItemalloc]initWithItemProvider:provider];
NSItemProvider *provider1 = [[NSItemProvideralloc]initWithObject:img1.image];
UIDragItem *dragItemImg = [[UIDragItemalloc]initWithItemProvider:provider1];
return @[dragItemString,dragItemImg];
}
- (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session {
//如果返回nil,说明这个操作来自另外一个APP
UIDropOperation dropOperation = session.localDragSession ?UIDropOperationMove:UIDropOperationCopy;
UIDropProposal *dropProposal = [[UIDropProposalalloc]initWithDropOperation:dropOperation];
return dropProposal;
}
//当用户进行放置后会调用,可以从session中获取被传递的数据
- (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session {
//这个方法内要判断是否源自本APP
if (session.localDragSession) {
if ([session canLoadObjectsOfClass:[NSString class]]) {
[session loadObjectsOfClass:[NSStringclass] completion:^(NSArray<__kindofid<NSItemProviderReading>> *_Nonnull objects) {
label.text = objects.firstObject;
}];
}
if ([session canLoadObjectsOfClass:[UIImage class]]) {
[session loadObjectsOfClass:[UIImageclass] completion:^(NSArray<__kindofid<NSItemProviderReading>> *_Nonnull objects) {
img2.image = objects.lastObject;
}];
}
}
}
- (UIDragInteraction *)dragInteraction {
if (!_dragInteraction) {
_dragInteraction = [[UIDragInteractionalloc]initWithDelegate:self];
_dragInteraction.allowsSimultaneousRecognitionDuringLift =YES;
_dragInteraction.enabled =YES;
}
return_dragInteraction;
}
- (UIDropInteraction *)dropInteraction {
if (!_dropInteraction) {
_dropInteraction = [[UIDropInteractionalloc]initWithDelegate:self];
_dropInteraction.allowsSimultaneousDropSessions =YES;
}
return_dropInteraction;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}