Horizontal swipe gesture on UWP

笑着哭i 提交于 2019-12-06 11:39:13

Actually you don't need to handle ManipulationStarted in this case and you don't need the initialPoint property. Assuming you have already defined your ScrollViewer's ManipulationMode to the following

ManipulationMode="TranslateX,TranslateInertia,System"

Then you simply use e.Cumulative.Translation.X to tell how long you have swiped in total

private void scrollview_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
    if (e.IsInertial)
    {
        var swipedDistance = e.Cumulative.Translation.X;

        if (Math.Abs(swipedDistance) <= 2) return;

        if (swipedDistance > 0)
        {
            i++;
        }
        else
        {
            i--;
        }

        txtBox1.Text = i.ToString();
    }
}

Update

Now that I understand your question better, I think you should handle gesture manipulation on the TextBox itself. If you want instant feedback, simply subscribe to the ManipulationDelta event and create a flag to only run the swipe logic once per touch.

private bool _isSwiped;
private void txtBox1_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    if (e.IsInertial && !_isSwiped)
    {
        var swipedDistance = e.Cumulative.Translation.X;

        if (Math.Abs(swipedDistance) <= 2) return;

        if (swipedDistance > 0)
        {
            i++;
        }
        else
        {
            i--;
        }

        txtBox1.Text = i.ToString();

        _isSwiped = true;
    }
}

private void txtBox1_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
    _isSwiped = false;
}

Make sure you move all the handlers and set the ManipulationMode onto the TextBox.

<TextBox x:Name="txtBox1"
         ManipulationMode="TranslateX,TranslateInertia,System" 
         ManipulationDelta="txtBox1_ManipulationDelta" 
         ManipulationCompleted="txtBox1_ManipulationCompleted" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!