我在UIViewController上的viewDidLoad中有以下代码:
UIScreenEdgePanGestureRecognizer *edgeRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightEdgeSwipe:)]; edgeRecognizer.edges = UIRectEdgeRight; [self.view addGestureRecognizer:edgeRecognizer];
目的是在检测到右边缘手势时触发视图滑入.
-(void)handleRightEdgeSwipe:(UIGestureRecognizer*)sender { NSLog(@"Showing Side Bar"); [self presentPanelViewController:_lightPanelViewController withDirection:MCPanelAnimationDirectionRight]; }
但是我看到“handleRightEdgeSwipe”功能被多次触发 – 有时是5次,这使侧边栏视图应该平滑地动画滑入闪光灯多次.
(注意:我尝试触发视图从UIButton出现,它工作正常).
为什么右边的手势会多次触发,我该如何解决?
解决方法
如上所述,随着GestureRecognizer状态的变化,UIScreenEdgePanGestureRecognizer会多次调用您的操作.请参阅UIGestureRecognizer类的state属性的文档.所以,在你的情况下,我相信你正在寻找的答案是检查状态是否“结束”.从而:
-(void)handleRightEdgeSwipe:(UIGestureRecognizer*)sender { if (sender.state == UIGestureRecognizerStateEnded) { NSLog(@"Showing Side Bar"); [self presentPanelViewController:_lightPanelViewController withDirection:MCPanelAnimationDirectionRight]; } }