开发者

In WPF how do I add easing function to my animation from code behind?

开发者 https://www.devze.com 2023-01-16 11:58 出处:网络
I am creating doubleAniation in code and I want to add an easing 开发者_高级运维function to it, so how do I do it?It\'s not necessary to use DoubleAnimationUsingKeyFrames - it can be done with just Do

I am creating doubleAniation in code and I want to add an easing 开发者_高级运维function to it, so how do I do it?


It's not necessary to use DoubleAnimationUsingKeyFrames - it can be done with just DoubleAnimation:

CircleEase easing = new CircleEase();  // or whatever easing class you want
easing.EasingMode = EasingMode.EaseInOut;
DoubleAnimation scrollQueue = new DoubleAnimation();
scrollQueue.By = -singleScrollAmt;
scrollQueue.EasingFunction = easing;
scrollQueue.Duration = TimeSpan.FromSeconds(0.5);
MyTextBlock.BeginAnimation(Canvas.TopProperty, scrollQueue);


Here's how I do it:

        DoubleAnimationUsingKeyFrames compassRoseAnimation = new DoubleAnimationUsingKeyFrames();
        compassRoseAnimation.Duration = new Duration(TimeSpan.FromSeconds(2));
        QuarticEase easingFunction = new QuarticEase();
        easingFunction.EasingMode = EasingMode.EaseInOut;
        EasingDoubleKeyFrame startAnimation = new EasingDoubleKeyFrame(previousRotationDegrees, KeyTime.FromPercent(0));
        EasingDoubleKeyFrame endAnimation = new EasingDoubleKeyFrame(newRotationDegrees, KeyTime.FromPercent(1.0), easingFunction);
        compassRoseAnimation.KeyFrames.Add(startAnimation);
        compassRoseAnimation.KeyFrames.Add(endAnimation);

        RotateTransform rotateTransform = new RotateTransform();
        CompassWithNumbersControl.RenderTransform = rotateTransform;
        rotateTransform.BeginAnimation(RotateTransform.AngleProperty, compassRoseAnimation);


I have figure out it myself. I was looking for Easing property, but in fact it is called KeySpline and I have to use DoubleAniamtionUsingKeyFrames instead, to get easing functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消