【问题标题】:Adding a UITapGestureRecognizer to a view then removing seems to short circuit button events将 UITapGestureRecognizer 添加到视图然后删除似乎会使按钮事件短路
【发布时间】:2014-06-10 23:35:56
【问题描述】:

在下面的代码中,由于用户 touchUpInside 在一个简单的信息按钮上,我弹出了一个 ImageView。视图上还有其他按钮。

为了消除信息,我在控制器视图中添加了一个 UITapGestureRecognizer,并在检测到点击时隐藏视图。

如果我不删除 tapGestureRecognizer,则每次都会调用该操作。

即使我确实删除了手势操作,一旦添加了此手势识别器,按钮也不会收到 touchUpInside 事件。为什么?

我的 MainViewController 中的代码

- (void) dismissInfo: (UITapGestureRecognizer *)gesture {
    [kInfoView setHidden: YES];
    [gesture removeTarget: self action: NULL];
}

- (IBAction) displayInfo {      
    CGRect startFrame = CGRectMake(725, 25, 0, 0), origFrame;
    CGFloat yCenter = [kInfoView frame].size.height/2 + 200;
    CGPoint startCenter = CGPointMake(724, 25), displayCenter = CGPointMake(384, yCenter);
    UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc] initWithTarget: self
                                                                        action: @selector(dismissInfo:)];

    [self.view addGestureRecognizer: g];
    origFrame = [kInfoView frame];
    [kInfoView setCenter: startCenter];
    [kInfoView setHidden: NO];
    [kInfoView setFrame: startFrame];

    [UIView beginAnimations: @"info" context: nil];
    [UIView setAnimationDuration: .5];
    [UIView setAnimationDelegate: self];

    [kInfoView setFrame: origFrame];
    [kInfoView setCenter: displayCenter];

    [UIView commitAnimations];
}

【问题讨论】:

    标签: iphone events event-handling uigesturerecognizer


    【解决方案1】:

    我可以想象一种可能的解决方案:
    除了隐藏 view 之外,您还可以将其从 superview 中删除(并在需要时再次添加)。我认为在这种情况下,GestureRecognizer 不再处于活动状态。

    【讨论】:

      【解决方案2】:

      您删除手势识别器的方式会从您的班级中删除所有手势识别器。不仅是你设置的,还有在“super”中设置的。

      这就是您以这种方式移除手势识别器后没有收到 touchUpInside 事件的原因。

      根据您所写的内容,我认为可能有比使用 UITapGestureRecognizer 更简单的方法,但如果没有更多关于您要完成的工作的信息,我无法确定。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多