Customize UIMenuController

前端 未结 2 875
猫巷女王i
猫巷女王i 2020-12-04 16:21

Hi I want to create a customize bubble menu, like cut/copy/paste menu, in IPhone SDK3.x. I know it is UIMenuController but it is only provide standard cut/copy/past menu.

相关标签:
2条回答
  • 2020-12-04 16:35

    1) you need to add custom menu items to the shared UIMenuController:

    UIMenuItem* miCustom1 = [[[UIMenuItem alloc] initWithTitle: @"Custom 1" action:@selector( onCustom1: )] autorelease];
    UIMenuItem* miCustom2 = [[[UIMenuItem alloc] initWithTitle: @"Custom 2" action:@selector( onCustom2: )] autorelease];
    UIMenuController* mc = [UIMenuController sharedMenuController];
    mc.menuItems = [NSArray arrayWithObjects: miCustom1, miCustom2, nil];
    

    2) you need to implement your handler methods somewhere in the responder chain for the view that will be first-responder when you show the menu:

    - (void) onCustom1: (UIMenuController*) sender
    {
    }
    
    - (void) onCustom2: (UIMenuController*) sender
    {
    }
    

    3) you optionally need to implement canPerformAction: in the responder chain for the view that will be first-responder when you show the menu:

    - (BOOL) canPerformAction:(SEL)action withSender:(id)sender
    {
        if ( action == @selector( onCustom1: ) )
        {
                return YES; // logic here for context menu show/hide
        }
    
        if ( action == @selector( onCustom2: ) )
        {
                return NO;  // logic here for context menu show/hide
        }
    
        if ( action == @selector( copy: ) )
        {
                 // turn off copy: if you like:
            return NO;
        }
    
        return [super canPerformAction: action withSender: sender];
    }
    

    4) if the view you want to present the menu for doesn't already support showing a menu, (i.e. a UIWebView will show a menu when the user does a long-tap, but a UILabel has no built in support for showing a menu), then you need to present the menu yourself. This is often done by attaching a UILongPressGestureRecognizer to the view, then showing the menu in the callback:

    UILongPressGestureRecognizer* gr = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector( onShowMenu: ) ] autorelease];
       [_myview addGestureRecognizer: gr];
    
    
    
    - (void) onShowMenu: (UIGestureRecognizer*) sender
    {
        [sender.view becomeFirstResponder];
    
        UIMenuController* mc = [UIMenuController sharedMenuController];
    
        CGRect bounds = sender.view.bounds;
    
        [mc setTargetRect: sender.view.frame inView: sender.view.superview];
        [mc setMenuVisible: YES animated: YES];
    }
    

    Note, there has to be a view that claims firstResponder for the menu to show.

    5) make sure the view you're showing the menu for returns YES/TRUE to canBecomeFirstResponder. For example, if you try to make a UILabel a first responder it will return NO, so you would have to subclass it.

    6) that's about it. You may want to resignFirstResponder when the action callback is called - but to do this you'll need to implement logic to discover the firstResponder.

    0 讨论(0)
  • 2020-12-04 16:44

    Use the menuItems property on UIMenuController.

    0 讨论(0)
提交回复
热议问题