将GestureRecogniser附加到多个图像浏览

今天遇到的一些奇怪的事情,同时将同一个手势识别器连接到多个图像视图。 它只附加到最后一个,换句话说,它可以附加到只有一个视图!

我不得不创build多个手势识别器来满足我的要求。

以下是我所做的。 我正确吗? 这是将识别器附加到多个图像视图的唯一方法吗?

请注意,我不想使用UITableView或UIVIew,并把所有的图像,并附加手势识别器只有UITableView或UIVIew。 我有所有图像分散,我必须检测哪个图像被拖动。 谢谢。

[imgView1 setUserInteractionEnabled:YES]; [imgView1 setMultipleTouchEnabled:YES]; [imgView2 setUserInteractionEnabled:YES]; [imgView2 setMultipleTouchEnabled:YES]; [imgView3 setUserInteractionEnabled:YES]; [imgView3 setMultipleTouchEnabled:YES]; [imgView4 setUserInteractionEnabled:YES]; [imgView4 setMultipleTouchEnabled:YES]; [imgView5 setUserInteractionEnabled:YES]; [imgView5 setMultipleTouchEnabled:YES]; [imgView6 setUserInteractionEnabled:YES]; [imgView6 setMultipleTouchEnabled:YES]; //Attach gesture recognizer to each imagviews gestureRecognizer1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; gestureRecognizer1.delegate = self; gestureRecognizer2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; gestureRecognizer2.delegate = self; gestureRecognizer3 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; gestureRecognizer3.delegate = self; gestureRecognizer4 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; gestureRecognizer4.delegate = self; gestureRecognizer5 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; gestureRecognizer5.delegate = self; gestureRecognizer6 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; gestureRecognizer6.delegate = self; [imgView1 addGestureRecognizer:gestureRecognizer1]; [imgView2 addGestureRecognizer:gestureRecognizer2]; [imgView3 addGestureRecognizer:gestureRecognizer3]; [imgView4 addGestureRecognizer:gestureRecognizer4]; [imgView5 addGestureRecognizer:gestureRecognizer5]; [imgView6 addGestureRecognizer:gestureRecognizer6]; 

是的,每个手势识别器都有一个视图。 所以如果你只想要一个识别器,就把它放在超级视图上,例如:

 UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; [self.view addGestureRecognizer:gestureRecognizer]; 

然后,在你的处理程序中,你可以:

 - (void)handleLongPress:(UILongPressGestureRecognizer *)sender { CGPoint location = [sender locationInView:self.view]; if (sender.state == UIGestureRecognizerStateBegan) { for (UIView *view in self.view.subviews) { if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location)) { UIImageView *image = (UIImageView *) view; // ok, now you know which image you received your long press for // do whatever you wanted on it at this point return; } } } } 

顺便说一下,如果你这样做,你也不用担心在图像上启用用户交互。

最后,你不需要担心指定你的手势识别器的委托,除非你要符合UIGestureRecognizerDelegate ,这不是。 另外请注意,我为我的识别器使用了一个本地variables,因为没有理由挂在它上面。

更新:

虽然上面的代码工作正常,也许更好的是一个自定义的长按手势识别器,如果长按没有发生在图像上,将失败(这样,如果您有其他手势识别器放在你的视野里)。 所以:

 #import <UIKit/UIGestureRecognizerSubclass.h> @interface ImageLongPressGestureRecognizer : UILongPressGestureRecognizer @property (nonatomic, weak) UIImageView *imageview; @end @implementation ImageLongPressGestureRecognizer @synthesize imageview = _imageview; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { self.imageview = nil; [super touchesBegan:touches withEvent:event]; CGPoint location = [self locationInView:self.view]; for (UIView *view in self.view.subviews) { if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location)) { self.imageview = (UIImageView *)view; return; } } self.state = UIGestureRecognizerStateFailed; } @end 

然后使用这个新的子类创build你的手势识别器:

 ImageLongPressGestureRecognizer *gestureRecognizer = [[ImageLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; [self.view addGestureRecognizer:gestureRecognizer]; 

然后,作为这个子类的一个很好的小好处,您的主要手势识别器被简化,即:

 - (void)handleLongPress:(ImageLongPressGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateBegan) { // you can now do whatever you want with sender.imageview, eg this makes it blink for you: [UIView animateWithDuration:0.5 animations:^{ sender.imageview.alpha = 0.0; } completion:^(BOOL finished){ [UIView animateWithDuration:0.5 animations:^{ sender.imageview.alpha = 1.0; } completion:nil]; }]; } } 

您不能将手势识别器附加到多个对象(如您所发现的那样)。 你正在做的一个解决scheme可能是UIImageView的子类,并在该类中设置代码,以便每个视图创build它的识别器等。

我想,首先,你应该制作一系列的视图和识别器(可变数组,如果需要的话),然后填充它。 它将帮助您使用循环来避免代码重复。

至于多个视图与一个识别器 – 不,这是不可能的, 在这里回答 。