效果图
长按tableViewCell弹出菜单.png关键代码
吐槽:今天在写这个界面的使用用的是QBPopupMenu这个第三方库,这个点赞量超过了1k,用着也是挺方便的,但是在tableViewCell上面使用的时候出现了一个bug,因为cell复用的原因,在网上找了好久,发现都是抄袭的一两篇比较早的博文,经过仔细研究终于把bug解决了。
bug现象
1、弹出的菜单视图没有出现在我想要的地方
2、长按点击时,有时候不出现菜单视图
3、如果把[self.popupMenu showInView:self targetRect:self.bounds animated:YES];
这句代码写在自定制cell里面,没有出现弹出视图
注意鼠标
bug.gif
bug解决代码
[self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];
tableViewCell出现粘贴板的方式
-
1、 tableViewCell的代理事件
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
-
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
-
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
-
2、使用
UIMenuController
-
3、使用
QBPopupMenu
1、 tableViewCell的代理事件
// 允许长按菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// 允许每一个Action
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
{
// 可以支持所有Action,也可以只支持其中一种或者两种Action
if (action == @selector(copy:) || action == @selector(paste:)) { // 支持复制和黏贴
return YES;
}
return NO;
}
// 对一个给定的行告诉代表执行复制或黏贴操作内容
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
{
if (action == @selector(copy:)) {
NSLog(@"复制");
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; // 黏贴板
[pasteBoard setString:cell.textLabel.text];
} else if (action == @selector(paste:)) {
NSLog(@"黏贴");
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSLog(@"%@",pasteBoard.string);
} else if (action == @selector(cut:)) {
NSLog(@"剪切");
}
}
效果图
屏幕快照 2017-05-24 下午6.45.40.png如果想要自定制上面的文字,表示不太清楚,知道的同学可以吱一声。如果想要自定制可以使用UIMenuController
2、使用UIMenuController
UIMenuController,系统默认支持UITextField、UITextView、UIWebView控件的UIMenuController相关操作。当我们长按一段文字或者图片的时候会弹出一个菜单,我们通过这个菜单可以实现文字等的复制、剪切、删除以及各种操作。
UIMenuController相关方法
创建一个UIMenuController对象
+ (UIMenuController *)sharedMenuController
显示或者隐藏菜单,注意 在显示menu之前,一点要确定为menu设置与其相关的显示位置
@property(nonatomic,getter=isMenuVisible) BOOL menuVisible; // default is NO
//是否通过动画进行设置显示、隐藏
- (void)setMenuVisible:(BOOL)menuVisible animated:(BOOL)animated;
设置menu显示的位置
/**
* 设置menu显示的位置信息
*
* @param targetRect menu需要显示的矩形区域
* @param targetView targetRect会以targetView的左上角为坐标原点进行显示
*/
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView;
注意
targetRect一旦设定以后,矩形范围不会跟随view的移动而移动,如果view移动,必须相应的更新targetRect 。比如tableView 点击cell出现menu,当按住对应的cell,拖动tableView滚动时,menu不会随着对应的cell一起滚动---见示例代码2
targetRect通常设置为需要弹出menu控件的bounds,targetView设置为对应的控件本身
自定义menuItem
@property(nonatomic, copy) NSArray <UIMenuItem *> *menuItems
@interface UIMenuItem : NSObject
//创建UIMenuItem对象
- (instancetype)initWithTitle:(NSString *)title action:(SEL)action ;
@property(nonatomic,copy) NSString *title;
@property(nonatomic) SEL action;
数据类型:编辑菜单箭头指向view的位置 。默认取决于view在界面的位置
typedef enum {
UIMenuControllerArrowDefault,
UIMenuControllerArrowUp,
UIMenuControllerArrowDown,
UIMenuControllerArrowLeft,
UIMenuControllerArrowRight,
} UIMenuControllerArrowDirection;
自定义控件的UIMenuController
一般步骤:
- 设置控件成为第一响应者
- 创建UIMenuControler
- 创建UIMenuItem(如果需要自定义item)
在自定制cell里面
- (void)addGesture{
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressCellHandle:)];
[self addGestureRecognizer:longPressGesture];
}
-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture
{
[self becomeFirstResponder];
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *checkItem = [[UIMenuItem alloc] initWithTitle:@"审批" action:@selector(menuBtnPressed:)];
_checkItem = checkItem;
UIMenuItem *rejectItem = [[UIMenuItem alloc] initWithTitle:@"驳回" action:@selector(menuBtnPressed:)];
_rejectItem = rejectItem;
UIMenuItem *postponeItem = [[UIMenuItem alloc] initWithTitle:@"延期" action:@selector(menuBtnPressed:)];
_postponeItem = postponeItem;
UIMenuItem *moreItem = [[UIMenuItem alloc] initWithTitle:@"更多..." action:@selector(menuBtnPressed:)];
_moreItem = moreItem;
menuController.menuItems = @[checkItem,rejectItem,postponeItem,moreItem];
[menuController setTargetRect:gesture.view.frame inView:gesture.view.superview];
[menuController setMenuVisible:YES animated:YES];
[UIMenuController sharedMenuController].menuItems=nil;
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(menuBtnPressed:)) {
return YES;
}
return NO;
}
#pragma mark ---- 菜单按钮执行事件 ----
-(void)menuBtnPressed:(UIMenuItem *)menuItem
{
}
防止拖动tableView时产生的BUG
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
UIMenuController * menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
}
想要了解更加详细的内容,可以参考UIMenuController的使用简介
3、使用QBPopupMenu
- (void)addGesture{
[self becomeFirstResponder];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressCellHandle:)];
[self addGestureRecognizer:longPressGesture];
QBPopupMenuItem *item = [QBPopupMenuItem itemWithTitle:@"Hello" target:self action:@selector(action)];
QBPopupMenuItem *item2 = [QBPopupMenuItem itemWithTitle:@"Cut" target:self action:@selector(action)];
QBPopupMenuItem *item3 = [QBPopupMenuItem itemWithTitle:@"Copy" target:self action:@selector(action)];
QBPopupMenuItem *item4 = [QBPopupMenuItem itemWithTitle:@"Delete" target:self action:@selector(action)];
QBPopupMenuItem *item5 = [QBPopupMenuItem itemWithImage:[UIImage imageNamed:@"clip"] target:self action:@selector(action)];
QBPopupMenuItem *item6 = [QBPopupMenuItem itemWithTitle:@"Delete" image:[UIImage imageNamed:@"trash"] target:self action:@selector(action)];
NSArray *items = @[item, item2, item3, item4, item5, item6];
QBPopupMenu *popupMenu = [[QBPopupMenu alloc] initWithItems:items];
popupMenu.highlightedColor = [[UIColor colorWithRed:0 green:0.478 blue:1.0 alpha:1.0] colorWithAlphaComponent:0.8];
self.popupMenu = popupMenu;
}
-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture
{
[self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];
}
当时我写的时候主要是这句代码没有理解好
-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture
{
[self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];
}
网友评论