Reputation: 14304
Is it possible to alloc only one UIPanGestureRecognizer and attach to multiple views? I have multiple on-screen UIImageView objects that user can move (pan). If I'm making alloc and attaching the same UIPanGestureRecognizer object to all views then the pan gesture works only on the last attached view. I did a workaround by making multiple alloc/init of UIPanGestureRecognizer, I mean one different UIPanGestureRecognizer object for each view. Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(updateImagePosition:)];
[self.panningBtn1 addGestureRecognizer:pgr];
[pgr release];
pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(updateImagePosition:)];
[self.panningBtn2 addGestureRecognizer:pgr];
[pgr release];
...
PS. I also have enabled MultipleTouchEnabled & UserInteractionEnabled. Are there any more elegant solutions?
Upvotes: 2
Views: 1846
Reputation: 7946
No - each UIGestureRecognizer
can only belong to one view. Just create multiple with the same properties and assign them to the different views.
I suggest creating the gesture recognizer in a method:
-(UIGestureRecognizer *)myGRMethod {
UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(updateImagePosition:)];
return [pgr autorelease];
}
and then just doing
for (UIView *view in views) {
[view addGestureRecognizer:[self myGRMethod]];
}
Upvotes: 4