UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];
[self.view2 addGestureRecognizer:tapGesture];
[tapGesture release];
In the above code only taps on view2
are recognized. If I comment out the third line then taps on view1
are recognized. If I'm right and you can only use a gesture recognizer once, I'm not sure if this is a bug or it just needs some more documentation.
Well if someone does not want to code for adding gesture view for multiple buttons like kwalker has answered above, and want to do it via Interface Builder this may help you.
1) You can add Long Press gesture Recognizer from Object Library like you add other objects like UIButtons and UILabels.
Initially what I ended up using was I took only one
2) Set referencing outlets to
UIButton
and sent actions with File's Owner.if you have fixed view I suggest you doing something like this
that way will reduce multiple different useless variable
A
UIGestureRecognizer
is to be used with a single view. I agree the documentation is spotty. ThatUIGestureRecognizer
has a singleview
property gives it away:We can do something Like this, it's easy and simple
1) create function as below in your controller (this function will return GestureRecognizer)
2) now set this recognizer in multiple views
For Swift 3 in case anyone requires this: Based on Bhavik Rathod Answer above.
I got around it by using the below.
Then in my handleLongPress method I just set a UIButton equal to the view of the gesture recognizer and branch what I do based upon that button