这段时间在集成使用融云聊天功能的时候,想自定义消息cell的长按的菜单,在网上查了查,这是根据 UIMenuController 实现的。具体代码如下,我是使用一个btn实现的
首先创建一个btn,并给btn添加一个长按的事件.
<pre>
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[btn addGestureRecognizer:recognizer];
</pre>
然后实现长按方法
注意,这个控件必须要实现几个方法
1、<pre>
[btn becomeFirstResponder];
</pre>
2、<pre>
- (BOOL)canBecomeFirstResponder{
return YES;
}
</pre>
3、让该view处于可交互状态
具体代码如下
<pre>
-
(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];
}
}
</pre>
下面再写出各个菜单点击的实现方法即可,非常简单。
<pre>
- (void)flag:(id)sender {
NSLog(@"Cell was flagged");
} - (void)approve:(id)sender {
NSLog(@"Cell was approved");
} - (void)deny:(id)sender {
NSLog(@"Cell was denied");
}
</pre>