1
如何在Xamarin Forms for Windows Phone(8.1 & 10)平臺中最佳實現刷卡gesture recognizer?在Xamarin Forms中爲Windows Phone(8.1和10)平臺刷卡手勢識別器
我在renderers的Android和iOS平臺上看到很多例子。但不適用於WinRT或UWP。
如何在Xamarin Forms for Windows Phone(8.1 & 10)平臺中最佳實現刷卡gesture recognizer?在Xamarin Forms中爲Windows Phone(8.1和10)平臺刷卡手勢識別器
我在renderers的Android和iOS平臺上看到很多例子。但不適用於WinRT或UWP。
我看到很多適用於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;
}
}
我沒能還測試這種方法 - 但我真的很喜歡它。我可能可以爲大多數UI控件添加滑動手勢支持,而無需爲它們編寫平臺渲染器。 – Ada
我能看到的惟一警告是隻有在滑動完成時才能檢測和觸發事件 - 但是在閾值和計時器周圍進行輕微調整後,我認爲我將能夠管理它。謝謝 :) – Ada