【问题标题】:GestureRecognizer Swipe GestureGestureRecognizer 滑动手势
【发布时间】:2014-04-11 09:06:33
【问题描述】:

我正在尝试通过连接到CrossSliding 事件来检测带有GestureRecognizer 的水平滑动手势。

        _gr = new GestureRecognizer
        {
            GestureSettings = GestureSettings.ManipulationTranslateX |
                              GestureSettings.ManipulationTranslateY |
                              GestureSettings.CrossSlide
        };

        _gr.CrossSliding += OnSwipe;
        _gr.ManipulationStarted += OnManipulationStarted;
        _gr.ManipulationUpdated += OnManipulationUpdated;
        _gr.ManipulationCompleted += OnManipulationCompleted;

        _gr.CrossSlideHorizontally = true;

从上面的代码可以看出,不仅要检测滑动,还要检测拖动手势。

我的问题是我似乎无法自定义滑动手势。

我想自定义在手势被视为滑动之前用户必须拖动指针的最小速度和距离。在当前状态下,即使是最慢和最小的水平拖动动作也会触发CrossSliding 事件。

我看到了允许自定义手势的CrossSlideThresholds 类,但我看不出它如何用于配置滑动手势的速度和距离。

CrossSliding 事件是检测滑动的正确方法吗?如果是这样,我该如何配置速度和滑动距离?

如果没有,我如何检测滑动手势?

【问题讨论】:

  • 您对滑动的理解是什么?我认为在 Windows 8 的情况下,它是用来选择列表项的,并且交叉滑动描述了这个手势垂直于 ScrollViewer 的平移方向。这是您要检测的内容吗?
  • 不,不是这样,我的意思是快速的水平或垂直触摸点移动,就像您用来在设备上翻阅书籍或专辑封面的页面(希望有意义)。我重新阅读了文档并得出结论,没有内置的方法来检测滑动。我实现了自己的检测并将添加答案

标签: c# windows-runtime microsoft-metro windows-store-apps


【解决方案1】:

我找不到任何内置的方法来检测滑动,所以我实现了自己的检测方法。

代码检测到水平滑动。

显示的方法是GestureRecognizer 事件的事件处理程序:

readonly Stopwatch _manipulationTimer = new Stopwatch();

public void OnManipulationStarted(ManipulationStartedEventArgs e)
{
    _manipulationTimer.Restart();
}

public void OnManipulationCompleted(ManipulationCompletedEventArgs e)
{
    var millis = _manipulationTimer.ElapsedMilliseconds;

    if (Math.Abs(e.Cumulative.Translation.Y) < MaxVerticalSwipeDistanceInPix &&
        Math.Abs(e.Cumulative.Translation.X) > MinHorizontalSwipeDistanceInPix && 
        millis > MinSwipeDurationInMillis && 
        millis < MaxSwipeDurationInMillis && 
        Math.Abs(e.Cumulative.Scale - 1) < 0.01 // 1 touch point 
        )
    { 
        var leftSwipe = e.Cumulative.Translation.X < 0;
        if (leftSwipe)
        {
        }
        else // right swipe
        {               
        }
    }

    _manipulationTimer.Stop();
    _manipulationTimer.Reset();
}

【讨论】:

  • 似乎是个不错的方法。此外,您可以通过处理 ManipulationInertiaStarting 事件并检查平移速度以查看轻弹的方向来检测轻弹。
  • @FilipSkakun 谢谢你的建议,我还没考虑过
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
相关资源
最近更新 更多