简体   繁体   English

iOS:Tap Recognizer不一致

[英]iOS: Tap Recognizer not consistent

I have a scenario like shown below 我有一个如下所示的情况 脚本

  1. Right now, I am showing only 1 view with label 8. But I am planning to add 3 more such views to the HolderView. 现在,我只显示带有标签8的1个视图。但是我计划再向HolderView添加3个这样的视图。

  2. The SmallerView/s are created from other Nib files. SmallerView是从其他Nib文件创建的。

I did this code for adding Tap Recognizer for ViewController's view 我为添加用于ViewController的视图的Tap Recognizer的代码

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

Code for adding Tap Recognizer to the smaller views I added smaller views to the HolderView. 用于将Tap Recognizer添加到较小视图的代码我向HolderView添加了较小视图。 And assigned Tag IDs to them. 并为其分配标签ID。 After that, 之后,

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(@"tapped on %ld", paramSender.view.tag); 3.-(void)tapRecognized:(UITapGestureRecognizer *)paramSender {NSLog(@“在%ld上点击”,paramSender.view.tag); } }

- (void)tapRecognized2:(UITapGestureRecognizer *)paramSender
{
    NSLog(@"VC view");
}
  1. I have enabled UserInteraction (both in code and Inspector) for all the views and UILabels on smaller views too. 我也为较小的视图上的所有视图和UILabel启用了UserInteraction(在代码和Inspector中)。

The problem now is... The smaller view's Tap recognisers are not really working consistently. 现在的问题是... 较小视图的Tap识别器实际上并不能始终如一地工作。 Sometimes they print the output. 有时他们打印输出。 All at suddenly it prints the ViewController's recogniser's output. 突然之间,它会打印ViewController的识别器的输出。 Please help 请帮忙

UPDATE: Below is my View diagram. 更新:下面是我的视图图。 Green Border: (In UIView's initWithFrame)d 绿色边框:(在UIView的initWithFrame中)d

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

Red Border: 红色边框:

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

Why that Green Border is coming only of that size? 为什么那个绿色边界只会这么大? Shouldn't that be full square? 那不是正方形吗? And, the gesture works only when I tap on the green area. 并且,仅当我点击绿色区域时,手势才起作用。 Why? 为什么? 在此处输入图片说明

It seems that you have a tap gesture recognizer on a view and also a tap gesture recognizer on its superview. 看来您在视图上有轻击手势识别器,在其超级视图上也有轻击手势识别器。 The way gesture recognizers work is that, by default, both of them will be candidates to recognize the tap. 该方法手势识别的工作是,默认情况下,他们将是考生认识到水龙头。 If that is not what you want, it is up to you to prevent it. 如果那不是您想要的,则由您自己决定如何避免。

You can: 您可以:

  • set up a "you-go-first" relationship between the gesture recognizers, or 在手势识别器之间建立“先行先得”的关系,或者

  • you can use their delegates to help decide between them, or 您可以使用他们的代表来帮助他们之间进行决定,或者

  • you can set up the subview so that it stops the superview's gesture recognizer from recognizing. 您可以设置子视图,以使其停止识别超级视图的手势识别器。

You have lots of options! 您有很多选择!

But if you do none of them, then both this view's gesture recognizer and its superview's gesture recognizer will be trying to recognize. 但是,如果您都不执行任何操作,则该视图的手势识别器及其超级视图的手势识别器都将尝试识别。

Perhaps a better solution would just to add one tapGestureRecognizer to the parent view. 也许更好的解决方案是将一个tapGestureRecognizer添加到父视图。

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

Then add the target method 然后添加目标方法

- (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;
        }
    }
}

I have assigned a new frame to the frame which was having wrong borders. 我已为边框错误的框架分配了一个新框架。 This helped in getting the gestures right. 这有助于正确设置手势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM