UIMenuController在UILongGestureRecognizer上不可见

时间:2013-11-03 12:17:16

标签: ios iphone objective-c uigesturerecognizer gesture

我在同一个问题上提到了关于SO的各种帖子。但仍然无法得到解决方案。

我有{strong>子类我UIButton所在的UILongGestureRecognizer。我的实施如下:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:longGesture];
    }
    return self;
}

- (BOOL)becomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return YES;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"ButtonView: longPress: event called");
        UIMenuController *menu = [UIMenuController sharedMenuController];
        if (![menu isMenuVisible])
        {
            ButtonView *btn = (ButtonView *)gesture.view;
            if (![btn becomeFirstResponder])
            {
                NSLog(@"couldn't become first responder");
                return;
            }

            UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)];
            UIMenuController *menuCont = [UIMenuController sharedMenuController];
            menuCont.arrowDirection = UIMenuControllerArrowDown;
            menuCont.menuItems = [NSArray arrayWithObject:menuItem];
            if([btn canBecomeFirstResponder])
            {
                [menuCont setTargetRect:btn.frame inView:btn.superview];
                [menuCont setMenuVisible:YES animated:YES];
                NSLog(@"menu visible....");
            }
        }
    }
    if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled || gesture.state == UIGestureRecognizerStateFailed)
    {
        [self.layer setBorderColor:[UIColor clearColor].CGColor];
        [self.layer setBorderWidth:0.0];
    }
}

我已覆盖becomeFirstRespondercanBecomeFirstResponder

重要提示:我的日志消息“菜单可见....”在我按下按钮时会被记录,但我无法看到UIMenuController可见。

上面的代码中是否还有任何遗漏?提前谢谢。

1 个答案:

答案 0 :(得分:2)

在显示菜单之前添加此行:

[self becomeFirstResponder];

并删除此代码:

- (BOOL)becomeFirstResponder
{
    return YES;
}