开发者

Customize UIMenuController

Hi I want to create a cus开发者_StackOverflow中文版tomize 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. Anyone know how to make a bubble menu similar like this. Any example and code for reference?


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.


Use the menuItems property on UIMenuController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜