【问题标题】:iOS: Tap Recognizer not consistentiOS:点击识别器不一致
【发布时间】:2014-03-01 22:50:52
【问题描述】:

我有一个如下所示的场景

  1. 现在,我只显示 1 个标签为 8 的视图。但我计划在 HolderView 中再添加 3 个这样的视图。

  2. SmallerView/s 是从其他 Nib 文件创建的。

我为 ViewController 的视图添加 Tap Recognizer 编写了这段代码

UITapGestureRecognizer *tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self                                                                         action:@selector(tapRecognized2:)];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];

将 Tap Recognizer 添加到较小视图的代码 我向 HolderView 添加了较小的视图。并为他们分配了标签 ID。之后,

for (SmallerView *view in HolderView.subviews) {
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
    [recognizer setDelegate:self];
NSLog(@"Added gesture to the view with tag: %ld",view.tag);

    [view addGestureRecognizer:recognizer];
}

3。 - (void)tapRecognized:(UITapGestureRecognizer *)paramSender { NSLog(@"点击 %ld", paramSender.view.tag); }

- (void)tapRecognized2:(UITapGestureRecognizer *)paramSender
{
    NSLog(@"VC view");
}
  1. 我已经为所有视图和小视图上的 UILabel 启用了 UserInteraction(在代码和 Inspector 中)。

现在的问题是... 较小视图的 Tap 识别器实际上并不能始终如一地工作。有时他们会打印输出。突然间,它打印了 ViewController 的识别器的输出。 请帮忙

更新: 下面是我的视图图。 绿色边框:(在 UIView 的 initWithFrame 中)d

self.layer.borderColor = [UIColor greenColor].CGColor;

红色边框:

    MyTile *tile = [[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil] objectAtIndex:0];
self.myLabel.layer.borderColor=[UIColor redColor].CGColor;

为什么绿色边框只有那么大?那不应该是完整的正方形吗? 而且,手势只有在我点击绿色区域时才有效。为什么?

【问题讨论】:

    标签: ios uigesturerecognizer


    【解决方案1】:

    您似乎在视图上有一个点击手势识别器,在其父视图上也有一个点击手势识别器。手势识别器的工作方式是,默认情况下,两个都将成为识别点击的候选者。如果这不是你想要的,你可以阻止它。

    你可以:

    • 在手势识别器之间建立“先行者”关系,或

    • 您可以使用他们的代表来帮助他们做出决定,或者

    • 您可以设置子视图,使其阻止父视图的手势识别器识别。

    您有很多选择!

    但是如果你什么都不做,那么这个视图的手势识别器和它的父视图的手势识别器都会尝试识别。

    【讨论】:

    • 谢谢马特!会试试的!
    【解决方案2】:

    也许更好的解决方案是向父视图添加一个 tapGestureRecognizer。

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [tap addTarget:self action:@selector(handleTap:)];
    [holderView addGestureRecognizer:tap];
    

    然后添加目标方法

    - (void) handleTap:(UITapGestureRecognizer *)tap {
        UIView * holderView;
    
        CGPoint tapPoint = [tap locationInView:holderView];
        for (UIView * v in holderView.subviews) {
            if (CGRectContainsPoint(v.frame, tapPoint)) {
                // v is the subview that was pressed.
                // add your code here.
    
    
                break;
            }
        }
    }
    

    【讨论】:

    • 谢谢。我的小视图(v,根据您的代码)v 上有一个标签。如果用户点击标签,那么 v 会响应手势吗?而且,只有当我点击视图的左上角时它才有效。你能帮忙吗?
    • 如果 superView 有点击手势识别器,而子视图没有点击手势,它应该能够接收所有触摸。确保您的子视图都在其父视图的范围内。
    • 你说的在我的代码中都是真的。所有子视图都在我的超级视图的范围内。但在每个子视图的左上角区域仅响应手势。不知道我什么时候做错了。
    • 有些东西没有意义,你能包含更多你的代码,或者发布一个 git 吗?
    • 也许,将您的子视图设置为 userInteractionEnabled = NO;这样一来,他们所有的触摸都肯定会通过。
    【解决方案3】:

    我已为边框错误的框架分配了一个新框架。这有助于做出正确的手势。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      相关资源
      最近更新 更多