I am creating doubleAniation in code and I want to add an easing function to it, so how do I do it?
Asked
Active
Viewed 9,224 times
3 Answers
10
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);

Conrad
- 2,197
- 28
- 53
5
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);

DefenestrationDay
- 3,712
- 2
- 33
- 61
-
Um, correct me if I'm wrong, but is there a typo in your code? You define, then set the EasingMode on your QuadraticEase, but it doesn't actually look like you *use* it. Where do you actually apply it to your animation(s)? – Mark A. Donohoe Aug 02 '18 at 06:15
-
@MarqueIV: Line 6 – DefenestrationDay Oct 21 '18 at 21:37
-
I am really confused on how the he** I missed that! I think I was looking at it on a smaller screen and that part was clipped off and I didn’t notice. Ignore my comment! :-) – Mark A. Donohoe Oct 21 '18 at 23:20
-1
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.

Vitalij
- 4,587
- 9
- 42
- 65
-
3You don't have to use keyframes at all - there were two perfectly valid answers which you failed to give credit to and instead answered your own question with a BAD solution. Very poor community interplay on your part. The original question was also poorly worded, poorly defined/constrained, and did not have any example code demonstrating your issue. – tpartee Jul 28 '14 at 23:21