检测简单触摸手势

Elm*_*lmo 21 c# vb.net xaml windows-8 windows-runtime

任何人都可以解释如何在WinRT应用程序中检测简单的触摸手势?我尝试使用GestureRecognizer该类,但它不起作用:

    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");
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*all 37

如果您注意到MainPage类有自己的操作事件,您可以使用它而无需创建单独的GestureRecognizer.您可以通过设置this.ManipulationMode为 启用它ManipulationModes.All.这将让你看到的MainPages回应Tapped,RightTapped,ManipulationStarting,ManipulationStarted,ManipulationDeltaManipulationCompleted事件.

至于让GestureRecongnizer根据这个博客和这个MSDN论坛发布工作,你将需要处理MainPage PointerMoved,PointerReleased以及PointerPressed类似的事件.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

根据文档,您需要通过将CrossSlide事件添加到GestureRecongnizer并设置CrossSlideThresholds和Direction 来启用它.从上一个链接:

必须在GestureSettings属性中设置CrossSlide才能支持CrossSliding.默认情况下禁用CrossSliding distance阈值.使用CrossSlideThresholds设置这些值.

例:

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;
Run Code Online (Sandbox Code Playgroud)

并确保它已添加到您的 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;
Run Code Online (Sandbox Code Playgroud)