Handle Swipe Up, Swipe Down, Swipe Left & Swipe Right Gestures in a WinRT app

别说谁变了你拦得住时间么 提交于 2019-11-30 00:26:55
Jawahar

Manipulation events provide you the translation values. Manipulation Delta will fire continuously until your manipulation completed along with inertia. In this event check whether the move is inertial, (a normal move shouldn't be considered as swipe) and detect the difference between initial and current position.

Once it reached the threshold, fire the swipe up/down/left/right event. And stop the manipulation immediately to avoid firing the same event again and again.

Following code will help you,

    private Point initialpoint;

    private void Grid_ManipulationStarted_1(object sender, ManipulationStartedRoutedEventArgs e)
    {
        initialpoint = e.Position;
    }

    private void Grid_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        if (e.IsInertial)
        {
            Point currentpoint = e.Position;
            if (currentpoint.X - initialpoint.X >= 500)//500 is the threshold value, where you want to trigger the swipe right event
            {
                System.Diagnostics.Debug.WriteLine("Swipe Right");
                e.Complete();
            }
        }
    }
drcoderz

I tried the answer by XAML lover, but it wasn't that accurate for me (IsIntertial always came back false for me). I implemented something different (I replied to a previous post of a related topic here Handling Swipe Guesture in Windows 8 Grid) for anyone who wanted to try something different.

Take a look at GestureRecognizer.CrossSliding event. There is also EdgeGesture class, and samples: EdgeGesture sample, gestures sample.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!