Question

I have five views on which i have applied UIPanGestureRecognizer placed with some distance to each other having same Y-asix from top (box type shape).

I am dragging view with panning on x-axis. The problem is that i can drag multiple views with multiple touches. i want to restrict one view dragging at a time.

I tried using UIPanGestureRecognizer property

pan.maximumNumberOfTouches=1;

but that is for just single view. Any Ideas?

Was it helpful?

Solution

Try this too,

Lets say you have all this five views added on the view of a viewController. Then do the following;

myViewController.view.multipleTouchEnabled = NO;

This make the mainView to process only first touch and only this first touch will flow down the hierarchy (i.e. to subViews and their gestureRecognizers )

Hope this works.

Alernate:

  1. Define this in the interface of your mainView or mainViewController that holds the 5 views.

    @property(nonatomic,retain)UIPanGestureRecognizer *currentRecognizer;

    1. Then apply the following code.

-(void)on_pan_gesture:(UIPanGestureRecognizer*) panGestureRecognizer
{

if(self.currentRecognizer != nil && [self.currentRecognizer isEqual:panGestureRecognizer])

        {
            //do the task of the selected gesture recognizer
            //this recognizer will be active till the touches are not ended or cancelled.
            //hence on the first recognizer will work.
        }
        else
        {
            //if there is not currentRecognizer then set this recognizer to be current.
            self.currentRecognizer = panGestureRecognizer;
        }
    }

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.currentRecognizer = nil;
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.currentRecognizer = nil;
}

OTHER TIPS

Use the gesture recognizers' delegate method

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

to make it return NO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top