UIMenuController糾結之夜

weixin_34402408發表於2016-01-06

RedRain的簡書:http://www.jianshu.com/users/29e03e6ff407/latest_articles

在使用微信等應用的時候,你長按一個cell會出現複製blalala等一長串的小黑按鈕,那個這個東西就是UIMenuController, 上程式碼~~~


-(void)SystemNotifyLongPress:(UILongPressGestureRecognizer *)longPress{
    if (longPress.state == UIGestureRecognizerStateBegan) {

        UIWindow *window = [[UIApplication sharedApplication].delegate window];
        if ([window isKeyWindow] == NO)
        {
            [window becomeKeyWindow];
            [window makeKeyAndVisible];
        }
        
        [self becomeFirstResponder];
        
        UIMenuItem *collectID = [[UIMenuItem alloc] initWithTitle:@"收藏" action:@selector(CollectCellClick)];
        UIMenuItem *deleteID = [[UIMenuItem alloc] initWithTitle:@"刪除"  action:@selector(DeleteMenuClick)];
        
        UIMenuController *menu = [UIMenuController sharedMenuController];
        
        [menu setMenuItems:@[collectID, deleteID]];
        
        [menu setTargetRect:self.bounds inView:self];
        
        [menu setMenuVisible:YES animated:YES];
        
    }
}


我常按~~~~~~~~~~~~~~ 我ca, 我的按鈕哪裡去了!!!

列印它們Frame,isMenuVisible

一個是{{0,0}{0,0}}. 沒有尺寸呢!!!!!

一個是NO,怎麼不顯示呢!!~

好吧, 搜一下是怎麼回事~

原來要讓MenuController出來需要同時控制3個條件

  • 1.becomFirstResponder方法,使view或者viewController的self成為第一響應者,可以在相應檔案的任意地方呼叫實現該方法,不過建議與UIMenuController放在一起。
    [self becomeFirstResponder];

  • 2.設定-(BOOL) canBecomeFirstResponder的返回值為YES,原因不言而喻。
    - (BOOL) canBecomeFirstResponder{  
        return YES;  
    }
  • 3.過載函式-(BOOL) canPerfomAction:(SEL)actionwithSender:(id)sender,設定要顯示的選單項,返回值為YES。若不進行任何限制,則將顯示系統自帶的所有選單項(很多的,自己可以試一下),在此,只顯示自定義的2項,即

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender{

    // 這裡需要對每一個Item對應的Action進行判斷.
    if (action ==@selector(CollectCellClick) || action ==@selector(DeleteMenuClick)){

        return YES;

    }

    return NO;//隱藏系統預設的選單項

}

大功告成, 撒花!~

另:
上面的方法,[self becomeFirstResponder];時,在ios5/6沒問題,ios7卻crash:didChangeToFirstResponder

經過幾次查詢推測,最終解決:

1,首先,在UITableVeiw層

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   if (IOS7)
   {
       if (scrollView == myTable)
       {
          NSArray *_cellArray = [myTable visibleCells];
           if([_cellArray count] > 0)
           {
               [_cellArray makeObjectsPerformSelector:@selector(hiddenMenu)];
           }
       }
   }
}

2,在UITableVeiwCell內加hiddenMenu

-(void)hiddenMenu
{
    if (IOS7)
    {
        if ([self canBecomeFirstResponder])
        {
            if ([self isFirstResponder])
            {
                [self resignFirstResponder];
            }
        }
        UIMenuController *menu = [UIMenuController sharedMenuController];
        if ([menu isMenuVisible])  {
            [menu setMenuVisible:NO animated:YES];
        }
    }
}

3,並在UILongPressGestureRecognizer新增的地方加delegate

_recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

if (IOS7) {
    _recognizer.delegate = self;
}

[recognizerView addGestureRecognizer:_recognizer];
[_recognizer release];

相關文章