需求 - 3 - 长按菜单栏

在手机的使用中我们经常会对一些文字、图像长按,想要响应出复制或者是别的东西,其中微信的功能算是最多的,有:复制、转发、收藏、撤回、翻译、更多..这么多功能。


我们今天来看看怎么实现的,并且会给出一个“复制”的简单功能。

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值