UIScreenEdgePanGestureRecognizer Run multiple times

I have the following code in viewDidLoad on a UIViewController:

UIScreenEdgePanGestureRecognizer *edgeRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightEdgeSwipe:)]; edgeRecognizer.edges = UIRectEdgeRight; [self.view addGestureRecognizer:edgeRecognizer]; 

and the goal is to trigger a slide show when a right edge gesture is detected.

 -(void)handleRightEdgeSwipe:(UIGestureRecognizer*)sender { NSLog(@"Showing Side Bar"); [self presentPanelViewController:_lightPanelViewController withDirection:MCPanelAnimationDirectionRight]; } 

But I see that the handleRightEdgeSwipe function is run several times - sometimes 5 times, which makes the sidebar view, which should smoothly animate the slide in Flash several times.

(NOTE: I tried to start the view from UIButton and it works fine).

Why is the right edge gesture triggered several times and how can I fix it?

+7
ios objective-c uigesturerecognizer
source share
2 answers

As noted, UIScreenEdgePanGestureRecognizer calls your action several times as the state of the GestureRecognizer changes. See the documentation for the state property of the UIGestureRecognizer class. So, in your case, I believe that the answer you are looking for is to check if the state has ended. Thus:

 -(void)handleRightEdgeSwipe:(UIGestureRecognizer*)sender { if (sender.state == UIGestureRecognizerStateEnded) { NSLog(@"Showing Side Bar"); [self presentPanelViewController:_lightPanelViewController withDirection:MCPanelAnimationDirectionRight]; } } 
+16
source share

This gesture is not a one-shot event, but continuous.

handleRightEdgeSwipe: called once when sender.state changes or the touch moves. You must move the UIButton depending on the state gesture and locationInView:

+3
source share

All Articles