Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScreenEdgePanGestureRecognizer Triggering Multiple Times

I have the following code in a viewDidLoad on a UIViewController:

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

and the purposes is to trigger a view to slide in when a right edge gesture is detected.

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

But I am seeing that the "handleRightEdgeSwipe" function is triggered multiple times - sometimes 5 times which makes the side bar view that should smoothly animate slide in to flash multiple times.

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

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

like image 757
motionpotion Avatar asked Oct 07 '14 20:10

motionpotion


2 Answers

As noted, the UIScreenEdgePanGestureRecognizer invokes your action multiple 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 the answer you're looking for is to check if the state is "ended". Thus:

-(void)handleRightEdgeSwipe:(UIGestureRecognizer*)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"Showing Side Bar");
        [self presentPanelViewController:_lightPanelViewController withDirection:MCPanelAnimationDirectionRight];
   }
}
like image 141
Brian Redman Avatar answered Oct 24 '22 21:10

Brian Redman


This gesture is not a single-shot event but continuous.

handleRightEdgeSwipe: is called once whenever sender.state changes or the touch moved around. You have to move the UIButton depending on the gesture's state and locationInView:.

like image 33
fluidsonic Avatar answered Oct 24 '22 22:10

fluidsonic