2

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.

Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

1 Answers1

6

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

Ian Henry
  • 22,255
  • 4
  • 50
  • 61
  • Thanks. I thought the same. I have created UITableView and inserted each UIImageView as Cell background to make GUI exactly similar to my requirements. Attached UIGestureRecognizer to UITableView and done! Do you have idea why I can't attache gesture recogniser to the imageview inside the Subview? – Paresh Masani Apr 03 '12 at 14:40
  • What do you mean by "the subview"? You added a subview to a table view? – Ian Henry Apr 03 '12 at 15:29
  • Sorry for the confusion. I added UITableView on the main view and hidden it until it's required. The good way to do it is Create separate view add all required controls and whenever need raised you add this view as a subview to main view. It works for me if I add UITableView in main view(self.view) and make it hidden until its required as said but it doesn't work I create another view(mysubview), add uitableview into it, add this view to main view (self.view addsubview:mysubview). Now UITableView doesn't recognise the gestures! – Paresh Masani Apr 03 '12 at 15:51
  • Hmm. I don't know what the issue could be. I'm assuming that user interaction is enabled in the subview to which you're adding the table view? This sounds like a problem that merits a separate question. – Ian Henry Apr 03 '12 at 15:55
  • No I have made sure that user interaction enabled. This question is part of original question! – Paresh Masani Apr 03 '12 at 18:21
  • Your question in its current state does not have enough information to diagnose the problem. – Ian Henry Apr 03 '12 at 22:42
  • I am not sure what could be the issue. Its might be gesture recogniser events needs to be redirected but I am happy for now to put tableview in main view! – Paresh Masani Apr 04 '12 at 09:20