美文网首页iOS知识专题
iOS开发长按tabbaleVew

iOS开发长按tabbaleVew

作者: wwwwwwww1 | 来源:发表于2016-06-27 15:53 被阅读44次

实现步骤:
1.给cell添加UILongPressGestureRecognizer和相应处理事件

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    ..............
    UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:selfaction:@selector(cellLongPress:)];
    [cell addGestureRecognizer:longPressGesture];
    return cell;
    }
    2.配置和显示UIMenuController

  • (void)cellLongPress:(UIGestureRecognizer *)recognizer{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
    CGPoint location = [recognizer locationInView:self];
    NSIndexPath * indexPath = [self indexPathForRowAtPoint:location];
    UIMyTableViewCell *cell = (UIMyTableViewCell *)recognizer.view;
         //这里把cell做为第一响应(cell默认是无法成为responder,需要重写canBecomeFirstResponder方法)
    [cell becomeFirstResponder];

      UIMenuItem *itCopy = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(handleCopyCell:)];
      UIMenuItem *itDelete = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(handleDeleteCell:)];        
      UIMenuController *menu = [UIMenuController sharedMenuController];
    

[menu setMenuItems:[NSArray arrayWithObjects:itCopy, itDelete, nil]];
[menu setTargetRect:cell.frame inView:self];
[menu setMenuVisible:YES animated:YES];

    [itCopy release];
    [itDelete release];
}

}

  • (void)handleCopyCell:(id)sender{//复制cell
    NSLog(@"handle copy cell");
    }

  • (void)handleDeleteCell:(id)sender{//删除cell
    NSLog(@"handle delete cell");
    }

3.在自定义的cell里重写canBecomeFirstResponder方法,返回yes
//为了让菜单显示,目标视图必须在responder链中,很多UIKit视图默认并无法成为一个responder,因此你需要使这些视图重载 canBecomeFirstResponder方法,并返回YES

  • (BOOL)canBecomeFirstResponder{
    return YES;
    }

经过这几步,就可以成功显示了,又在网上看到一篇讲这个的外文,分享一下:
http://www.intridea.com/blog/2010/12/22/developers-notes-for-uimenucontroller

相关文章

  • iOS开发长按tabbaleVew

    实现步骤:1.给cell添加UILongPressGestureRecognizer和相应处理事件 (UITabl...

  • Lable+复制

    在iOS开发中,我们可能有需求需要长按某个控件来复制内容。 iOS8.0之后,lable不再提供长按复制的功能了?...

  • iOS 手势

    本文介绍iOS开发中手势的运用,话不多说,先上代码;给tableView添加长按手势代码如下: 给cell添加长按...

  • Flutter中webview禁止iOS长按手势

    我们在开发过程中会有这样的需求-长按webview中的图片识别图片中的二维码或是保存图片,此时IOS需要禁止长按手...

  • 移动端资源小结

    以下内容收集于网络资源! 禁止ios 长按时不触发系统的菜单,禁止ios&android长按时下载图片 禁止ios...

  • 按钮长按事件

    iOS有长按手势UILongPressGestureRecognizer,这个手势需要指定长按的事件,指定时间之后...

  • iOS实战之特效

    特效一:给View加阴影 特效二:cell长按阴影 1)、去除长按效果 2)、更换长按效果以及颜色: 参考:iOS...

  • Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)

    ==================================ios webView 同时添加单击和长按手势...

  • iOS 长按移动UITableViewCell

    之前写了一篇有关于UICollectionViewCell的长按移动的文章:iOS 长按移动UICollectio...

  • iOS开发之一步英文改中文

    在iOS开发中,例如像相册选取,默认的长按复制操作等,显示的都是英文 打开工程文件的plist文件,找到Local...

网友评论

    本文标题:iOS开发长按tabbaleVew

    本文链接:https://www.haomeiwen.com/subject/ueutdttx.html