检测简单的触摸手势 [英] Detect Simple Touch Gestures

查看:187
本文介绍了检测简单的触摸手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以就如何在WinRT的应用程序检测简单的触摸手势解释一下吗?我尝试使用 GestureRecognizer 类,但它没有工作:

 公开的MainPage()
    {
        this.InitializeComponent();
        Windows.UI.Input.GestureRecognizer克=新Windows.UI.Input.GestureRecognizer();
        gr.CrossSliding + = gr_CrossSliding;
        gr.Dragging + = gr_Dragging;
        gr.Holding + = gr_Holding;
        gr.ManipulationCompleted + = gr_ManipulationCompleted;
        gr.ManipulationInertiaStarting + = gr_ManipulationInertiaStarting;
        gr.ManipulationStarted + = gr_ManipulationStarted;
        gr.ManipulationUpdated + = gr_ManipulationUpdated;
        gr.RightTapped + = gr_RightTapped;
        gr.Tapped + = gr_Tapped;
        gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |
        Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |
        Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap;    }    无效gr_Tapped(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.TappedEventArgs参数)
    {
        的Debug.WriteLine(gr_Tapped);
    }
    无效gr_RightTapped(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.RightTappedEventArgs参数)
    {
        的Debug.WriteLine(gr_RightTapped);
    }
    无效gr_Holding(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.HoldingEventArgs参数)
    {
        的Debug.WriteLine(gr_Holding);
    }
    无效gr_Dragging(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.DraggingEventArgs参数)
    {
        的Debug.WriteLine(gr_Dragging);
    }
    无效gr_CrossSliding(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.CrossSlidingEventArgs参数)
    {
        的Debug.WriteLine(gr_CrossSliding);
    }
    无效gr_ManipulationUpdated(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.ManipulationUpdatedEventArgs参数)
    {
        的Debug.WriteLine(gr_ManipulationUpdated);
    }
    无效gr_ManipulationStarted(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.ManipulationStartedEventArgs参数)
    {
        的Debug.WriteLine(gr_ManipulationStarted);
    }
    无效gr_ManipulationCompleted(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.ManipulationCompletedEventArgs参数)
    {
        的Debug.WriteLine(gr_ManipulationCompleted);
    }
    无效gr_ManipulationInertiaStarting(Windows.UI.Input.GestureRecognizer发件人,Windows.UI.Input.ManipulationInertiaStartingEventArgs参数)
    {
        的Debug.WriteLine(gr_ManipulationInertiaStarting);
    }


解决方案

如果你会发现MainPage类有,你可以无需创建一个单独的 GestureRecognizer 。您可以通过设置 this.ManipulationMode ManipulationModes.All 启用。这将让你看到的MainPages响应螺纹 RightTapped ManipulationStarting 在ManipulationStarted 在ManipulationDelta 在ManipulationCompleted 活动。

至于获得GestureRecongnizer根据本博客工作,此<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/0dad4881-da1c-4db9-be8d-a9324a233662\">MSDN论坛发帖您将需要处理的MainPage的 PointerMoved PointerReleased 指针pressed 事件,像这样。

  Windows.UI.Input.GestureRecognizer克=新Windows.UI.Input.GestureRecognizer();公众的MainPage()
{
    this.InitializeComponent();
    this.Pointer pressed + = MainPage_Pointer pressed;
    this.PointerMoved + = MainPage_PointerMoved;
    this.PointerReleased + = MainPage_PointerReleased;
    gr.CrossSliding + = gr_CrossSliding;
    gr.Dragging + = gr_Dragging;
    gr.Holding + = gr_Holding;
    gr.ManipulationCompleted + = gr_ManipulationCompleted;
    gr.ManipulationInertiaStarting + = gr_ManipulationInertiaStarting;
    gr.ManipulationStarted + = gr_ManipulationStarted;
    gr.ManipulationUpdated + = gr_ManipulationUpdated;
    gr.RightTapped + = gr_RightTapped;
    gr.Tapped + = gr_Tapped;
    gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |
    Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |
    Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap;
}无效MainPage_PointerReleased(对象发件人,PointerRoutedEventArgs E)
{
    VAR PS = e.GetIntermediatePoints(NULL);
    如果(PS =空&放大器;!&放大器; ps.Count大于0)
    {
        gr.ProcessUpEvent(PS [0]);
        e.Handled =真实的;
        gr.CompleteGesture();
    }
}无效MainPage_PointerMoved(对象发件人,PointerRoutedEventArgs E)
{
    gr.ProcessMoveEvents(e.GetIntermediatePoints(NULL));
    e.Handled =真实的;
}虚空MainPage_Pointer pressed(对象发件人,PointerRoutedEventArgs E)
{
    VAR PS = e.GetIntermediatePoints(NULL);
    如果(PS =空&放大器;!&放大器; ps.Count大于0)
    {
        gr.ProcessDownEvent(PS [0]);
        e.Handled =真实的;
    }
}


按照文档你需要启用<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.gesturerecognizer.crosssliding.aspx\">CrossSlide事件将它添加到您的GestureRecongnizer搭好<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.crossslidethresholds.aspx\">CrossSlideThresholds和方向。
从最后一个环节:


  

拖板必须在GestureSettings属性设置为支持CrossSliding。
  CrossSliding距离阈值默认情况下禁用。使用CrossSlideThresholds设置这些值。


例如:

  Windows.UI.Input.CrossSlideThresholds CST =新Windows.UI.Input.CrossSlideThresholds();
cst.SelectionStart = 2;
cst.Speed​​BumpStart = 3;
cst.Speed​​BumpEnd = 4;
cst.RearrangeStart = 5;
gr.CrossSlideHorizo​​ntally = TRUE;
gr.CrossSlideThresholds =厘;
gr.CrossSliding + = gr_CrossSliding;

和确保它被添加到您的 GestureSettings

  gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX |
                     Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia |
                     Windows.UI.Input.GestureSettings.ManipulationScaleInertia | Windows.UI.Input.GestureSettings.ManipulationTranslateInertia |
                     Windows.UI.Input.GestureSettings.Tap | Windows.UI.Input.GestureSettings.CrossSlide;

Can anyone explain on how to detect simple touch gestures in a WinRT app? I tried using the GestureRecognizer class but it didn't work:

    public MainPage()
    {
        this.InitializeComponent();
        Windows.UI.Input.GestureRecognizer gr = new Windows.UI.Input.GestureRecognizer();
        gr.CrossSliding += gr_CrossSliding;
        gr.Dragging += gr_Dragging;
        gr.Holding += gr_Holding;
        gr.ManipulationCompleted += gr_ManipulationCompleted;
        gr.ManipulationInertiaStarting += gr_ManipulationInertiaStarting;
        gr.ManipulationStarted += gr_ManipulationStarted;
        gr.ManipulationUpdated += gr_ManipulationUpdated;
        gr.RightTapped += gr_RightTapped;
        gr.Tapped += gr_Tapped;
        gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |
        Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |
        Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap;

    }

    void gr_Tapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.TappedEventArgs args)
    {
        Debug.WriteLine("gr_Tapped");
    }
    void gr_RightTapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.RightTappedEventArgs args)
    {
        Debug.WriteLine("gr_RightTapped");
    }
    void gr_Holding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.HoldingEventArgs args)
    {
        Debug.WriteLine("gr_Holding");
    }
    void gr_Dragging(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.DraggingEventArgs args)
    {
        Debug.WriteLine("gr_Dragging");
    }
    void gr_CrossSliding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.CrossSlidingEventArgs args)
    {
        Debug.WriteLine("gr_CrossSliding");
    }
    void gr_ManipulationUpdated(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationUpdatedEventArgs args)
    {
        Debug.WriteLine("gr_ManipulationUpdated");
    }
    void gr_ManipulationStarted(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationStartedEventArgs args)
    {
        Debug.WriteLine("gr_ManipulationStarted");
    }
    void gr_ManipulationCompleted(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationCompletedEventArgs args)
    {
        Debug.WriteLine("gr_ManipulationCompleted");
    }
    void gr_ManipulationInertiaStarting(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationInertiaStartingEventArgs args)
    {
        Debug.WriteLine("gr_ManipulationInertiaStarting");
    }

解决方案

If you will notice the MainPage Class has its own Manipulation Events which you can use without creating a seperate GestureRecognizer. You can enable it by setting this.ManipulationMode to ManipulationModes.All. This will allow you to see responses on the MainPages Tapped, RightTapped, ManipulationStarting, ManipulationStarted, ManipulationDelta and ManipulationCompleted Events.

As far as getting the GestureRecongnizer to work according to this Blog and this MSDN Forum Posting you will need to handle the MainPage's PointerMoved, PointerReleased and PointerPressed events like so.

Windows.UI.Input.GestureRecognizer gr = new Windows.UI.Input.GestureRecognizer();  

public MainPage()
{
    this.InitializeComponent();
    this.PointerPressed += MainPage_PointerPressed;
    this.PointerMoved += MainPage_PointerMoved;
    this.PointerReleased += MainPage_PointerReleased;
    gr.CrossSliding += gr_CrossSliding;    
    gr.Dragging += gr_Dragging;    
    gr.Holding += gr_Holding;    
    gr.ManipulationCompleted += gr_ManipulationCompleted;    
    gr.ManipulationInertiaStarting += gr_ManipulationInertiaStarting;    
    gr.ManipulationStarted += gr_ManipulationStarted;    
    gr.ManipulationUpdated += gr_ManipulationUpdated;    
    gr.RightTapped += gr_RightTapped;    
    gr.Tapped += gr_Tapped;    
    gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |    
    Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |    
    Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap; 
}

void MainPage_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    var ps = e.GetIntermediatePoints(null);
    if (ps != null && ps.Count > 0)
    {
        gr.ProcessUpEvent(ps[0]);
        e.Handled = true;
        gr.CompleteGesture();
    }
}

void MainPage_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    gr.ProcessMoveEvents(e.GetIntermediatePoints(null));
    e.Handled = true;
}

void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    var ps = e.GetIntermediatePoints(null);
    if (ps != null && ps.Count > 0)
    {
        gr.ProcessDownEvent(ps[0]);
        e.Handled = true;
    }
}


According to the Documentation you need to enable the CrossSlide Event by adding it to your GestureRecongnizer and setting up the CrossSlideThresholds and Direction. From last link:

CrossSlide must be set in the GestureSettings property to support CrossSliding. CrossSliding distance thresholds are disabled by default. Use CrossSlideThresholds to set these values.

example:

Windows.UI.Input.CrossSlideThresholds cst = new Windows.UI.Input.CrossSlideThresholds();
cst.SelectionStart = 2;
cst.SpeedBumpStart = 3;
cst.SpeedBumpEnd = 4;
cst.RearrangeStart = 5;
gr.CrossSlideHorizontally = true;
gr.CrossSlideThresholds = cst;
gr.CrossSliding += gr_CrossSliding;

and make sure it is added to your GestureSettings

gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX |
                     Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia |
                     Windows.UI.Input.GestureSettings.ManipulationScaleInertia | Windows.UI.Input.GestureSettings.ManipulationTranslateInertia |
                     Windows.UI.Input.GestureSettings.Tap | Windows.UI.Input.GestureSettings.CrossSlide;

这篇关于检测简单的触摸手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆