長按tableViewCell彈出菜單欄粘貼板

效果圖
長按tableViewCell彈出菜單.png
關(guān)鍵代碼

吐槽:今天在寫這個(gè)界面的使用用的是QBPopupMenu這個(gè)第三方庫,這個(gè)點(diǎn)贊量超過了1k,用著也是挺方便的,但是在tableViewCell上面使用的時(shí)候出現(xiàn)了一個(gè)bug,因?yàn)閏ell復(fù)用的原因,在網(wǎng)上找了好久,發(fā)現(xiàn)都是抄襲的一兩篇比較早的博文,經(jīng)過仔細(xì)研究終于把bug解決了。

bug現(xiàn)象

1、彈出的菜單視圖沒有出現(xiàn)在我想要的地方
2、長按點(diǎn)擊時(shí),有時(shí)候不出現(xiàn)菜單視圖
3、如果把[self.popupMenu showInView:self targetRect:self.bounds animated:YES];這句代碼寫在自定制cell里面,沒有出現(xiàn)彈出視圖

注意鼠標(biāo)


bug.gif
bug解決代碼
[self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];

tableViewCell出現(xiàn)粘貼板的方式

  • 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;
}
// 允許每一個(gè)Action
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
{
    // 可以支持所有Action,也可以只支持其中一種或者兩種Action
    if (action == @selector(copy:) || action == @selector(paste:)) { // 支持復(fù)制和黏貼
        return YES;
    }
    return NO;
}
// 對一個(gè)給定的行告訴代表執(zhí)行復(fù)制或黏貼操作內(nèi)容
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
{
    if (action == @selector(copy:)) { 
        NSLog(@"復(fù)制");
        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

如果想要自定制上面的文字,表示不太清楚,知道的同學(xué)可以吱一聲。如果想要自定制可以使用UIMenuController

2、使用UIMenuController

UIMenuController,系統(tǒng)默認(rèn)支持UITextField、UITextView、UIWebView控件的UIMenuController相關(guān)操作。當(dāng)我們長按一段文字或者圖片的時(shí)候會彈出一個(gè)菜單,我們通過這個(gè)菜單可以實(shí)現(xiàn)文字等的復(fù)制、剪切、刪除以及各種操作。

UIMenuController相關(guān)方法

創(chuàng)建一個(gè)UIMenuController對象

+ (UIMenuController *)sharedMenuController

顯示或者隱藏菜單,注意 在顯示menu之前,一點(diǎn)要確定為menu設(shè)置與其相關(guān)的顯示位置

@property(nonatomic,getter=isMenuVisible) BOOL menuVisible;        // default is NO
//是否通過動畫進(jìn)行設(shè)置顯示、隱藏
- (void)setMenuVisible:(BOOL)menuVisible animated:(BOOL)animated;

設(shè)置menu顯示的位置

/**
 *  設(shè)置menu顯示的位置信息
 *
 *  @param targetRect menu需要顯示的矩形區(qū)域
 *  @param targetView targetRect會以targetView的左上角為坐標(biāo)原點(diǎn)進(jìn)行顯示
 */
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView;
注意

targetRect一旦設(shè)定以后,矩形范圍不會跟隨view的移動而移動,如果view移動,必須相應(yīng)的更新targetRect 。比如tableView 點(diǎn)擊cell出現(xiàn)menu,當(dāng)按住對應(yīng)的cell,拖動tableView滾動時(shí),menu不會隨著對應(yīng)的cell一起滾動---見示例代碼2
targetRect通常設(shè)置為需要彈出menu控件的bounds,targetView設(shè)置為對應(yīng)的控件本身

自定義menuItem

@property(nonatomic, copy) NSArray <UIMenuItem *> *menuItems

@interface UIMenuItem : NSObject 
//創(chuàng)建UIMenuItem對象
- (instancetype)initWithTitle:(NSString *)title action:(SEL)action ;
@property(nonatomic,copy) NSString *title;
@property(nonatomic)      SEL       action;

數(shù)據(jù)類型:編輯菜單箭頭指向view的位置 。默認(rèn)取決于view在界面的位置

typedef enum {
 UIMenuControllerArrowDefault,
 UIMenuControllerArrowUp,
 UIMenuControllerArrowDown,
 UIMenuControllerArrowLeft,
 UIMenuControllerArrowRight,
} UIMenuControllerArrowDirection;
自定義控件的UIMenuController

一般步驟:

  • 設(shè)置控件成為第一響應(yīng)者
  • 創(chuàng)建UIMenuControler
  • 創(chuàng)建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 ---- 菜單按鈕執(zhí)行事件 ----
-(void)menuBtnPressed:(UIMenuItem *)menuItem

{
    
}
防止拖動tableView時(shí)產(chǎn)生的BUG

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIMenuController * menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:NO animated:YES];
}

想要了解更加詳細(xì)的內(nèi)容,可以參考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];
}

當(dāng)時(shí)我寫的時(shí)候主要是這句代碼沒有理解好

-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture

{
   [self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];
}
簡單的總結(jié)一下,希望能過幫助跟我遇到一樣問題的小朋友
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容