【问题标题】:UIGestureRecognizers not respondingUIGestureRecognizers 没有响应
【发布时间】:2012-12-12 02:17:50
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios uiview uigesturerecognizer


    【解决方案1】:

    这行得通吗:

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

    【讨论】:

    • 是的。我想我无法将手势识别器附加到多个视图?
    • 没错。这是因为手势识别器具有作为属性附加的视图,而不是视图列表,因此只能附加一个。
    • @yuf 这在任何官方文档中都提到了吗?
    • @skyline75489 你的意思是说手势识别器只能附加到一个视图的文档?这听起来是由单数 view property 暗示的。
    • @yuf 这是有道理的。感谢您的回答。
    【解决方案2】:

    仔细检查并确保将 UIGestureRecognizer 添加到的 UIView 将 UserInteractionEnabled 设置为 YES。即

    [self.imageView setUserInteractionEnabled:YES];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2013-02-06
      • 2020-06-23
      • 2017-09-25
      • 2013-04-20
      相关资源
      最近更新 更多