码迷,mamicode.com
首页 > 其他好文 > 详细

UIMenuController,UIPasteboard:复制,粘贴详解

时间:2014-07-02 15:18:54      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:复制 粘贴

首先用下面四张图,说明我要实现的功能。【讲一张图片复制到另外一个UIimageView上】

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣


实现代码如下:

-(IBAction)panGestureTop:(UILongPressGestureRecognizer *)longPress
{

     UIMenuItem * itemPase = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyimage)];
     UIMenuItem * itemTrans = [[UIMenuItem alloc] initWithTitle:@"转发" action:@selector(trans)];
     UIMenuItem * itemCollect = [[UIMenuItem alloc] initWithTitle:@"收藏" action:@selector(collect)];
     UIMenuItem * itemJoin = [[UIMenuItem alloc] initWithTitle:@"加入" action:@selector(join)];
    
    UIMenuController * menuController = [UIMenuController sharedMenuController];
    [menuController setMenuItems: @[itemPase,itemCollect,itemTrans,itemJoin]];
    
    CGPoint location = [longPress locationInView:[longPress view]];
    CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
    [menuController setTargetRect:menuLocation inView:[longPress view]];
    menuController.arrowDirection = UIMenuControllerArrowDown;

    [menuController setMenuVisible:YES animated:YES];

}
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)copyimage
{
    
    UIPasteboard * pasterBoard = [UIPasteboard pasteboardWithName:@"com.test.image" create:YES];
    pasterBoard.image = [self.topImgView image];
    
}
-(IBAction)panGestureBottom:(UILongPressGestureRecognizer *)longPress
{
    UIMenuItem * itemPase = [[UIMenuItem alloc] initWithTitle:@"粘贴" action:@selector(pase)];
    
    UIMenuController * menuController = [UIMenuController sharedMenuController];
    [menuController setMenuItems: @[itemPase]];
    
    CGPoint location = [longPress locationInView:[longPress view]];
    CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
    [menuController setTargetRect:menuLocation inView:[longPress view]];
    menuController.arrowDirection = UIMenuControllerArrowDown;
    
    [menuController setMenuVisible:YES animated:YES];

}
-(void)pase
{
    UIPasteboard * pasterBoard = [UIPasteboard pasteboardWithName:@"com.test.image" create:NO];
    self.bottomImgView.image = pasterBoard.image;

}

当操作的时候,我们需要把当前的UIViewController变成第一响应者。


复制相当于copy了一份,加入我在复制过图片之后,粘贴图片之前,把原来的那个image删除掉,则依然可以进行粘贴。



UIMenuController,UIPasteboard:复制,粘贴详解,布布扣,bubuko.com

UIMenuController,UIPasteboard:复制,粘贴详解

标签:复制 粘贴

原文地址:http://blog.csdn.net/jijunyuan/article/details/36392605

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!