I love WPF

"jamais sans son interface"

I love WPF

"jamais sans son interface"

DoubleAnimation with Step

For having step working in double animation use

public class DoubleAnimationStep : DoubleAnimationUsingKeyFrames
    {
        #region static
        static DoubleAnimationStep()
        {
            DurationProperty.OverrideMetadata(typeof(DoubleAnimationStep),
                new FrameworkPropertyMetadata(new Duration(), (ss,ee) => (ss as DoubleAnimationStep).Update()));
        }
        #endregion

        #region Step (DP SHORT)
        public int Step { get { return (int)GetValue(StepProperty); } set { SetValue(StepProperty, value); } }
        public static readonly DependencyProperty StepProperty =
            DependencyProperty.Register("Step",
                typeof(int),
                typeof(DoubleAnimationStep),
                new FrameworkPropertyMetadata(0, (ss, ee) => (ss as DoubleAnimationStep).Update()));
        #endregion

        #region From (DP SHORT)
        public double From { get { return (double)GetValue(FromProperty); } set { SetValue(FromProperty, value); } }
        public static readonly DependencyProperty FromProperty = DependencyProperty.Register(
            "From", 
            typeof(double), 
            typeof(DoubleAnimationStep),
            new FrameworkPropertyMetadata(0.0, (ss, ee) => (ss as DoubleAnimationStep).Update()));
        #endregion

        #region To (DP SHORT)
        public double To { get { return (double)GetValue(ToProperty); } set { SetValue(ToProperty, value); } }
        public static readonly DependencyProperty ToProperty = DependencyProperty.Register(
            "To", 
            typeof(double), 
            typeof(DoubleAnimationStep),
            new FrameworkPropertyMetadata(0.0, (ss, ee) => (ss as DoubleAnimationStep).Update()));
        #endregion 

        #region Update
        private void Update()
        {
            this.KeyFrames.Clear();
            if (this.Duration.TimeSpan.TotalMilliseconds == 0 || 
                this.Step == 0 ||
                this.From.Equals(this.To) ||
                this.To < this.From
                ) return;

            double totalDuration = this.Duration.TimeSpan.TotalMilliseconds;
            double stepDuration = totalDuration / (this.Step);
            double from = this.From;
            double to = this.To;
            double delta = to - from;
            double stepValue = (double)delta / this.Step;
            for (double i = 0, td = 0, sv = 0; i <= this.Step; i++, td += stepDuration, sv += stepValue)
            {
                TimeSpan ts = TimeSpan.FromMilliseconds(td);
                this.KeyFrames.Add(new DiscreteDoubleKeyFrame(
                    sv + from,
                    KeyTime.FromTimeSpan(ts)));
                //Console.WriteLine($"time {ts} = {sv}");
            }           
        }
        #endregion
    }

like this (for rotation)

 <DoubleAnimationStep Duration="0:0:2"
                      Step="14"
                      From="0" 
                      To="360"/>

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut