2013-09-26 20 views
0

在Monotouch C#中,如何區分左,右,上,下滑動? 我需要爲每個事件。如何在Xamarin Monotouch中設置UIPanGestureRecognizer的方向

這裏是我提出創建我的刷卡識別器,其工作,但不是定向: view_swipe_gesture = new UIPanGestureRecognizer(); view_swipe_gesture.AddTarget(this, new MonoTouch.ObjCRuntime.Selector("view_swipe_gesture_Selector")); this.View.AddGestureRecognizer(view_swipe_gesture);

回答

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); 
    } 
相關問題