Mark A.
Mark A.

Reputation: 1093

Stopping a UIGestureRecognizer from calling selector

I'm adding in a few UIGestureRecognizers with a target and selector. I'll just talk about one since the other will be the same i'm sure.

I've added a UIPinchGestureRecognizer

UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer new];
[pinch addTarget:self action:@selector(pinchGestureDetected:)];
[self.view setMultipleTouchEnabled:YES];
[self.view addGestureRecognizer:pinch];

Now my goal here is to simply call this method once when I receive a pinch gesture. But obvoiusly it continues to call it as the person pinches. I'm using it as part of a page navigation and will be updating the view when a pinch in is detected.

So in my -(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch method I'll be calling another method. Kinda like ... and this is a little sudo

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1) layoutViewWithMoreDetail;
else layoutViewWithLessDetail;
}

So I don't want it to keep calling this method or the layout method will continue to be called. I want one layout / pinch gesture.

Is there a way I can stop detecting the pinch once it has determined the scale?? Something along the way of ...

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1)
{
layoutViewWithMoreDetail;
stop receiving pinch gestures till this.gesture is finished;
}

Would I impliment the GestureDelegate??

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1 && pinching == NO )
{
layoutViewWithMoreDetail;
pinching = YES;
}

and then in the delegate for Gesture ended ... pinching = NO;

Thanks for any help

Upvotes: 0

Views: 758

Answers (1)

CrimsonDiego
CrimsonDiego

Reputation: 3616

UPinchGestureRecognizer is a continuous gesture - use if(pinch.state == UIGestureRecognizerStateBegan) to detect whether or not the event is just now starting.

However, this results in a low threshold for triggering the event. An alternate method is to quicky disable and enable a gesture when it has been triggered to your satisfaction, like so:

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
    if (pinch.scale > 1)
    {
        //do your stuff here
        pinch.enabled = NO;
        pinch.enabled = YES;
    }
}

This is because if you look at the documentation, it states:

enabled

A Boolean property that indicates whether the gesture recognizer is enabled. @property(nonatomic, getter=isEnabled) BOOL enabled

Discussion

Disables a gesture recognizers so it does not receive touches. The default value is YES. If you change this property to NO while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state.

Upvotes: 2

Related Questions