locked
How to differentiate btw Tap and DoubleTap RRS feed

  • Question

  • As discussed elsewhere in this forum differentiating between tap and doubletap can be a vexed question.

    • When does an initial tap become a tap event?
    • How do you wait for a second tap before deciding between the two?

    Tap: My app required tapping on a grid which showed a popup with information about the line of the grid tapped.

    DoubleTap: My app required the page control to be scaled up by a certain percentage on a double tap.

    Hold: Returns the scale to one (This wasn't a problem).

    The grid is programmatically generated to each cell is given a row and column index in its name. That is how the DoubleTap handler determines what to show in the popup.

    On the Popup doubletap closes the popup and there is a vertical drag event to scroll through the data displayed (changing the grid row whose data is displayed).

    My Solution (Thx for input in this forum)

    As outlined below I use a dispatch timer to differentiate. If it times out then its a tap event. Otherwise its a doubletap when teh timer gest stopped by the next tap.  Note that the system doubletap event is also trapped.  My doubltap event and this system tap both end up at the same handler.

    500  enum gestures { tap, doubletap, hold }
    501  List<gestures> Gestures;

    505 private void gestureListener_DoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
    510
    511 private void gestureListenerDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
    519
    520 System.Windows.Threading.DispatcherTimer tapTimer;
    521
    522 void dt_Tick(object sender, EventArgs e)
    528
    529 Microsoft.Phone.Controls.GestureEventArgs eTap = null;
    530
    531 private void gestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
    554
    555 private void gestureListenerTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
    588
    589 private void gestureListener_Hold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)

    • gestureListener_Tap
    • gestureListener_Hold
    • gestureListener_DoubleTap
    • These are configured in the page's XAML to handle their events via the page control GestureService.

    Hold()  Calls my scaling function (The easy part).

            private void gestureListener_Hold(object sender,
                                   Microsoft.Phone.Controls.GestureEventArgs e)

            {
                Scale(1.0);     
            }

    gestureListener_Tap()  Handles the tap event

            private void gestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
            {
                if (tapTimer==null)
                    tapTimer = new System.Windows.Threading.DispatcherTimer();
               
                if ((!Gestures.Contains(gestures.tap)) && (!tapTimer.IsEnabled) )
                {

                    Gestures.Add(gestures.tap);

                    tapTimer = new System.Windows.Threading.DispatcherTimer();
                    //Set  interval to just shy of 1s.  Can make shorter
                    tapTimer.Interval = new TimeSpan(0, 0, 0, 0, 999);
                    tapTimer.Tick += new EventHandler(dt_Tick);

                    eTap = e;//Need e to determine teh originalsource (grid cell)
                    e.Handled = true;
                    tapTimer.Start();
                }
                else
                {
                    //Tap occurred before timeout so doubletap
                    gestureListenerDoubleTap(sender,e);
                }
            }

    The timer tick event handler (It's a one shot):

            void dt_Tick(object sender, EventArgs e)
            {
                tapTimer.Stop();
                Gestures.Clear();
                gestureListenerTap(null, eTap);
            }

    gestureListener_DoubleTap() Handles the system double tap event (Just calls my doubletap handler)

            private void gestureListener_DoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
            {
                gestureListenerDoubleTap(sender, e);

            }

    My DoubleTap Handler:

            private void gestureListenerDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
            {
                tapTimer.Stop();
                Scale(GetNextScale(CurrentScale));
                Gestures.Clear();
                e.Handled = true;

            }

    Note when the scale gets to 2.0 the next scale is 1.0. It increments by 0.25 each time.

    My tap Handler:

           private void gestureListenerTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
            {
                if (e.OriginalSource is TextBlock)
                {
                    TextBlock tb = (TextBlock)e.OriginalSource;

                     .........................................

                     //Get row from tb.Name

                     .........................................

                      //Show detailed data from row in popup.

                     .........................................

                  }

              }


    DJaus Snr SW Dev (Embedded Systems and .NET)

    Friday, August 17, 2012 5:36 AM