Edit this page

Custom Gestures

With TouchManager you can create and use custom gestures using the basic events as TouchDown, TouchUp, TouchMove, etc. To implement a custom gesture you will need to create a gesture recognizer that defines the touch behavior and a gesture factory.

Gesture recognizer

The gesture recognizer should be a class that derives from the abstract GestureRecognizerBase class and handles the basic touch events translating them to specific gesture-related events.

GestureRecognizerBase exposes the Element property that holds the element the recognizer is assigned to and the HasGestureHandlers property that indicates if there are any attached handlers to the specific gesture.

The GestureRecognizerBase class exposes methods for the basic event handlers of TouchManager that are used for implementing the custom gesture.

  • OnTouchEnter: This method will be called when the TouchEnter event for the element is fired.
  • OnTouchDown: This method will be called when the TouchDown event for the element is fired.
  • OnTouchMove: This method will be called when a TouchMove event for the element is fired.
  • OnTouchUp: This method will be called when a TouchUp event for the element is fired.
  • OnTouchLeave: This method will be called when a TouchLeave event for the element is fired.

Example 1: Sample gesture recognizer

public class MyGestureRecognizer : GestureRecognizerBase
{
    public override void OnTouchEnter(GestureRecognizerEventArgs args)
    {
    }

    public override void OnTouchDown(GestureRecognizerEventArgs args)
    {   
    }       

    public override void OnTouchMove(GestureRecognizerEventArgs args)
    {
    }

    public override void OnTouchUp(GestureRecognizerEventArgs args)
    {
    }

    public override void OnTouchLeave(GestureRecognizerEventArgs args)
    {
    }
}
Public Class MyGestureRecognizer
    Inherits GestureRecognizerBase
    Public Overrides Sub OnTouchEnter(args As GestureRecognizerEventArgs)
    End Sub

    Public Overrides Sub OnTouchDown(args As GestureRecognizerEventArgs)
    End Sub 

    Public Overrides Sub OnTouchMove(args As GestureRecognizerEventArgs)
    End Sub

    Public Overrides Sub OnTouchUp(args As GestureRecognizerEventArgs)
    End Sub

    Public Overrides Sub OnTouchLeave(args As GestureRecognizerEventArgs)
    End Sub     
End Class

Gesture recognizer factory

The recognizer factory creates recognizers for the UI elements. In order to register a gesture recognizer you will need to implement a factory that creates the recognizer and defines its priority. The custom recognizer factory should implement the IGestureRecognizerFactory interface which exposes the following API:

  • Priority: A property of type int that defines the priority of the gesture.
  • CreateGestureRecognizer: A method that creates a new instance of a recognizer dedicated to the UI element.
  • RegisterGestureTransitions: A method that can be used to register the allowed gesture transitions.

Example 2: Sample gesture recognizer factory

public class MyGestureRecognizerFactory : IGestureRecognizerFactory
{
    public GestureRecognizerBase CreateGestureRecognizer(System.Windows.UIElement element)
    {
        return new MyGestureRecognizer(element);
    }

    public int Priority
    {
        get
        {
            return 0;
        }
    }

    public void RegisterGestureTransitions()
    {
    }
}
Public Class MyGestureRecognizerFactory
    Implements IGestureRecognizerFactory
    Public Function CreateGestureRecognizer(element As System.Windows.UIElement) As GestureRecognizerBase
        Return New MyGestureRecognizer(element)
    End Function

    Public ReadOnly Property Priority() As Integer
        Get
            Return 0
        End Get
    End Property

    Public Sub RegisterGestureTransitions()
    End Sub
End Class   

You can see how to implement a gesture in the Creating Custom Gesture help article.

See Also