This question already has an answer here:
- Can you attach a UIGestureRecognizer to multiple views? 10 answers
I am working on a sample of Pan Gesture. I am keen to know that can I add single gesture on two views?
My code is as follows:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(invokePanGesture:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:2];
[btnGreen addGestureRecognizer:panGesture];
[btnYellow addGestureRecognizer:panGesture];
my handler method is as follows:
- (void)invokePanGesture:(UIPanGestureRecognizer*)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
The problem now is gesture recognizer is considering only last view in my case btnYellow
as its view. If I create two separate GestureRecognizer object it works. So please clear me that:
Is it possible to have single gesture in multiple views or not?
If yes then how?
If now then why?
Thanks in Advance