我有一个如下所示的情况
现在,我只显示带有标签8的1个视图。但是我打算再向HolderView添加3个这样的视图。
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");
}
现在的问题是...较小视图的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] 删除。
我来说两句