2016-08-02 6 views

答えて

2

あなたはXamarin/C#の例

実装を探しているならActivity(または表示)のGestureDetector.IOnGestureListenerを入力し、OnFlingメソッドでタッチの方向を計算します。

[Activity(Label = "Swiper", MainLauncher = true, Icon = "@mipmap/icon")] 
public class MainActivity : Activity, GestureDetector.IOnGestureListener 
{ 
    GestureDetector gestureDetector; 
    const int SWIPE_DISTANCE_THRESHOLD = 100; 
    const int SWIPE_VELOCITY_THRESHOLD = 100; 

    public bool OnDown(MotionEvent e) 
    { 
     return true; 
    } 

    public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
    { 
     float distanceX = e2.GetX() - e1.GetX(); 
     float distanceY = e2.GetY() - e1.GetY(); 
     if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) 
     { 
      if (distanceX > 0) 
       OnSwipeRight(); 
      else 
       OnSwipeLeft(); 
      return true; 
     } 
     return false; 
    } 

    void OnSwipeLeft() 
    { 
     Button button = FindViewById<Button>(Resource.Id.myButton); 
     button.Text = "Swiped Left"; 
    } 

    void OnSwipeRight() 
    { 
     Button button = FindViewById<Button>(Resource.Id.myButton); 
     button.Text = "Swiped Right"; 
    } 

    public void OnLongPress(MotionEvent e) 
    { 
    } 

    public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
    { 
     return false; 
    } 

    public void OnShowPress(MotionEvent e) 
    { 
    } 

    public bool OnSingleTapUp(MotionEvent e) 
    { 
     return false; 
    } 

    public bool OnTouch(View v, MotionEvent e) 
    { 
     return gestureDetector.OnTouchEvent(e); 
    } 

    public override bool OnTouchEvent(MotionEvent e) 
    { 
     gestureDetector.OnTouchEvent(e); 
     return false; 
    } 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.Main); 

     gestureDetector = new GestureDetector(this); 
    } 
} 
2

onScroll

@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
     float distanceY) { 
    Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString()); 
    return true; 
} 


@Override 
public boolean onFling(MotionEvent event1, MotionEvent event2, 
     float velocityX, float velocityY) { 
    Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); 
    return true; 
} 
関連する問題