Reputation: 1446
I'm using GestureDetector.SimpleOnGestureListener
class for handling some simple gestures on my app. And it's works perfectly.
But now I'm faced with a problem of handling multiple tap gesture. I Just want to configure NumbersOfTaps and handle appropriate gesture.
But can't find any details or notes How to implement it on Andoid or Xamarin.Android documentation.
Upvotes: 3
Views: 472
Reputation: 1303
DateTime _firstTap;
int _tapCount = 0;
const int TAP_COUNT_TRESHOLD = 5; //number of taps
const int TIME_TRESHOLD 200; //ms time
protected override void OnResume()
{
myButton.Clicked += ButtonTapped;
}
protected override void OnPause()
{
myButton.Clicked -= ButtonTapped;
}
void ButtonTapped(object sender, EventArgs e){
var time = Math.Round((DateTime.Now - _firstTap).TotalMilliseconds, MidpointRounding.AwayFromZero);
if (time > TIME_TRESHOLD)
{
_tapCount = 1;
_firstTap = DateTime.Now;
}
else
_tapCount++;
if (_tapCount == TAP_COUNT_TRESHOLD)
{
//do your logic here
}
}
Time limit and number of clicks are configurable.
Upvotes: 6
Reputation: 899
Use that code, I do not believe if it is good approach but it works. Check it out.
long milliSeconds = 0;
var tapCount = 0;
var millisecondsPeriod = 200;
button.Click += (object sender, EventArgs e) => {
if (milliSeconds == 0) {
milliSeconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
tapCount++;
} else {
var currMill = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - milliSeconds;
if (currMill < millisecondsPeriod) {
milliSeconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
tapCount++;
if (tapCount == 3) {
Toast.MakeText (this, "triple", ToastLength.Long).Show ();
}
} else {
tapCount = 0;
milliSeconds = 0;
}
}
};
You can change the millisecondsPeriod how you wish.
Upvotes: 2