【发布时间】:2014-03-01 22:50:52
【问题描述】:
我有一个如下所示的场景
现在,我只显示 1 个标签为 8 的视图。但我计划在 HolderView 中再添加 3 个这样的视图。
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");
}
- 我已经为所有视图和小视图上的 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;
为什么绿色边框只有那么大?那不应该是完整的正方形吗? 而且,手势只有在我点击绿色区域时才有效。为什么?
【问题讨论】: