我不想破坏一篇旧帖子,所以我正在为此创建一个新帖子。我有一个集合视图,两侧都有两个按钮来增加或减少计数,这些按钮可以工作,但是它们很难触发,因为我的左右滑动视图项目在尝试点击按钮时不断触发。每尝试点击按钮 3-5 次就会发生这种情况,有没有办法调整在 swipeview 激活之前水平滑动的距离?
我也遇到了像你一样的类似问题,并且有解决方法。由于滑动视图太敏感,如果拇指移动一点点,设备很容易被视为滑动而不是点击。
它可以使用 OnSwipeChanging 事件和 OnSwipeEnded 事件以及公共属性 Offset 来测量滑动的移动距离。每当调用 OnSwipeChanging 事件时,Offset 的值就会不断更新。当用户停止滑动时,OnSwipeEnded 会获取 Offset 的值,并在 Offset 小于特定值时调用点击操作。
XAML
<SwipeView SwipeChanging="OnSwipeChanging" SwipeEnded="OnSwipeEnded">
<SwipeView.LeftItems>
<SwipeItemView>
<Grid WidthRequest="100" />
</SwipeItemView>
</SwipeView.LeftItems>
<SwipeView.RightItems>
<SwipeItemView>
<Grid WidthRequest="100" />
</SwipeItemView>
</SwipeView.RightItems>
<Frame HeightRequest="50">
<Frame.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnTapped" />
</Frame.GestureRecognizers>
</Frame>
</SwipeView>
C#
public partial class TestView : ContentPage
{
public double OffSet { get; set; } = 0
public TestView()
{
InitializeComponent();
}
public void OnSwipeChanging(object sender, SwipeChangingEventArgs args)
{
OffSet = args.Offset;
var swipeView = (Microsoft.Maui.Controls.SwipeView)sender;
if (args.SwipeDirection == SwipeDirection.Left && offSet < -50)
{
//swipe left action here
}
else if (args.SwipeDirection == SwipeDirection.Right && offSet > 50)
{
//swipe right action here
}
}
public void OnSwipeEnded(object sender, SwipeEndedEventArgs args)
{
var swipeView = (SwipeView)sender;
if (offSet < 5 && offSet > -5)
{
//click button action
OnTapped();
}
swipeView.Close();
}
}