多个UIImageView上的TapGesture识别器无法正常工作,同时它检测到上次添加的图像视图手势..我已经这样做了,
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
tapped.delegate = self;
UIImageView *sample_book1= [[UIImageView alloc]initWithFrame:CGRectMake(70, 135, 100,125) ];
sample_book1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mathematics.png"]];
sample_book1.userInteractionEnabled = YES;
sample_book1.tag = 0;
[sample_book1 addGestureRecognizer:tapped];
[self.view addSubview:sample_book1];
UIImageView *sample_book2= [[UIImageView alloc]initWithFrame:CGRectMake(220, 135, 100,125) ];
sample_book2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"biology.png"]];
sample_book2.userInteractionEnabled = YES;
sample_book2.tag = 1;
[sample_book2 addGestureRecognizer:tapped];
[self.view addSubview:sample_book2];
UIImageView *sample_book3= [[UIImageView alloc]initWithFrame:CGRectMake(370, 135, 100,125) ];
sample_book3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"chemistry.png"]];
sample_book3.userInteractionEnabled = YES;
sample_book3.tag = 2;
[sample_book3 addGestureRecognizer:tapped];
[self.view addSubview:sample_book3];
点击手势在sample_book1,sample_book2中不起作用....它只在sample_book3中工作..我做错了什么..
答案 0 :(得分:7)
你做错了是试图以不应该使用的方式使用手势。手势只能附加到一个视图。您需要为每个视图创建一个新视图。
答案 1 :(得分:7)
正如borrrden所说,在尝试跟踪手势时,每个视图都必须有自己的gestureRecognizer。 对于每个sample_books,您应该使用
[sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(myFunction:)]];
而不是尝试多次添加相同的GR
myFunction收到的参数:然后是正确的tapGR,你可以通过调用sender.view来获取tapped imageView(提供你的myFunction签名看起来像
- (void) myFunction:(UIGestureRecognizer *)sender
干杯,