0

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

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

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 存在,但是当我点击它们时不会调用手势识别器选择器。为什么不?

4

2 回答 2

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];
于 2012-12-12T02:31:35.530 回答
1

仔细检查并确保您添加 UIGestureRecognizer 的 UIView 已UserInteractionEnabled设置为YES. IE

[self.imageView setUserInteractionEnabled:YES];

于 2013-03-08T08:27:34.717 回答