UIMenuController的使用

#pragma mark - UIView添加

- (void)showPopupView:(UIButton*)button {

button.selected= !button.selected;

//必须要有,如果要UIMenuController显示

[self becomeFirstResponder];

_menuController= [UIMenuControllersharedMenuController];

UIMenuItem*menuitem_01 = [[UIMenuItemalloc]initWithTitle:@"delete"action:@selector(deleteAction)];

UIMenuItem*menuitem_02 = [[UIMenuItemalloc]initWithTitle:@"copy"action:@selector(copyAction)];

UIMenuItem*menuitem_03 = [[UIMenuItemalloc]initWithTitle:@"cancel"action:@selector(cancelAction)];

_menuController.menuItems= [NSArrayarrayWithObjects:menuitem_01, menuitem_02, menuitem_03,nil];

[_menuControllersetTargetRect:CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width,30)inView:self.view];

[_menuControllersetMenuVisible:YESanimated:YES];

}

//必须要有,如果要UIMenuController显示

-(BOOL)canBecomeFirstResponder {

return YES;

}

//监听自己的定义事件,是return YES;否return NO即移除系统;

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

if(action ==@selector(deleteAction) || action ==@selector(copyAction) || action ==@selector(cancelAction)) {

returnYES;

}

returnNO;

}

#pragma mark -UITableViewCell添加

一、使用UITableViewDelegate方法无需子类化

前提:我们假设一个控制器中有一个tableView,并完成了tableView的基本设置。

0.在viewDidLoad中:给menu添加一个举报的Item

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(report:)];

UIMenuController *menu=[UIMenuController sharedMenuController];

[menu setMenuItems: @[testMenuItem]];

[menu update];//必须要更新

1.是否显示Menu

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {

return YES;

}

2.是否该显示系统菜单选项并调(就是到底显示系统的哪几项)

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

//显示一个系统的,一个自定义的

return (action == @selector(copy:) || action == @selector(report:));

}

3.根据action来判断具体要什么执行动作

-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

if (action == @selector(copy:)) {

NSLog(@"copy...");

}

else if(action == @selector(report:)){

NSLog(@"report...");

}

}

二、使用长按手势加子类化(大多数用到cell都会把它子类化的)

0.在cell子类化初始化方法中或者在数据源方法返回cell的时候都可以加上手势

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

[cell addGestureRecognizer:longPress];

1.实现长按方法

-(void)longPress:(UILongPressGestureRecognizer *)recognizer

{

if (recognizer.state == UIGestureRecognizerStateBegan) {

YDSonCommentCell *cell = (YDSonCommentCell *)recognizer.view;

[cell becomeFirstResponder];

UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报"action:@selector(report:)];

UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"拷贝"action:@selector(copy:)];

UIMenuController *menu = [UIMenuController sharedMenuController];

[menu setMenuItems:[NSArray arrayWithObjects:report,copy, nil]];

[menu setTargetRect:cell.frame inView:cell.superview];

[menu setMenuVisible:YES animated:YES];

}

}

2.cell子类化实现两个方法

//只有成为第一响应者时menu才会弹出

-(BOOL)canBecomeFirstResponder

{

return YES;

}

//是否可以接收某些菜单的某些交互操作

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{

return (action == @selector(copy:) || action == @selector(report:));

}

//实现这两个方法来执行具体操作

-(void)copy:(id)sender{

[UIPasteboard generalPasteboard].string=self.comment.Content;

}

-(void)report:(id)sender{

NSLog(@"举报啦");

}

你可能感兴趣的:(UIMenuController的使用)