UIMenuController 实现长按显示自定义菜单功能

这段时间在集成使用融云聊天功能的时候,想自定义消息cell的长按的菜单,在网上查了查,这是根据 UIMenuController 实现的。具体代码如下,我是使用一个btn实现的

首先创建一个btn,并给btn添加一个长按的事件

UIMenuController 实现长按显示自定义菜单功能

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

然后实现长按方法

注意,这个控件必须要实现几个方法

1、

[btn becomeFirstResponder];

2、

- (BOOL)canBecomeFirstResponder{

return YES;

}

3、让该view处于可交互状态

具体代码如下

UIMenuController 实现长按显示自定义菜单功能

 - (void)longPress:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
[btn becomeFirstResponder];
UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag"action:@selector(flag:)];
UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve"action:@selector(approve:)];
UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny"action:@selector(deny:)];
UIMenuController *menu = [UIMenuController sharedMenuController]; [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
[menu setTargetRect:btn.frame inView:btn.superview];
[menu setMenuVisible:YES animated:YES];
}
}

下面再写出各个菜单点击的实现方法即可,非常简单。

 - (void)flag:(id)sender {

     NSLog(@"Cell was flagged");

 }
- (void)approve:(id)sender { NSLog(@"Cell was approved");
} - (void)deny:(id)sender { NSLog(@"Cell was denied"); }
上一篇:SQL 查询所有表名、字段名、类型、长度、存储过程、视图


下一篇:MySql折腾小记二:text/blog类型不允许设置默认值,不允许存在两个CURRENT_TIMESTAMP