Binky
Binky

Reputation: 39

UIGestureRecognizer on multiple view

There is several examples on here about how to get multiple views recognized by a single UIGestureRecognizer. I have this code, and i've tried varies ways to get it work but unfortunately doesn't seem to work. I would appreciate it if one can shed some light on this.

the only way that i was able to get it to work if I create several (IBAction) functions but i have up to 100 views that need to be moved around so that would require to have 100 different functions. I read an example here where tag can be used to do just that but i tried it and unfortunately it does not work. I'm using XCode 4.6.1 if that helps. Here's the code below.

-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
     UIView *player=recognizer.view;
     int tag=player.tag;
     [player addGestureRecognizer:recognizer];
if (tag!=0) {
    CGPoint translation =[recognizer translationInView:self.view];
    player.center=CGPointMake(player.center.x+translation.x, player.center.y+translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:player.superview];
}  
}

Upvotes: 0

Views: 610

Answers (1)

CharlieReed
CharlieReed

Reputation: 126

It seems to me that you are adding the same gesture recognizer to the view that already has it. You could use a loop using the tag to add the UIPanGestureRecognizer to all your views and then in the method handlePan:(UIPanGestureRecognizer *)recognizer check which view is currently being panned and continue from there.

Upvotes: 1

Related Questions