Har*_*ran 2 objective-c uiimageview ios uitapgesturerecognizer
多个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];
Run Code Online (Sandbox Code Playgroud)
点击手势在sample_book1,sample_book2中不起作用....它只在sample_book3中工作..我做错了什么..
正如borrrden所说,在尝试跟踪手势时,每个视图都必须有自己的gestureRecognizer.对于每个sample_books,您都应该使用
[sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(myFunction:)]];
Run Code Online (Sandbox Code Playgroud)
而不是试图多次添加相同的GR
myFunction收到的参数:然后是正确的tapGR,你可以通过调用sender.view来获取tapped imageView(提供你的myFunction签名看起来像
- (void) myFunction:(UIGestureRecognizer *)sender
Run Code Online (Sandbox Code Playgroud)
干杯,
| 归档时间: |
|
| 查看次数: |
3358 次 |
| 最近记录: |