Xamarin形成TapGesture动画问题

时间:2018-10-15 10:39:25

标签: c# animation xamarin xamarin.forms xamarin.ios

当我用动画单击第一个堆栈布局时,我想显示第二个堆栈布局。第一次尝试后效果很好。

我的问题是动画在第一次尝试中不起作用。

代码-

private TapGestureRecognizer tg;
    public MainPage()
    {
        InitializeComponent();
        answerStack.IsVisible = false;

        tg = new TapGestureRecognizer();
        tg.Tapped += Tg_Tapped;
        questionStack.GestureRecognizers.Add(tg);
    }

    public Animation animate;

    void Tg_Tapped(object sender, EventArgs e)
    {
        var answerHeight = answerStack.Height;

        answerStack.IsVisible = !answerStack.IsVisible;
        questionStack.GestureRecognizers.Remove(tg);

        animate = new Animation(d => answerStack.HeightRequest = d, 0, answerHeight);

        animate.Commit(answerStack, "a", 5, 450, Easing.CubicIn, (data, x) => {
            Device.BeginInvokeOnMainThread(() => {
                answerStack.HeightRequest = answerHeight;
                questionStack.GestureRecognizers.Add(tg);
            });
        }, null);
    }

GitHub链接-https://github.com/manieshavirt/XamarinAccordion

问题的原因是,在第一个动画过程中,“ answerHeight”设置为-1。在随后的动画中,“ answerHeight”是从答案内容的高度派生的,因此有一个平滑的动画。如果在第一个动画中手动设置“ answerHeight”的值,则动画过渡会显得平滑。但是,我不想手动设置一个值,相反,我需要在第一个动画期间从答案内容的高度导出该值。

我该如何实现?

0 个答案:

没有答案