1

I'm working on a app where i have implanted a swipe function.

When the user swipe a box it's fire a event where the box is animated to slide a side. where i created the animations like shown below

boxview.Swiped += (s, e) =>
{
    if (e.Direction == MR.Gestures.Direction.Right)
    {
        boxview.TranslateTo(-600, 0, 500, null);
    }
}

So what i wanna do is to ignore this if, if it's called again within the 500ms timespan it takes to complete the animations.

Been thinking about just take the event(e) and cancelled it. but i still need a way to say it should only do it within the timespan. and haven't had luck getting it working.

anyway thanks for any input you may have, and thanks for your time.

DaCh
  • 921
  • 1
  • 14
  • 48

2 Answers2

1

Since I'm not familiar with Xamarin itself, this answer is theoretical. TranslateTo seems to be awaitable, so you could use a flag that you are swiping, await the translate to and reset the flag after that

bool swiping;
boxview.Swiped += async (s, e) =>
{
    if (e.Direction == MR.Gestures.Direction.Right && !swiping)
    {
        swiping = true;
        await boxview.TranslateTo(-600, 0, 500, null);
        swiping =false;
    }
}
Me.Name
  • 12,259
  • 3
  • 31
  • 48
  • This could do the trick. In another swipe event i got. i need to do something else where i need to await that too. So can i have 2 awaits an run the synchronously? – DaCh Nov 10 '15 at 09:10
  • The await won't block the UI, (methods calling the async method will continue when await is hit), so the other swipe can hypothetically run concurrently if that's what you mean? – Me.Name Nov 10 '15 at 09:20
  • What i mean is that the snippet is for swipe right. i also got a swipe left. an this one got two translateTo. And i need to wait for one swipe to finish before i can call another one. And by saying that, i also mean i want the translateto to be done. before any other swipe event is called. hope this is understandable? – DaCh Nov 10 '15 at 09:25
  • That shouldn't be a problem, but is the left swipe inside a different handler or inside the same handler with `else if(e.Direction == ..Left)` ? If inside a different handler, you can still use the same flag, but it would be advisable then to add some locking to prevent racing issues on the flag. As far as 2 `translateTo` calls, if they have to run simultaneously, only await the longest running one and it should be fine. – Me.Name Nov 10 '15 at 09:30
  • never thought about using the await on only the longest. it's fixed my problem. give med a bunch of others. but thats curse i need to rewrite some of my logic. thanks for the help mate – DaCh Nov 10 '15 at 09:36
0

You could start a timer and check it on the next event.

RokX
  • 334
  • 6
  • 16
  • yha. thought about it myself. but couldn't get a timer working for this in xamarin-forms – DaCh Nov 10 '15 at 09:00