iOS-UIMenuController的基本使用

2015-12-17  本文已影响1034人  巨人永不倒

前言

UIMenuController基本使用

-(BOOL)canBecomeFirstResponder{
    return YES;
}
// 添加长按手势
 UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPress:)];
 [cell addGestureRecognizer:longPress];
-(void)cellLongPress:(UIGestureRecognizer *)recognizer{ 
if (recognizer.state==UIGestureRecognizerStateBegan )   
MessageCell * messageCell=(MessageCell *)recognizer.view;  
[messageCell becomeFirstResponder];
}
UIMenuItem * copyItem=[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(myCopy:)];
UIMenuItem * deleateItem=[[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(myDeleate:)];
//   获取UIMenuController单例
UIMenuController * menuControl=[UIMenuController sharedMenuController];
//   塞进UIMenuController中
[menuControl setMenuItems:[NSArray arrayWithObjects:copyItem,deleateItem ,nil]];
//   设置要显示的位置
[menuControl setTargetRect:CGRectMake(messageCell.contentBtn.frame.origin.x, messageCell.contentBtn.frame.origin.y, messageCell.contentBtn.frame.size.width, messageCell.contentBtn.frame.size.height) inView:messageCell];
//   显示出来
 [menuControl setMenuVisible:YES animated:YES];
-(void)myCopy:(id)sender
{
//获取粘贴板单例并把Cell中的文本值赋入
    [[UIPasteboard generalPasteboard] setString:self.currentSelectMessgaeCell.contentBtn.currentTitle];
}
-(void)myDeleate:(id)sender
{
//    删除环信某一条聊天(项目中用到环信聊天)
    NSString * messageID=self.currentSelectMessgaeCell.messageFrame.message.messageId;
    if ([self.conversation removeMessageWithId:messageID]) {
//    根据Cell定位Cell的位置
        NSIndexPath * path = [self.tableView indexPathForCell:self.currentSelectMessgaeCell];
//    根据位置删除内存数据
        [self.tableDataSource removeObjectAtIndex:path.row];
//    再删除对应位置的Cell
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationRight];
    }
}

UIMenuController的使用并不难,苹果已经封装好了一些原子化操作,只需要我们拼装起来就行了

上一篇 下一篇

猜你喜欢

热点阅读