Attach GestureRecogniser to multiple imageviews

Posted by AppleDeveloper on Stack Overflow See other posts from Stack Overflow or by AppleDeveloper
Published on 2012-07-04T14:09:58Z Indexed on 2012/07/04 15:15 UTC
Read the original article Hit count: 199

Something strange I encountered today while attaching same gesture recogniser to multiple image views. It gets attached to only the last one, in other words, it can be attached to only one view!

I had to create multiple gesture recognisers to meet my requirements.

Following is what I have done. Am I doing correct? Is that's the only way to attach recognisers to the multiple imageviews?

Please note that I don't want to use UITableView or UIVIew and put all imageviews in it and attach gesture recogniser to only UITableView or UIVIew. I have all image scattered and I have to detect which image is being dragged. Thanks.

[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];

© Stack Overflow or respective owner

Related posts about ios

Related posts about uiimageview