如何获得发生的每个 pann 手势

How to get every pann gesture that happens

我有一个非常简单的问题,但找不到任何答案。

我有一个应用程序,我希望在每次 Pan/Swipe/Slide 手势发生时调用一个函数。

我知道布局是如何组织的,所以这意味着我不能在主布局上添加手势,但与此同时,将这个手势添加到每个元素似乎令人毛骨悚然...

有什么想法吗?

感谢

制作自定义页面呈现器并在其中添加手势。

这是 iOS

上的示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Foundation;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using Knok;
using Knok.iOS;

[assembly: ExportRenderer(typeof(SearchEventsPage), typeof(SearchEventsPageRenderer))]

namespace Knok.iOS
{
    class SearchEventsPageRenderer : PageRenderer
    {
        UISwipeGestureRecognizer swipeLeftGestureRecognizer, swipeRightGestureRecognizer;

        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null) 
            {
                if (swipeLeftGestureRecognizer != null)
                    NativeView.RemoveGestureRecognizer(swipeLeftGestureRecognizer);
                if (swipeRightGestureRecognizer != null)
                    NativeView.RemoveGestureRecognizer(swipeRightGestureRecognizer);
            }

            if (e.NewElement != null)
            {

                swipeLeftGestureRecognizer = new UISwipeGestureRecognizer(_ => 
                    {
                        ((SearchEventsPage)Element).OnSwipeLeft();
                    })
                {
                    Direction = UISwipeGestureRecognizerDirection.Left
                };
                NativeView.AddGestureRecognizer(swipeLeftGestureRecognizer);

                swipeRightGestureRecognizer = new UISwipeGestureRecognizer(() =>
                    {
                        ((SearchEventsPage)Element).OnSwipeRight();
                    })
                {
                    Direction = UISwipeGestureRecognizerDirection.Right
                };
                NativeView.AddGestureRecognizer(swipeRightGestureRecognizer);
            }
        }


    }
}

其中 OnSwipeLeft 和 OnSwipeRight 在 SearchEventsPage 中定义为

public async void OnSwipeLeft()
{
}

我发现了一些还不错的东西,它还可以引发事件或调用给定的 Action<>

public class PanContainer : ContentView
{
    private double x, y;

    public PanContainer()
    {
        var panGesture = new PanGestureRecognizer();
        panGesture.PanUpdated += OnPanUpdated;
        GestureRecognizers.Add(panGesture);
        BackgroundColor = Color.Transparent;
    }

    private double valueX;
    private double valueY;

    private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
    {
        if (e.TotalX == 0 && e.TotalX != valueX)
            Debug.WriteLine("The X pan value is [{0}]", valueX);
        valueX = e.TotalX;
        if (e.TotalY == 0 && e.TotalY != valueX)
            Debug.WriteLine("The Y pan value is [{0}]", valueY);
        valueY = e.TotalY;
    }
}

喜欢就用

<ContentPage.Content>
    <AbsoluteLayout BackgroundColor="Black">
        <component:PanContainer AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1" AbsoluteLayout.LayoutFlags="All">

        </component:PanContainer>
    </AbsoluteLayout>
</ContentPage.Content>

并且不要忘记包含 xmlns:component="clr-namespace:NAMESPACE.Sources.Components;assembly=NightLine"


如果您想引发事件或调用函数,请从 private void OnPanUpdated(object sender, PanUpdatedEventArgs e) { .. }

开始