UWP 上的水平滑动手势 [英] Horizontal swipe gesture on UWP

查看:34
本文介绍了UWP 上的水平滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 wptoolkit 获得与 Windows Phone 8 相同的滑动手势.因为 wptoolkit nuget 包不适用于 uwp,所以我无法在 UWP 上获得类似的滑动手势在 Windows Phone 8 中借助 WPtoolkit nugetget 包我把这个

is there any way to get swipe gesture same as windows phone 8 with wptoolkit. Because wptoolkit nuget package is not available for uwp, so i am unable to get similar swipe gesture on UWP In windows Phone 8 with the help of WPtoolkit nugetget package i placed this

<toolkit:GestureService.GestureListener>
         <toolkit:GestureListener Flick="OnSwipe"/>
</toolkit:GestureService.GestureListener>

在文本块上,所以我可以在textbox1上从左向右或从右向左滑动.和滑动手势帮助我实现这一点

over text block, so i can swipe left to right or right to left over textbox1. and swipe gesture help me to implement this

private static int i;
private void OnSwipe(object sender, FlickGestureEventArgs e)
    {
        if (e.HorizontalVelocity < 0)
        {
              i++;
              txtBox1.Text = i.ToString();             
        }

        if (e.HorizontalVelocity > 0)
        {
             i--;
             txtBox1.Text = i.ToString();
        }
    }

我在 uwp 上尝试了使用 scrollViewer 的 Manupulation 方法,但它不断增加值,直到它滚动查看器停止

i tried Manupulation method with scrollViewer on uwp but it continuously increase the value untill it scroll viewer stopped

这是代码

    private static int i;
    private Point initialpoint;
    private void scrollview_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
    {
        initialpoint = e.Position;
    }

    private void scrollview_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        if (e.IsInertial)
        {
            Point currentpoint = e.Position;
            if (currentpoint.X - initialpoint.X >= 2)
            {
                i++;
                txtBox1.Text = i.ToString();
            }

            if (currentpoint.Y - initialpoint.Y >= 2)
            {
                i--;
                txtBox1.Text = i.ToString();
            }
        }
    }

实现相同功能的任何其他方式.

Any other way to implement same functionality.

推荐答案

其实这种情况下你不需要处理ManipulationStarted,也不需要initialPoint> 财产.假设您已经将 ScrollViewerManipulationMode 定义为以下内容

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"

然后您只需使用 e.Cumulative.Translation.X 来告诉您总共刷了多长时间

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();
    }
}

<小时>

更新

既然我更好地理解了您的问题,我认为您应该处理 TextBox 本身的手势操作.如果您想要即时反馈,只需订阅 ManipulationDelta 事件并创建一个标志,以便每次触摸仅运行一次滑动逻辑.


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;
}

确保移动所有处理程序并将 ManipulationMode 设置到 TextBox 上.

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" />

这篇关于UWP 上的水平滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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