效果圖
關鍵代碼
吐槽:今天在寫這個界面的使用用的是QBPopupMenu這個第三方庫,這個點贊量超過了1k,用著也是挺方便的,但是在tableViewCell上面使用的時候出現了一個bug,因為cell復用的原因,在網上找了好久,發現都是抄襲的一兩篇比較早的博文,經過仔細研究終于把bug解決了。
bug現象
1、彈出的菜單視圖沒有出現在我想要的地方
2、長按點擊時,有時候不出現菜單視圖
3、如果把[self.popupMenu showInView:self targetRect:self.bounds animated:YES];
這句代碼寫在自定制cell里面,沒有出現彈出視圖
注意鼠標
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(@"剪切");
}
}
效果圖
如果想要自定制上面的文字,表示不太清楚,知道的同學可以吱一聲。如果想要自定制可以使用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];
}