0

I have downloaded the windows phone toolkit from codeplex that enables the phone to receive gesture support. In my application, I have an ellipse. I want to create a method that will rotate the ellipse Counter clockwise when the screen receives upward vertical gesture & clockwise when it receives downward vertical gesture. I've spent a lot of time on google but with poor results, how can I achieve this operation?

Siddharth Thevaril
  • 3,722
  • 3
  • 35
  • 71
  • Apply a RotateTransform to the Ellipse, detect the vertical gesture by using the ManipulationDelta event of the page, and change the angle of the RotateTransform accordingly – Kevin Gosse Jul 28 '13 at 12:18
  • I can write the RotateTransform to Ellipse but I am unable to come up with the logic to detect vertical gesture, can you give an example? – Siddharth Thevaril Jul 28 '13 at 12:51

1 Answers1

1

First, assign a RotateTransform to the control you want to rotate. Here I'm using a rectangle because it's easier to see it rotate, but it works just as well with an ellipse:

<Rectangle Fill="Blue" Height="50" Width="50">
    <Rectangle.RenderTransform>
        <RotateTransform x:Name="RotateTransform" />
    </Rectangle.RenderTransform>
</Rectangle>

Then, subscribe to the ManipulationDelta event of your page:

<phone:PhoneApplicationPage 
    x:Class="..."
    various stuff
    ManipulationDelta="PhoneApplicationPage_ManipulationDelta">

In the event handler, use e.DeltaManipulation.Translation to know how much the finger has moved, on the X and Y axis. Then, rotate your shape accordingly:

private void PhoneApplicationPage_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    this.RotateTransform.Angle += e.DeltaManipulation.Translation.Y;
}

If you want your shape to rotate at a different speed, then multiply e.DeltaManipulation.Translation.Y by a constant (> 1 to accelerate the rotation, < 1 to slow it down). Also, if you don't want the shape to rotate when the finger is moving diagonally, check the value of e.DeltaManipulation.Translation.X and apply the rotation only if it's way smaller than e.DeltaManipulation.Translation.Y (you can't just check if it's equal to 0, since the finger will always move slightly to the left or to the right when doing a vertical gesture).

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • The code works fine when I rotate my finger in either direction, but when I make a vertical gesture in up/down direction starting from the ellipse, the ellipse rotates up to 90 degrees and then stops, why is it happening? – Siddharth Thevaril Jul 28 '13 at 17:44
  • @Sidsec9 Indeed. To be honest, I don't know how to explain this behavior. You can prevent it by setting `IsHitTestVisible="False"` on the ellipse, but that's a dirty fix. – Kevin Gosse Jul 28 '13 at 18:45
  • I came up with a new solution. I copied the ellipse & pasted it over the old ellipse. The new ellipse has Opacity = 0 and your code is applied to the new ellipse to rotate the old ellipse. This works fine! – Siddharth Thevaril Jul 29 '13 at 19:44