代码之家  ›  专栏  ›  技术社区  ›  Doug Null

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

  •  0
  • Doug Null  · 技术社区  · 11 年前

    在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 回复  |  直到 11 年前
        1
  •  0
  •   Doug Null    11 年前

    这是我的解决方案。它设置了bools滑动_向下滑动_向上滑动_向右滑动_向左滑动。享受

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