UIGestureRecognizers没有响应

时间:2012-12-12 02:17:50

标签: ios uiview uigesturerecognizer

我有许多弹出视图保存为视图控制器的属性。它们仅在呈现时添加到视图中,并在隐藏时从视图中删除。一切正常,但我改变了我的代码以简化事情,它已不再有效。

以下是我创建和添加手势识别器的示例:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePopUpView:)];
[self.totalPowerPopUpView addGestureRecognizer:tapGestureRecognizer];
[self.co2PopUpView addGestureRecognizer:tapGestureRecognizer];

视图由按下UIButton触发的选择器显示(if语句中通常还有其他代码设置自定义视图属性,但我为了简单起见将其剪切掉):

- (void)showPopUpView:(UIButton*)sender
{
    CGRect endFrame = CGRectMake(10, 10, 300, 400);
    UIView *popUpView;

    if (sender == self.totalPowerInfoButton)
    {
        [self.view addSubview:self.totalPowerPopUpView];
        popUpView = self.totalPowerPopUpView;
    }
    if (sender == self.co2LevelInfoButton)
    {
        [self.view addSubview:self.co2PopUpView];
        popUpView = self.co2PopUpView;
    }

    [UIView animateWithDuration:0.5
                     animations:^ {
                         popUpView.alpha = 1.0;
                         popUpView.frame = endFrame;
                     }];
}

popUpView存在,但是当我点击它时,手势识别器选择器不会被调用。为什么不呢?

2 个答案:

答案 0 :(得分:4)

这是否有效:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]     initWithTarget:self action:@selector(hidePopUpView:)];
[self.totalPowerPopUpView addGestureRecognizer:tapGestureRecognizer];

tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePopUpView:)];
[self.co2PopUpView addGestureRecognizer:tapGestureRecognizer];

答案 1 :(得分:1)

仔细检查并确保您要添加UIGestureRecognizer,并将UserInteractionEnabled设置为YES。即。

[self.imageView setUserInteractionEnabled:YES];