WPF rotate an loading image for gif effect

前端 未结 2 1628
感动是毒
感动是毒 2020-12-22 12:28

I have a static loader image in wpf, I can easily use it as a loading gif by using WPFAnimatedGIF nuget package, but it seems like an overkill.

There is only one sce

2条回答
  •  我在风中等你
    2020-12-22 12:59

    This Style animates the Angle of a RotateTransform in 30 degree steps when the Image element is visible.

    
    
    ...
    
    

    In order to avoid using an animation with many DiscreteDoubleKeyFrames, you may derive from DoubleAnimation and add a Step property:

    public class DoubleAnimationWithSteps : DoubleAnimation
    {
        public static readonly DependencyProperty StepProperty = DependencyProperty.Register(
            nameof(Step), typeof(double), typeof(DoubleAnimationWithSteps));
    
        public double Step
        {
            get { return (double)GetValue(StepProperty); }
            set { SetValue(StepProperty, value); }
        }
    
        protected override double GetCurrentValueCore(
            double from, double to, AnimationClock animationClock)
        {
            var value = base.GetCurrentValueCore(from, to, animationClock);
    
            if (Step > 0d)
            {
                value = Step * Math.Floor(value / Step);
            }
    
            return value;
        }
    
        protected override Freezable CreateInstanceCore()
        {
            return new DoubleAnimationWithSteps();
        }
    }
    

    You would use it like this:

    
        
    
    

提交回复
热议问题