Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Swipe Guesture in Windows 8 Grid

I am trying to implement a custom control which consists of a grid with some canvas elements as children , When a swipe action is made on the grid , I am intended to preform some operation with the canvas elements . I am unable to handle the swipe for the grid , i have posted the same in the msdn - win8 Dev forum

like image 241
Rajmohan Kathiresan Avatar asked May 29 '12 11:05

Rajmohan Kathiresan


3 Answers

I was in the same boat as you guys, since there was no samples out there on how this was done, but after perusing and scrutinizing the MSDN documentation on how a swipe gesture is implemented on a Windows 8 Store app using C#, this is what i came up with (and it works for my app which requires swiping up / down / left / right):

First of all, instead of the GestureRecognizer, the Manipulation events need to be used, so on the grid that you want to handle the swiping (lets' say you make it so that it takes the whole screen so it interprets the gestures) do the following:

I called my grid swipingSurface and i'm handling manipulation modes for both the Y-axis and X-axis:

swipingSurface.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;

Then wire up the manipulation events that you want to be notified, in my case i just want to know then the manipulation started and when it ended:

swipingSurface.ManipulationStarted += OnManipulationStarted;
swipingSurface.ManipulationCompleted += OnManipulationCompleted;

Do whatever you want on your manipulation started, such as getting the initial point if you want. But the actual trick is on the ManipulationCompleted event, in which you need to get the Velocities resulting from your gesture, as follows:

public void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e) {
    var velocities = e.Velocities;      
}

The ManipulationCompletedEventArgs Velocities property will bring back a struct of type ManipulationVelocities, which contains other properties inside:

-Angular: The rotational velocity in degrees per millisecond.

-Expansion: The expansion, or scaling, velocity in DIPs per millisecond.

-Linear: The straight line velocity in DIPs per millisecond.

I'm actually looking at the Linear velocity, which is a Point that contains X and Y values indicating the direction in which the gesture was performed; for example, if the swipe was upward, you will notice that the Y value is positive, and if its down the Y value is negative; the same goes for the X value, if the swipe is left, the X values are negative and if the swipe is right, the X values are positive, so you can play around with those values and check your swiping direction, final points, etc.

Hope this helps.

like image 122
drcoderz Avatar answered Nov 13 '22 10:11

drcoderz


You could try setting ManipulationMode on your swipe-able control and handling the Manipulation~ events. Note that some controls might stop bubbling of UI events, so if you say put your control inside of a Button or a ScrollViewer - the events might not work.

like image 38
Filip Skakun Avatar answered Nov 13 '22 10:11

Filip Skakun


You could check out SwipeHintThemeAnimation that uses GestureRecognizer to hook up to a Rectangle control or modify it to use your Grid control, see the documentation.

like image 40
Nasser Amira Avatar answered Nov 13 '22 12:11

Nasser Amira