Search code examples
iosipaduiimageviewuigesturerecognizersubviews

iOS: issues with UIGestureRecognisers vs Subviews


I have written following code to attach gesture recogniser to multiple imageviews.

[imageview1 setUserInteractionEnabled:YES];
[imageview1 setMultipleTouchEnabled:YES];

[imageview2 setUserInteractionEnabled:YES];
[imageview2 setMultipleTouchEnabled:YES];

[imageview3 setUserInteractionEnabled:YES];
[imageview3 setMultipleTouchEnabled:YES];

[imageview4 setUserInteractionEnabled:YES];
[imageview4 setMultipleTouchEnabled:YES];

[imageview5 setUserInteractionEnabled:YES];
[imageview5 setMultipleTouchEnabled:YES];

[imageview6 setUserInteractionEnabled:YES];
[imageview6 setMultipleTouchEnabled:YES];

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer.delegate = self;
[imageview1 addGestureRecognizer:gestureRecognizer];
[imageview2 addGestureRecognizer:gestureRecognizer];
[imageview3 addGestureRecognizer:gestureRecognizer];
[imageview4 addGestureRecognizer:gestureRecognizer];
[imageview5 addGestureRecognizer:gestureRecognizer];
[imageview6 addGestureRecognizer:gestureRecognizer];

I noticed two issues!

  1. All imageview doens't have gesture recogniser attached! Only one imageview6(the last attached) has the gesture recogniser. Is this something apple doesn't allow?

  2. I have all these imageviews in subview of parent view. When I add these directly to parent view (self.view), it works but still issue#1 remains. When I have these imageviews in subview (self.view.mysubview), none of them recognise the gestures!

Could someone please tell me how to deal with these issues please.

Thanks.


Solution

  • UIGestureRecognizers can only be attached to one view at a time. You will have to create a separate one for each image view.