自定义UIMenuController [英] Customize UIMenuController

查看:170
本文介绍了自定义UIMenuController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone SDK3.x中创建自定义气泡菜单,例如剪切/复制/粘贴菜单.我知道它是UIMenuController,但它仅提供标准的剪切/复制/粘贴菜单.任何人都知道如何制作类似这样的气泡菜单.有示例和代码可供参考吗?

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. Anyone know how to make a bubble menu similar like this. Any example and code for reference?

推荐答案

1),您需要将自定义菜单项添加到共享的UIMenuController:

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)您需要在响应程序链中某个位置实现处理程序方法,以便在显示菜单时首先响应的视图:

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)您可以选择在响应者链中实现canPerformAction:,以便在显示菜单时首先响应的视图:

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)如果您要为其显示菜单的视图尚不支持显示菜单,(例如,当用户长按时,UIWebView将显示菜单,但是UILabel不支持该菜单显示菜单),那么您需要亲自展示菜单.这通常是通过将UILongPressGestureRecognizer附加到视图,然后在回调中显示菜单来完成的:

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];
}

请注意,必须有一个声明为firstResponder的视图才能显示菜单.

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

5)确保显示菜单的视图向canBecomeFirstResponder返回YES/TRUE.例如,如果您尝试将UILabel设置为第一响应者,它将返回NO,因此您必须将其子类化.

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)就是这样.调用动作回调时,您可能想要resignFirstResponder-但要执行此操作,您将需要实现逻辑以发现firstResponder.

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.

这篇关于自定义UIMenuController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆