DoubleAnimation with Step
For having step working in double animation use
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
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)
1 2 3 4 |
<DoubleAnimationStep Duration="0:0:2" Step="14" From="0" To="360"/> |
Categories: Non classé
Commentaires récents