Silverlight Circular Motion Animation
In this sample I demonstrate how to take a series of UserControls (Simple ellipses) and rotate each in a counter-clockwise circular motion.
The first step in the process is to define a user control and expose both the TranslateTransform and RotateTransform from the TransformGroup object. This allows us to rotate and move each user control on a specified tick (DispatcherTimer tick). The timer is controlled by the main page.xaml.
In this example, I start with a single “ball”, begin rotating it, and every second add an additional ball to the animation. Instead of having each user control have its own animation, I have chosen to use a single DispatcherTimer that iterates over a generic collection of UserControls, to generate the circular motion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | void Page_Loaded(object sender, RoutedEventArgs e)
{
_centerX = this.Width/2 |
In this handler I determine the center of my circle, place the first control, and define two DispatcherTimers, one for the animation, and one for inserting additional controls into the main Grid. Since the speed of rotation is consistent for each “ball”, I am able to use a single timer which iterates over a generic collection of UserControls. Each time the Animation timer fires, I will call the following method.
1 2 3 4 5 6 7 8 9 10 11 12 | void _timer_Tick(object sender, EventArgs e)
{
foreach(KeyValuePair<string ,BALL> ball in _balls)
{
ball.Value.Rotate.Angle -= 5 |
The math in this method is pretty basic, on each tick I rotate the ball between 1 and 360 degrees. The angle I am looking for is the remainder of the angle of my rotation from 360 degrees. To calculate this, I apply the modulus assignment operator to my new angle. To determine the appropriate X and Y position I transform my angle into Radians and apply a bit of trigonometry to calculate X/Y based on the sin and cosine of the radian. Thanks again to Paul Ortchanian flash site, reflektions for a Flash sample I could translate into Silverlight.
Code: CircularMotion.zip


1 Comment so far >>
Why would you go to the trouble to use a timer and translate the book position manually.
This is an ideal case to use animations and leave the framerate and position determination to the runtime.
Just to a RotateTransform of 360 degrees
By faz on 05.14.08 3:02 am
Leave a comment