#742 – Using Touch Manipulation Events to Rotate an Element
January 25, 2013 9 Comments
In addition to using the touch manipulation events to handle translation of an element, we can use the same mechanisms to allow a user to rotate an element using touch.
We can do both translation and rotation in the same event handler. The ManipulationDelta object gives us both a translation (vector) and a rotation (angle). Both are automatically incorporated into the ManipulationDelta object, based on how the user is touching the screen. The user can translate by sliding one finger around and can rotate by placing two fingers on the object and rotating it.
We transform the element by calling two different functions of the underlying Matrix, for both translation and rotation.
Here’s the XAML, containing a single Image control that we’ll interact with.
<Canvas Name="canvMain" Background="Transparent"> <Image Source="JamesII.jpg" Width="100" IsManipulationEnabled="True" RenderTransform="{Binding ImageTransform}" ManipulationStarting="Image_ManipulationStarting" ManipulationDelta="Image_ManipulationDelta"/> </Canvas>
Here is the source code, with the updated ManipulationDelta event handler.
public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; ImageTransform = new MatrixTransform(); } private MatrixTransform imageTransform; public MatrixTransform ImageTransform { get { return imageTransform; } set { if (value != imageTransform) { imageTransform = value; RaisePropertyChanged("ImageTransform"); } } } private void Image_ManipulationStarting(object sender, ManipulationStartingEventArgs e) { // Ask for manipulations to be reported relative to the canvas e.ManipulationContainer = canvMain; } private void Image_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) { ManipulationDelta md = e.DeltaManipulation; Vector trans = md.Translation; double rotate = md.Rotation; Matrix m = imageTransform.Matrix; // Find center of element and then transform to get current location of center FrameworkElement fe = e.Source as FrameworkElement; Point center = new Point(fe.ActualWidth / 2, fe.ActualHeight / 2); center = m.Transform(center); // Update matrix to reflect translation/rotation m.Translate(trans.X, trans.Y); m.RotateAt(rotate, center.X, center.Y); imageTransform.Matrix = m; RaisePropertyChanged("ImageTransform"); e.Handled = true; } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string prop) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }
Pingback: Dew Drop – January 25, 2013 (#1,487) | Alvin Ashcraft's Morning Dew
Hello there, is this Manipulation events can be apply to mouse? p/s I’m new in this .NET @ WPF things
No, the manipulation events are unique to touch input. You can handle different events, however, to deal with mouse input.
thanks for the info
thanks
Do you have any sample to do better rotation/pan by using mouse or other control?, I have been searching got some of it but not really good a bit jagged…
Well, remember that a mouse will only have one contact point, whereas touch can have many contact points. To rotate an element with touch, you’d use two contact points, so you can detect the points rotating about a center point. This isn’t possible with a mouse because you only have one mouse pointer. Typically, to rotate something using just a mouse, you’d add an additional control, like a slider or something.
great thanks for the idea…
i want to rotate an image using only three touch not from two touch ,can u tell me what i have to modify in above code