【问题标题】:UIScreenEdgePanGestureRecognizer Triggering Multiple TimesUIScreenEdgePanGestureRecognizer 触发多次
【发布时间】:2014-10-07 20:28:24
【问题描述】:

我在 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 显示的视图,它工作正常)。

为什么会多次触发右边缘手势,我该如何解决?

【问题讨论】:

  • 如果你想让它移动,而不是用你的手指拖动,你应该使用 UISwipeGestureRecognizer 代替。它识别单个事件而不是连续运动。
  • @rdelmar 我只想在边缘滑动时触发。如何在右边缘使用 UISwipeGestureRecognizer?
  • action方法中,需要查看locationInView:的值,只有该位置的x值在你想要的区域内才会触发action。

标签: ios objective-c uigesturerecognizer


【解决方案1】:

如前所述,UIScreenEdgePanGestureRecognizer 会随着 GestureRecognizer 状态的变化多次调用您的操作。请参阅 UIGestureRecognizer 类的 state 属性的文档。所以,在你的情况下,我相信你正在寻找的答案是检查状态是否“结束”。因此:

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

【讨论】:

    【解决方案2】:

    这个手势不是单次事件,而是连续的。

    handleRightEdgeSwipe:sender.state 更改或触摸移动时调用一次。您必须根据手势的statelocationInView: 移动UIButton

    【讨论】:

    • 你有答案的例子吗?我只想在右边缘触发。
    • 这里是 a tutorialsources on GitHub,应该会对您有所帮助。
    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多