按住Xamarin GestureRecognizer

时间:2019-07-11 14:52:01

标签: c# xaml xamarin

我有一个带有点击手势识别器的框架手势识别器。每当我单击框架并释放时,都会发生某些事情。但是,我希望在按住框架时也会发生一些事情。

我想在每次按住框架时都将其缩小(例如单击动画),并且希望在释放时将其缩小。使用点击手势识别器,似乎只有在手指松开框架时才能做些什么。

此刻我正在使用此代码:

XAML

<Frame.GestureRecognizers>
       <TapGestureRecognizer Tapped="Voeding_Tapped" />
</Frame.GestureRecognizers>

C#

void Info_Clicked(object sender, EventArgs e)
{
    frame.ScaleTo(0.9, 100);
}

使用此代码,每当我单击并释放框架时,框架就会缩小。我该如何做才能在单击框架时缩小它,而在释放框架时又缩小呢?

1 个答案:

答案 0 :(得分:0)

以下是不使用自定义手势的示例方式,仅使用Composite Animations,如下所示:

C#

async void Info_Clicked(object sender, EventArgs e)
{
    await frame.ScaleTo(0.9, 2000);
    await frame.ScaleTo(1, 2000);
}

=================================更新============= ======================

如果需要长按手势,请创建自定义框架

public class FrameWithLongPress : Frame
{
    public EventHandler LongPressActivated;

    public void HandleLongPress(object sender, EventArgs e)
    {
        //Handle LongPressActivated Event
    }
}

Android ,自定义FrameRenderer(LongPressGestureRecognizerFrameRenderer):

[assembly: ExportRenderer(typeof(FrameWithLongPress), typeof(LongPressGestureRecognizerFrameRenderer))]
// FrameWithLongPress is created custom frame name in forms, LongPressGestureRecognizerFrameRenderer is renderer name
public class LongPressGestureRecognizerFrameRenderer : FrameRenderer
{
    FrameWithLongPress view;

    [Obsolete]
    public LongPressGestureRecognizerFrameRenderer()
    {
        this.LongClick += async (sender, args) => {
            Toast.MakeText(this.Context, "Long press is activated.", ToastLength.Short).Show();
            await view.ScaleTo(0.9, 2000);
            await view.ScaleTo(1, 2000);
        };
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            view = e.NewElement as FrameWithLongPress;
        }
    }
}

IOS ,LongPressGestureRecognizerFrameRenderer:

[assembly: ExportRenderer(typeof(FrameWithLongPress), typeof(LongPressGestureRecognizerFrameRenderer))]
// FrameWithLongPress is created custom frame name in forms, LongPressGestureRecognizerFrameRenderer is renderer name
public class LongPressGestureRecognizerFrameRenderer : FrameRenderer
{
    FrameWithLongPress view;
    public LongPressGestureRecognizerFrameRenderer()
    {
        UILongPressGestureRecognizer uiLongPressGesture = new UILongPressGestureRecognizer(new Action(async () => {
            await view.ScaleTo(0.9, 2000);
            await view.ScaleTo(1, 2000);
        }));
        this.AddGestureRecognizer(uiLongPressGesture);
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            view = e.NewElement as FrameWithLongPress;
        }
    }
}

Xaml 使用自定义框架:

<local:FrameWithLongPress WidthRequest="300" HeightRequest="100" BackgroundColor="BlueViolet"/>