Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you attach a UIGestureRecognizer to multiple views?

A UIGestureRecognizer is to be used with a single view. I agree the documentation is spotty. That UIGestureRecognizer has a single view property gives it away:

view

The view the gesture recognizer is attached to. (read-only)

@property(nonatomic, readonly) UIView *view

Discussion You attach (or add) a gesture recognizer to a UIView object using the addGestureRecognizer: method.


I got around it by using the below.

for (UIButton *aButton in myButtons) {

            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
            longPress.minimumPressDuration=1.0;
            [aButton addGestureRecognizer:longPress];
            [longPress release];

}

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

- (void)handleLongPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        UIButton *whichButton=(UIButton *)[gesture view];
        selectedButton=(UIButton *)[gesture view];
    ....
}

For Swift 3 in case anyone requires this: Based on Bhavik Rathod Answer above.

 func setGestureRecognizer() -> UIPanGestureRecognizer {

        var panRecognizer = UIPanGestureRecognizer()

        panRecognizer = UIPanGestureRecognizer (target: self, action: #selector(pan(panGesture:)))
        panRecognizer.minimumNumberOfTouches = 1
        panRecognizer.maximumNumberOfTouches = 1
        return panRecognizer
    }

        ///set the recognize in multiple views
        view1.addGestureRecognizer(setGestureRecognizer())
        view2.addGestureRecognizer(setGestureRecognizer())

We can do something Like this, it's easy and simple

1) create function as below in your controller (this function will return GestureRecognizer)

-(UITapGestureRecognizer*)setRecognizer{
     UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openProfile)];
     [gestureRecognizer setNumberOfTapsRequired:1];
     return gestureRecognizer;
}

2) now set this recognizer in multiple views

[self.view1 addGestureRecognizer:[self setRecognizer]]; 
[self.view2 addGestureRecognizer:[self setRecognizer]];

No you should not attach gesture recognizers to more than one view.

There is this explicit information in the Apple documentation:

Gesture Recognizers Are Attached to a View

Every gesture recognizer is associated with one view. By contrast, a view can have multiple gesture recognizers, because a single view might respond to many different gestures. For a gesture recognizer to recognize touches that occur in a particular view, you must attach the gesture recognizer to that view.

Event Handling Guide for iOS - Gesture Recognizers Apple Developer Library

While as others mention they might work in some cases it is clearly against the documentation and could change in any future iOS version.

What you can do is add separate gesture recognisers to the views you want to monitor and they can share a common action.


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.

enter image description here Initially what I ended up using was I took only one

2) Set referencing outlets to UIButton and sent actions with File's Owner.

enter image description here

Note: If you have multiple UIButton or any other object you will need separate gesture recognizer for each of them. For more details please refer to this question of mine.Getting wrong UIButton tag on Long press gesture recognizer


if you have fixed view I suggest you doing something like this

[self.view1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)]];
[self.view2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)]];

that way will reduce multiple different useless variable