locked
TapGestureRecognizer - using NumberOfTapsRequired for double tap RRS feed

  • Question

  • User49731 posted

    Ideally I'm looking for a LongTap event or GestureRecognizer but as yet that doesn't exist in .Forms (hopefully next release!) so as a temporary workaround to my situation where my app normally uses tap to open and longtap (tap and hold) to show a popup context menu I thought I'd use tap to open and double tap to show the context menu as TapGestureRecognizer has a property NumberOfTapsRequired which should do the trick .. however I'm having problems working out how to do this.

    Based on the following code, whilst the second doubletapped function is indeed only called when a double tap occurs, the first tapped function is called both for a tap and a double tap but without any way as far as I can tell to know in the tapped function that this is a double tap and hence to ignore it.

        public someObject() : base()
        {
            var tgr = new TapGestureRecognizer();
            tgr.NumberOfTapsRequired = 1;
            tgr.Tapped += tapped;
            GestureRecognizers.Add(tgr);
    
            var ttgr = new TapGestureRecognizer();
            ttgr.NumberOfTapsRequired = 2;
            ttgr.Tapped += doubletapped;
            GestureRecognizers.Add(ttgr);
        }
        private void tapped(object sender, EventArgs e)
        {
        // deal with tap
        }
        private void doubletapped(object sender, EventArgs e)
        {
            // deal with doubletap
        }
    

    I would've hoped that perhaps in the EventArgs of tapped it would have the number of taps so I could then ignore double taps within it or is there some other way to deal with it?

    Thanks

    Thursday, July 10, 2014 2:47 PM

All replies

  • User40009 posted

    I would flip those around, add the double tapped recognizer first, then the single tap one

    from the doubletapped method, set a DateTime LastDoubleTapped that you declare globally, then inside the tapped method, check if LastDoubleTapped happened recently. No idea if that'll work, but worth a try (I'd normally try it myself but I don't have any time today)

    Snail

    Thursday, July 10, 2014 2:56 PM
  • User2773 posted

    @DerekPapworth? I would use a timer: - 1st TapGestureRecognizer - start the timer, tick method set to "tapped" - 2nd TapGestureRecognizer - stop the timer and invoke "doubletapped"

    Thursday, July 10, 2014 4:30 PM
  • User49731 posted

    Thanks for the ideas @RyanHatfield @DanielL?

    Ryan's thought on switching the sequence I thought was a goer and tried that with rather than a datetime just an instance bool variable "taphandled" in the double tape one setting to true and then in the tapped check if true, ignore and set false but .. even with them flipped around the tap gets called before the doubletap so that didn't work.

    Haven't tried Daniels idea yet but sounds a possible .. any thoughts on how long to allow the timer? As presume I need to use the timeout to then say "ok, no double tap occurred so do tap stuff"

    Thursday, July 10, 2014 5:06 PM
  • User40009 posted

    As long as you want the double tap. I see this having issues though.. what if the app laggs? what if ___ something happens?

    And you didn't ask for it but here's my 2 cents:

    Just stop and think of how many apps on your phone require a double tap. I don't think it's a normal mobile gesture. Double tap is OK, but not when there's also a single tap. A user should be able to figure out the gesture without very much instruction. I would suggest a long press or a swipe.

    Thursday, July 10, 2014 6:01 PM
  • User2773 posted

    @RyanHatfield? My position is similar but the problem is that Xamarin.Forms doesn't have swipe and long press gesture recognizers.

    Thursday, July 10, 2014 7:32 PM
  • User40009 posted

    @DanielL Then let's make one!

    Thursday, July 10, 2014 7:38 PM
  • User2773 posted

    @RyanHatfield? I tried :) but.... GestureRecognizer is internal

    Thursday, July 10, 2014 7:51 PM
  • User40009 posted

    @DanielL? Challenge Accepted.

    Thursday, July 10, 2014 7:53 PM
  • User2773 posted

    @RyanHatfield? Good luck! PM me if you need any help =]

    Thursday, July 10, 2014 8:00 PM
  • User49731 posted

    Hi guys, @RyanHatfield @DanielL

    Yes agree with all said, I don't really want to use doubletap for this, as mentioned at first it's merely as longtap doesn't yet exist in .Forms so hence was using doubtletap as temporary method to continue my app development with view to replace longtap when available so ..

    .. if we can come up with a longtap that'd be great! :) .. I tried looking myself at it but I can't seem to find any form of "release" gesture in .Forms to allow a combination of tap gesture, timer and release gesture to base one on .. any thoughts on that?

    By way of completeness on the doubletap front though, here's what I ended up doing for that for now :

    private bool tapHandled;
    
        public uuiImage() : base()
        {
            var tgr = new TapGestureRecognizer();
            tgr.NumberOfTapsRequired = 1;
            tgr.Tapped += tapped;
            GestureRecognizers.Add(tgr);
    
            var ttgr = new TapGestureRecognizer();
            ttgr.NumberOfTapsRequired = 2;
            ttgr.Tapped += doubletapped;
            GestureRecognizers.Add(ttgr);
        }
        private void tapped(object sender, EventArgs e)
        {
            tapHandled = false;
            Xamarin.Forms.Device.StartTimer(new TimeSpan(0, 0, 0, 0, 300), taptimer);
        }
        private void doubletapped(object sender, EventArgs e)
        {
            tapHandled = true;
            // do stuff here
        }
        private bool taptimer()
        {
            if (!tapHandled)
            {
                tapHandled = true;
                // do stuff here
            }
            return false;
        } 
    
    Sunday, July 13, 2014 11:24 AM
  • User49731 posted

    Just discovered the numberoftapsrequired doesn't seem to have any effect in Android as the function doubletapped never gets called in the above code .. anyone else have that working or not?

    Thursday, July 24, 2014 1:03 PM
  • User14 posted

    Unfortunately this is a known issue (NumberOfTapsRequired not working on Android), already in our backlog. Not sure what the timeframe to fix is, but we are aware of it!

    Thursday, July 24, 2014 5:27 PM
  • User49731 posted

    Any news on NumberOfTapsRequired fix in Android?

    Tuesday, September 23, 2014 12:11 PM
  • User102046 posted

    @CraigDunn Any updates on "NumberOfTapsRequired not working issue on Android"? this is the code i am using with XF version 1.4.0.6341 Stable. still double tap isnot working

    var gesture = new TapGestureRecognizer(); gesture.NumberOfTapsRequired = 2; gesture.Command = new Command(TapGestuer); gesture.CommandParameter = Indexes.FieldType.ChangeBaseUrl; var img = //My Image

    img.GestureRecognizers.Add(gesture);

    Wednesday, March 25, 2015 12:43 PM
  • User102046 posted

    @DerekPapworth @CraigDunn
    this is how i do it for now : private void TapGestuer(object o) { tapCount++; if (tapCount >= 7) { tapCount = 0; //TODO Perform my action } if (!isTimerSet) { isTimerSet = true; Device.StartTimer(new TimeSpan(0, 0, 0, 2, 0), () => { isTimerSet = false; tapCount = 0; return false; }); } }

    Thursday, March 26, 2015 6:47 AM
  • User65389 posted

    At all readers of this thread. I have tried to add a tapGestureRecognizer with NumberOfTapsRequired = n to a label (to show a password entry, if tapped the specific times). Unfortunately this is not usable on iOS (as the taps have to be took place in a specific time and the time can't be set.) :disappointed:
    So from 10 attempts, maybe one attempt is done in the "estimated" time -> not usable at least not for NumberOfTapsRequired >=4. So I have re-activated my "old" solution to count the taps myself (similar to the example of @lXami3), as it also is not usable on iOS.

    This also don't work perfect, but better than with "NumberOfTapsRequired".

    Hope this helps someone...

    Monday, June 22, 2015 4:02 PM
  • User176878 posted

    Hi,

    How to do single tap event fire for multiple click events on label or Image?

    Friday, December 4, 2015 9:35 AM
  • User329360 posted

    @"DerekPapworth.4183" said: Hi guys, @RyanHatfield @DanielL

    Yes agree with all said, I don't really want to use doubletap for this, as mentioned at first it's merely as longtap doesn't yet exist in .Forms so hence was using doubtletap as temporary method to continue my app development with view to replace longtap when available so ..

    .. if we can come up with a longtap that'd be great! :) .. I tried looking myself at it but I can't seem to find any form of "release" gesture in .Forms to allow a combination of tap gesture, timer and release gesture to base one on .. any thoughts on that?

    By way of completeness on the doubletap front though, here's what I ended up doing for that for now :

    private bool tapHandled;

        public uuiImage() : base()
        {
            var tgr = new TapGestureRecognizer();
            tgr.NumberOfTapsRequired = 1;
            tgr.Tapped += tapped;
            GestureRecognizers.Add(tgr);
    
            var ttgr = new TapGestureRecognizer();
            ttgr.NumberOfTapsRequired = 2;
            ttgr.Tapped += doubletapped;
            GestureRecognizers.Add(ttgr);
        }
        private void tapped(object sender, EventArgs e)
        {
            tapHandled = false;
            Xamarin.Forms.Device.StartTimer(new TimeSpan(0, 0, 0, 0, 300), taptimer);
        }
        private void doubletapped(object sender, EventArgs e)
        {
            tapHandled = true;
            // do stuff here
        }
        private bool taptimer()
        {
            if (!tapHandled)
            {
                tapHandled = true;
                // do stuff here
            }
            return false;
        } 
    

    I know this thread is a bit old, but I just ran into the same issue. This solution seems to work. Interestingly, on Android it just works. A double tap will not trigger a tap. On Windows and iOS, this type of tap handling is required if you need Single and Double Tap handlers.

    Wednesday, October 31, 2018 1:18 AM
  • User392579 posted

    how to apply zoom in and zoom out on ImageView with doubletap and explain with code

    Tuesday, February 4, 2020 10:33 AM