iOS:Tap Recognizer不一致

昌都

我有一个如下所示的情况 设想

  1. 现在,我只显示带有标签8的1个视图。但是我打算再向HolderView添加3个这样的视图。

  2. SmallerView是从其他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;

为什么那个绿色边界只会这么大?那不应该是正方形吗? 而且,仅当我点击绿色区域时,该手势才起作用。为什么? 在此处输入图片说明

昌都

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章