Question

In Monotouch C#, how can I differentiate between left, right, up, and down swipes? I need an event for each.

Here is how I present create my swipe recognizer, which works, but is not directional: view_swipe_gesture = new UIPanGestureRecognizer(); view_swipe_gesture.AddTarget(this, new MonoTouch.ObjCRuntime.Selector("view_swipe_gesture_Selector")); this.View.AddGestureRecognizer(view_swipe_gesture);

Was it helpful?

Solution

Here is my solution. It sets bools swipe_down swipe_up swipe_right swipe_left. Enjoy.

        public void view_swipe_gesture_handler(UIGestureRecognizer sender)
    {


        var translation = view_swipe_gesture.TranslationInView( this.View );
        int x = (int)translation.X;
        int y = (int)translation.Y;
        int absX = x;
        int absY = y;

        if (absX < 0)
            absX *= -1;

        if (absY < 0)
            absY *= -1;

        bool horizontal, veritical;
        horizontal = ( absX > absY ) ;
        veritical = !horizontal;

        // Determine up, down, right, or left:
        bool swipe_up, swipe_down, swipe_left, swipe_right;
        swipe_left = (horizontal && x < 0);
        swipe_right = (horizontal && x >= 0);
        swipe_up = (veritical && y < 0);
        swipe_down = (veritical && y >= 0);



        ++swipes;
        String dir="";
        if (swipe_down)
            dir = "DOWN";
        if (swipe_up)
            dir = "UP";
        if (swipe_left)
            dir = "LEFT";
        if (swipe_right)
            dir = "RIGHT";

                      show_debug_status("VIEW SWIPE - swipes=" + swipes 
                          + "  x=" + translation.X  + "  y=" + translation.Y 
                          + "  DIR=" + dir ); 
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top