首页 文章

用于Windows Phone(8.1和10)平台的Xamarin Forms中的滑动手势识别器

提问于
浏览
2

如何在Windows Phone(8.1和10)平台的Xamarin Forms中最好地实现滑动gesture recognizer

我看到了很多适用于Android和iOS平台的renderers示例 . 但不适用于WinRT或UWP .

1 回答

  • 3

    我看到很多渲染器的例子都适用于Android和iOS平台 . 但不适用于WinRT或UWP .

    目前,Xamarin.Forms没有这样的"SwipeGestureRecognizer" api . 但你可以根据PanGestureRecognizer自定义 SwipeGestureRecognizer . 我已经编写了以下用于模拟"SwipeGestureRecognizer"的代码 . 但我使用的阈值仅用于测试,而不是真实的阈值,您可以根据您的要求修改阈值 .

    public enum SwipeDeriction
    {
        Left = 0,
        Rigth,
        Above,
        Bottom
    }
    
    public class SwipeGestureReconginzer : PanGestureRecognizer
    {
        public delegate void SwipeRequedt(object sender, SwipeDerrictionEventArgs e);
    
        public event EventHandler<SwipeDerrictionEventArgs> Swiped;
    
        public SwipeGestureReconginzer()
        {
            this.PanUpdated += SwipeGestureReconginzer_PanUpdated;
        }
    
        private void SwipeGestureReconginzer_PanUpdated(object sender, PanUpdatedEventArgs e)
        {
            if (e.TotalY > -5 | e.TotalY < 5)
            {
                if (e.TotalX > 10)
                {
                    Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Rigth));
                }
                if (e.TotalX < -10)
                {
                    Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Left));
                }
            }
    
            if (e.TotalX > -5 | e.TotalX < 5)
            {
                if (e.TotalY > 10)
                {
                    Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Bottom));
                }
                if (e.TotalY < -10)
                {
                    Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Above));
                }
            }
        }
    }
    
    public class SwipeDerrictionEventArgs : EventArgs
    {
        public SwipeDeriction Deriction { get; }
    
        public SwipeDerrictionEventArgs(SwipeDeriction deriction)
        {
            Deriction = deriction;
        }
    }
    

    MainPage.xaml.cs

    var swipe = new SwipeGestureReconginzer();
      swipe.Swiped += Tee_Swiped;
      TestLabel.GestureRecognizers.Add(swipe);
    
      private void Tee_Swiped(object sender, SwipeDerrictionEventArgs e)
      {
          switch (e.Deriction)
          {
              case SwipeDeriction.Above:
                  {
                  }
                  break;
    
              case SwipeDeriction.Left:
                  {
                  }
                  break;
    
              case SwipeDeriction.Rigth:
                  {
                  }
                  break;
    
              case SwipeDeriction.Bottom:
                  {
                  }
                  break;
    
              default:
                  break;
          }
      }
    

相关问题