接下来我们做一个可复制的UIImageView,创建一个新的viewController,放两个imageView,默认显示不同的图:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{ return (action == @selector(copy:) || action == @selector(paste:)); } //标识可运行的方法 -(void)copy:(id)sender{ UIPasteboard *pboard = [UIPasteboard generalPasteboard]; pboard.image = self.image; } //拷贝的方法 -(void)paste:(id)sender{ UIPasteboard *pboard = [UIPasteboard generalPasteboard]; self.image = pboard.image; }//粘贴的方法
//自定义粘贴显示菜单
为了方便实用,我给cell添加了长按事件,看代码---
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[cell addGestureRecognizer:recognizer];
好,事件加上了,剩下的点击就是实现自定义剪贴板了,上代码:
- (void)longPress:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
CopyCell *cell = (CopyCell *)recognizer.view;
[cell becomeFirstResponder];
UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag"action:@selector(flag:)];
UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve"action:@selector(approve:)];
UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny"action:@selector(deny:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
NSLog(@".....%@",NSStringFromCGRect(cell.frame));
[menu setTargetRect:cell.frame inView:cell.superview];
[menu setMenuVisible:YES animated:YES];
}
}
通过上面这段代码,自定义剪贴板成功搞定,运行,你猛然会发现,操,怎么剪贴板没有出来,我猜你已经想到为什么了,因为- (BOOL)canBecomeFirstResponder{
return YES;
}
这么重要的一句话没有加,OK,下面就是你所要使用的方法了- (void)flag:(id)sender {
NSLog(@"Cell was flagged");
}
- (void)approve:(id)sender {
NSLog(@"Cell was approved");
}
- (void)deny:(id)sender {
NSLog(@"Cell was denied");
}