在 WPF 中,如何从代码后面向动画添加缓动功能?

发布于 2024-09-19 16:40:45 字数 52 浏览 2 评论 0原文

我正在代码中创建 doubleAniation,并且想为其添加缓动函数,那么我该怎么做呢?

I am creating doubleAniation in code and I want to add an easing function to it, so how do I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听闻余生 2024-09-26 16:40:45

没有必要使用 DoubleAnimationUsingKeyFrames - 只需使用 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);

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);
怀念你的温柔 2024-09-26 16:40:45

我是这样做的:

        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);

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);
硪扪都還晓 2024-09-26 16:40:45

我自己已经弄清楚了。我一直在寻找 Easing 属性,但实际上它被称为 KeySpline,我必须使用 DoubleAniamtionUsingKeyFrames 来代替,以获得缓动功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文