复制 粘贴 转发功能的实现


 

额 为了实现这个效果。。。怎么说呢 还好Xcode有自带的 不然真心费劲( ⊙o⊙ )哇 

最后发现实现这个需要配合长按手势 和 UImenuController 

  UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
    longPressGr.minimumPressDuration = 0.5;
    [self.tableView addGestureRecognizer:longPressGr];

这是长按手势 然后再长按手势里添加UIMenuController 

#pragma mark - 长按弹出菜单
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture{
    
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        CGPoint point = [gesture locationInView:self.tableView];
        _touchPoint =point;

//这里要获取到你点击时候的位置         
        [self addMenuController];
    }
   }


#pragma mark 菜单选项
-(void)addMenuController{
    UIMenuController *menuController =[UIMenuController sharedMenuController];
    UIMenuItem *menuitem1 =[[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(doCopy:)];

//@selset里面的方法是你点击弹出menucontroller其中一个所要执行的方法

    UIMenuItem *menuitem2 =[[UIMenuItem alloc]initWithTitle:@"保存" action:@selector(doSavePhoto:)];
    UIMenuItem *menuitem3 =[[UIMenuItem alloc]initWithTitle:@"转发" action:@selector(doForward:)];
     [menuController setTargetRect:CGRectMake(_touchPoint.x, _touchPoint.y, 0, 50) inView:self.tableView];

    [menuController setMenuVisible:YES animated:YES];

}

//复制
-(void)doCopy:(id)sender{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];

//这其实就是一个剪切板  UIPasteboard

   pboard.string = self.messageVo.body;
    [MBProgressHUD showSuccess:@"复制成功"];
    
}

//转发
-(void)doForward:(id)sender{
    DCSAddMessageViewController *add = [[DCSAddMessageViewController alloc]init];
    add.checkIsForward = YES;
    add.inMessageVo = self.messageVo;
    [self.navigationController pushViewController:add animated:YES];
}

//保存图片
-(void)doSavePhoto:(id)sender{
    NSURL *url = [NSURL URLWithString:self.messageVo.attachmentVo.attachUrl];
    UIImageView *viw = [[UIImageView alloc]init];
    [viw sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if(!error) {
             UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
        }
    }];
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值