首先用下面四张图,说明我要实现的功能。【讲一张图片复制到另外一个UIimageView上】
实现代码如下:
-(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
时间: 2024-10-11 04:11:27