5

I use this code for rotation

void rotate()
{
    Duration Time_duration = new Duration(TimeSpan.FromSeconds(20));
    Storyboard MyStory = new Storyboard();
    MyStory.Duration = Time_duration;
    DoubleAnimation My_Double = new DoubleAnimation();
    My_Double.Duration = Time_duration;
    MyStory.Chil*emphasized text*dren.Add(My_Double);
    RotateTransform MyTransform = new RotateTransform();
    Storyboard.SetTarget(My_Double, MyTransform);
    Storyboard.SetTargetProperty(My_Double, new PropertyPath("Angle"));
    My_Double.To = 270;
    this.maincan.RenderTransform = MyTransform;
    this.maincan.RenderTransformOrigin = new Point(0.5, 0.5);
    //stackPanel1.Children.Add(image1);
    MyStory.Begin();
}

It's working, but i want rotate the image continuously.

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
selvam
  • 1,177
  • 4
  • 18
  • 40

2 Answers2

7

The Storyboard has a property called RepeatBehavior which allows you to control how the animation repeats. In your code, add the following line...

MyStory.RepeatBehavior = RepeatBehavior.Forever;  // repeat forever 

or

MyStory.RepeatBehavior = new RepeatBehavior(TimeSpan.FromSeconds(20)); // repeat for 20 seconds

There are more examples in the official documentation:

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline.repeatbehavior(v=vs.95).aspx

Neil Turner
  • 2,712
  • 2
  • 18
  • 37
  • Thank You Friend. It's working fine...... But I want rotate that image from that last angle not repeated......... please help me... – selvam Apr 03 '13 at 05:28
  • Make sure the start-angle and end-angle are the same ie. rotate the shape/image 360 degrees then repeat. – Neil Turner Apr 03 '13 at 14:36
4

This is just an idea, but if your only concern is to make the image continuously rotating, this maybe is the most simplest way to do it.

        void rotate()
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

So every second the event will be fired

 private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        Storyboard MyStory = new Storyboard();
        MyStory.Duration = new TimeSpan(0, 0, 1);
        DoubleAnimation My_Double = new DoubleAnimation();
        My_Double.Duration = new TimeSpan(0, 0, 1);
        MyStory.Children.Add(My_Double);
        RotateTransform MyTransform = new RotateTransform();
        Storyboard.SetTarget(My_Double, MyTransform);
        Storyboard.SetTargetProperty(My_Double, new PropertyPath("Angle"));
        My_Double.To = 360;
        YourImage.RenderTransform = MyTransform;
        YourImage.RenderTransformOrigin = new Point(0.5, 0.5);       
        MyStory.Begin();
    }

Let me know how it goes (:

EDIT

This is just an idea i am sure there are better ways

 void rotate(int i)
       {            
        Storyboard MyStory = new Storyboard();
        MyStory.Duration = new TimeSpan(0,0,1);           
        DoubleAnimation My_Double = new DoubleAnimation();
        My_Double.Duration =  new TimeSpan(0,0,1);          
        MyStory.Children.Add(My_Double);
        RotateTransform MyTransform = new RotateTransform();
        Storyboard.SetTarget(My_Double, MyTransform);
        Storyboard.SetTargetProperty(My_Double, new PropertyPath("Angle"));
        My_Double.From = i;
        My_Double.To = i +90;
        m_Image.RenderTransform = MyTransform;
        m_Image.RenderTransformOrigin = new Point(0.5, 0.5);            
        MyStory.Begin();
        MyStory.Completed +=((arg,c) =>
        {
            if (i == 360)
            {
                rotate(0);
            }
            else 
            {
                rotate(i + 90);
            }        
        });                                
    }
Swift Sharp
  • 2,604
  • 3
  • 30
  • 54
  • Thank You Friend.... It's Working fine..... But I want rotate that image from that last angle....... that means once click rotate that time take take angel is 90, and another time i click rotate that time it's rotate from the angle of 90 and then rotate what angle i give..... Please Help Me. – selvam Apr 03 '13 at 05:29
  • please take a look of my answer now – Swift Sharp Apr 03 '13 at 07:42
  • hai rado..... I have another problem...... first time i rotate the image 90 degree.. after i want click rotate that time rotate image another 90 deg simultaneously. – selvam Apr 09 '13 at 04:59
  • you have property `from` and `to` try this so you can easily change the angle for example `My_Double.From = p; My_Double.To = q;` than `if (p == 360){rotate(0,90);}else{rotate(p+180,q+180);}` this way every time you skip ninety degrees – Swift Sharp Apr 09 '13 at 06:47