Reputation: 2736
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?
Upvotes: 10
Views: 1929
Reputation: 448
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];
}
}
Upvotes: 22
Reputation: 4676
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:
.
Upvotes: 4