如何在Xamarin Monotouch中设置UIPanGestureRecognizer的方向

时间:2013-09-26 20:02:07

标签: xamarin.ios monodevelop xamarin uipangesturerecognizer

在Monotouch C#中,如何区分左,右,上和下滑动? 我需要为每个人举办一次活动。

以下是我如何创建我的滑动识别器,它有效,但不是方向性的: view_swipe_gesture = new UIPanGestureRecognizer(); view_swipe_gesture.AddTarget(this, new MonoTouch.ObjCRuntime.Selector("view_swipe_gesture_Selector")); this.View.AddGestureRecognizer(view_swipe_gesture);

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。它设置了bools swipe_down swipe_up swipe_right swipe_left。享受。

        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 ); 
    }