首页 > 解决方案 > Animation.commit 在 Xamarin Forms 中动画结束之前返回回调

问题描述

我正在为轮盘游戏制作动画,它是一个有 4 个扇区的轮盘。我正在使用动画类 y 动画扩展。但问题是,在animation.commit方法中,其中一个参数应该返回动画的结束,因为我需要在动画结束后做些别的。但问题是,动画使代码在完整动画结束之前完成。我究竟做错了什么?

这是我的代码:

'using FormsControls.Base;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

  namespace RouletteFish.Views
  {
       [XamlCompilation(XamlCompilationOptions.Compile)]
       public partial class RouletteGame : ContentPage
       {

    private bool _userTapped;
    private long lngDegrees = 0;
    private int degree = 0;
    private int degreeOld;
    private int finalDegree = 0;
    private int cantidadSectores = 4;
    private long alldegrees;
    private int half;
    private float gradePosition;


    public RouletteGame ()
    {
        InitializeComponent ();
        alldegrees = 360;
        cantidadSectores = 4;
        half = 2;
        gradePosition = 360 / 4 / 2;

    }

    private double width;
    private double height;


    private  void OnImageNameTapped(object sender, EventArgs args)
    {
        try
        {
            if (_userTapped)
                return;

            _userTapped = true;




            // Create parent animation object.
            Animation parentAnimation = new Animation();

            RotateRoulette();


            Animation rotation = new Animation(callback: d => imageRoulette.RotateTo(finalDegree * (360 / 6), 3600, Easing.Linear),
                                         start: degreeOld,
                                         end: finalDegree,
                                         easing: Easing.SpringOut);


            parentAnimation.Add(0, 1, rotation);


            // Commit parent animation
              parentAnimation.Commit(
                  this, "Animation1", 16, 250, null,
                  (v, c) => checkResult(v,c));


                   }
        catch (Exception ex)
        {
            throw ex;
        }
    }



    private  void RotateRoulette() {
        degreeOld = degree % 360;
        degree = new Random().Next(360) + 720;
        uint u = (uint)(int)new Random().Next(360) + 3600;
         finalDegree = degree;
        //imageRoulette.RotateTo(finalDegree * (360/6), 3600, Easing.Linear);

    }



    public void checkResult(double v, bool c) {
        Debug.WriteLine("parent finished: {0} {1}");
        DisplayAlert("Sector", getSector(360 - (finalDegree % 360)), "Ok");
        _userTapped = false;
    }

    private String getSector(int degrees)
    {
        int i = 0;
        String text = null;

        do
        {
            // start and end of each sector on the wheel
            float start = gradePosition * (i * 2 + 1);
            float end = gradePosition * (i * 2 + 3);

            if (degrees >= start && degrees < end)
            {
                // degrees is in [start;end[
                // so text is equals to sectors[i];
                text = prices[i];
            }

            i++;
            // now we can test our Android Roulette Game :)
            // That's all !
            // In the second part, you will learn how to add some bets on the table to play to the Roulette Game :)
            // Subscribe and stay tuned !

        } while (text == null && i < prices.Count());

        return text;
    }


}

  }'

标签: animationxamarincross-platform

解决方案


推荐阅读