Detect Simple Touch Gestures

后端 未结 1 1932
广开言路
广开言路 2020-12-05 10:44

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:

    pub         


        
相关标签:
1条回答
  • 2020-12-05 11:07

    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;
    
    0 讨论(0)
提交回复
热议问题