2017-04-21 3 views

答えて

2

AndroidおよびiOSプラットフォーム用のレンダラの例がたくさんあります。しかし、WinRTやUWPのためではありません。

現在、Xamarin.Formsにはこのような「SwipeGestureRecognizer」APIはありません。しかし、あなたはカスタムSwipeGestureRecognizerPanGestureRecognizerをベースにすることができます。私は "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; 
     } 
    } 
+0

私はまだこのアプローチをテストすることができていない - しかし、私は本当にそれが好き。私は、ほとんどのUIコントロールにスワイプジェスチャーサポートを追加することができるかもしれませんが、UIレンダラーを記述する必要はありません。 – Ada

+0

私が見ることができる唯一の注意点は、スワイプが完了したときにのみイベントを検出して起動する機能ですが、スレッショルドとタイマーの周りを少し微調整すれば、それを管理できると思います。ありがとう:) – Ada

関連する問題