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

#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.

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值