-
首先用下面四张图,说明我要实现的功能。【讲一张图片复制到另外一个UIimageView上】
实现代码如下:
01.
-(IBAction)panGestureTop:(UILongPressGestureRecognizer *)longPress
02.
{
03.
04.
UIMenuItem * itemPase = [[UIMenuItem alloc] initWithTitle:@
"复制"
action:
@selector
(copyimage)];
05.
UIMenuItem * itemTrans = [[UIMenuItem alloc] initWithTitle:@
"转发"
action:
@selector
(trans)];
06.
UIMenuItem * itemCollect = [[UIMenuItem alloc] initWithTitle:@
"收藏"
action:
@selector
(collect)];
07.
UIMenuItem * itemJoin = [[UIMenuItem alloc] initWithTitle:@
"加入"
action:
@selector
(join)];
08.
09.
UIMenuController * menuController = [UIMenuController sharedMenuController];
10.
[menuController setMenuItems: @[itemPase,itemCollect,itemTrans,itemJoin]];
11.
12.
CGPoint location = [longPress locationInView:[longPress view]];
13.
CGRect menuLocation = CGRectMake(location.x, location.y,
0
,
0
);
14.
[menuController setTargetRect:menuLocation inView:[longPress view]];
15.
menuController.arrowDirection = UIMenuControllerArrowDown;
16.
17.
[menuController setMenuVisible:YES animated:YES];
18.
19.
}
20.
-(BOOL)canBecomeFirstResponder
21.
{
22.
return
YES;
23.
}
24.
25.
-(
void
)copyimage
26.
{
27.
28.
UIPasteboard * pasterBoard = [UIPasteboard pasteboardWithName:@
"com.test.image"
create:YES];
29.
pasterBoard.image = [self.topImgView image];
30.
31.
}
32.
-(IBAction)panGestureBottom:(UILongPressGestureRecognizer *)longPress
33.
{
34.
UIMenuItem * itemPase = [[UIMenuItem alloc] initWithTitle:@
"粘贴"
action:
@selector
(pase)];
35.
36.
UIMenuController * menuController = [UIMenuController sharedMenuController];
37.
[menuController setMenuItems: @[itemPase]];
38.
39.
CGPoint location = [longPress locationInView:[longPress view]];
40.
CGRect menuLocation = CGRectMake(location.x, location.y,
0
,
0
);
41.
[menuController setTargetRect:menuLocation inView:[longPress view]];
42.
menuController.arrowDirection = UIMenuControllerArrowDown;
43.
44.
[menuController setMenuVisible:YES animated:YES];
45.
46.
}
47.
-(
void
)pase
48.
{
49.
UIPasteboard * pasterBoard = [UIPasteboard pasteboardWithName:@
"com.test.image"
create:NO];
50.
self.bottomImgView.image = pasterBoard.image;
51.
52.
}
当操作的时候,我们需要把当前的UIViewController变成第一响应者。复制相当于copy了一份,加入我在复制过图片之后,粘贴图片之前,把原来的那个image删除掉,则依然可以进行粘贴。