WPF - Creating a custom doubleanimation - DegreesAnimation

93 Views Asked by At

I hope that I will be able to explain my problem easily.

I have a pie chart look-a-like control that contains "pie pieces"

On top of that pie chart control, I have a pie piece that is used as a selector for the user, he can click on a not-selected pie piece to move the selector to his new position (and have other options to do so, but that’s not relevant for my question)

When he clicks on a new pie piece, I'm calculating the Degrees Angle (a property of type double that I'm using to draw the pie piece) and it works great,

Now I wanted to add a cool animation when the selector moves, and I've used the DoubleAnimation to do so, but I have problem,

Let’s say that the current selected pie piece is at 30 degrees and the next selected pie piece is at 300 degrees, the animation will go a 270 degrees round instead of "going back" from 30 to 0 and then from 360 to 270, making a shorter spin...

Same when it's the opposite way, if the current selected piece is at 300 and the next is at 30 degrees, it should go to 360, and then 0 to 30.

The reason that it's doing so, is obvious to me. It’s a double animation, not a degrees animation,

This is why I want to create a custom animation that will do this,

I've tried to do so, but I couldn't understand how the custom animation works,

I just couldn't understand how the GetCurrentValueCore works.

Can anyone assist me with this issue?

Thanks a lot.

UPDATE:

Currently , I'm using this solution:

var selector = ...../

var from = fromRotationAngle;

                    var to = toRotationAngle;

                    if(Math.Abs(from - to) > 180)
                    {
                        var distanceFromZero = Math.Abs(0 - from);
                        var distanceFromEnd = Math.Abs(360 - from);


                        var anim = new DoubleAnimation
                        {
                            From = from,
                            To = distanceFromEnd < distanceFromZero ? 360 : 0,
                            SpeedRatio = 2,

                        };


                        var secondAnim = new DoubleAnimation
                        {
                            From = distanceFromEnd < distanceFromZero ? 0 : 360,
                            To = to,
                            SpeedRatio = 2
                        };

                        anim.Completed += (s, e) =>
                        {
                            selector.BeginAnimation(PiePiece.RotationAngleProperty, secondAnim);
                        };

                        selector.BeginAnimation(PiePiece.RotationAngleProperty, anim);
                    }
                    else
                    {
                        var anim = new DoubleAnimation
                        {
                            From = from,
                            To = to,
                            SpeedRatio = 2,
                        };

                        selector.BeginAnimation(PiePiece.RotationAngleProperty, anim); 
                    }

, but it's ugly, I want to use one Custom Animation to perform this action... anyone?

0

There are 0 best solutions below