2

多个 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 中起作用……我做错了什么……

4

2 回答 2

7

你做错了什么是试图以不应该使用的方式使用手势。手势只能附加到一个视图。您需要为每个视图创建一个新视图。

于 2013-04-10T08:50:30.523 回答
7

正如 borrrden 所说,在尝试跟踪手势时,每个视图都必须有自己的手势识别器。对于您的每个 sample_books,您应该使用

[sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(myFunction:)]];

而不是尝试多次添加相同的 GR

myFunction: 收到的参数将是正确的 tapGR 并且您可以通过调用 sender.view 来访问被点击的 imageView (提供您的 myFunction 签名看起来像

- (void) myFunction:(UIGestureRecognizer *)sender

干杯,

于 2013-04-10T09:00:11.367 回答