UITapGestureRecognizer无法识别

时间:2014-10-10 18:18:54

标签: ios objective-c

我正在编写简单的菜单(UIView)。 Singleton对象封装了所有绘图和显示

    @interface MenuManager()

@property UIView *menuView;

@end

@implementation MenuManager

static MenuManager *_menuManager;

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized)
    {
        initialized = YES;
        _menuManager = [[MenuManager alloc] init];
    }
}

+ (MenuManager*)singleton
{
    return _menuManager;
}

- (void)showInView:(UIView*)view animated:(BOOL)animated
{
    [self prepareMenuViewForRect:view.frame];

    [view addSubview:self.menuView];
    view.userInteractionEnabled = NO;

}

- (void)prepareMenuViewForRect:(CGRect)rect
{
    if (self.menuView != nil) return;

    self.menuView = [[UIView alloc] initWithFrame:rect];

    NSLog(@"Preparing to draw RECT MENU");
    const CGFloat menuWidth = rect.size.width - 20.0f; // make menu 10pt from sides
    const CGFloat menuItemHeight = 66.0f;
    const CGFloat menuSeparatorHeight = 3.0f;
    const CGFloat menuBottomHeight = 66.0f;

    CGFloat menuHeight = [self getMenuItemCount] * (menuItemHeight + menuSeparatorHeight) + menuSeparatorHeight + menuBottomHeight;
    CGFloat left = (rect.size.width - menuWidth) / 2.0f;
    CGFloat top = 64.0f;



    // Add "Refresh" menu item.-=-----------------------------------------
    UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(left, top, menuWidth, menuItemHeight)];
    refreshView.backgroundColor = UIColorFromRGB(0x4c4c4c);



    refreshView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshMenuClick)];
    [refreshView addGestureRecognizer:tgr];

    [self.menuView addSubview:refreshView];



    top += 100.0f;
    // Adding bottom bar with arrow:
    UIView *dragUpView = [[UIView alloc] initWithFrame:CGRectMake(left, top, menuWidth, menuItemHeight)];
    dragUpView.backgroundColor = UIColorFromRGB(0xababab);

    UIImageView *dragUpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_up"]];
    [dragUpImageView setFrame:CGRectMake((menuWidth - 36.0f)/2.0f, (menuItemHeight - 16.0f)/2.0f, 36.0f, 16.0f)];
    [dragUpView addSubview:dragUpImageView];

    [self.menuView addSubview:dragUpView];


}

- (void)refreshMenuClick
{
    NSLog(@"REFRESH MENU CLICK DETECTED!!!");
}

不执行refreshMenuClick。我错过了什么?

在我的View Controller中,我调用了这样的菜单:

- (IBAction)menuBarButtonClick:(UIBarButtonItem *)sender
{
    [[MenuManager singleton] showInView:self.tabBarController.view animated:YES];
}

2 个答案:

答案 0 :(得分:0)

refreshView的子视图正在捕获识别器。您可以通过将识别器添加到refreshLabel而不是refreshView来检查它。

编辑:

我只是尝试将您的代码添加到空VC中,它运行正常。也许你的观点有一些东西

答案 1 :(得分:0)

您已使用此行

禁用顶级视图的用户互动
view.userInteractionEnabled = NO;

这也将禁用所有此视图的子视图的用户互动。您需要将其设置为YES。