在手机的使用中我们经常会对一些文字、图像长按,想要响应出复制或者是别的东西,其中微信的功能算是最多的,有:复制、转发、收藏、撤回、翻译、更多..这么多功能。
我们今天来看看怎么实现的,并且会给出一个“复制”的简单功能。
1.在长按方法下:
- (void)longTap:(UILongPressGestureRecognizer *)longTap
{
//成为第一响应者
[self becomeFirstResponder];
//设置菜单栏对象
UIMenuController *menu=[UIMenuController sharedMenuController];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
//iOS提供了相应的通知处理
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuShow:)
name:UIMenuControllerWillShowMenuNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuHide:) name:UIMenuControllerWillHideMenuNotification object:nil];
}
2.使之能够成为第一响应者:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
3.响应相应的方法 - copy:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(copy:))
{
return YES;
}
return [super canPerformAction:action withSender:sender];
}
4.拷贝操作:
- (void)copy:(id)sender
{
[[UIPasteboard generalPasteboard]setString:self.contentStr];
}
参考:
Reference 1 : http://blog.csdn.net/piziliweiguang/article/details/8281007
Reference 2 : http://blog.csdn.net/tangaowen/article/details/6526019