Judgment of single touch and double touch with Skiasharp

TS44 81 Reputation points
2021-02-01T22:48:14.183+00:00

For example, if there is a code that judges single touch and multi-touch, each judgment can be made, but even if multi-touch is performed, the multi-touch command is executed after the single-touch command is executed. May be done.
Is there a reliable way to do single touch and multi touch respectively?

Dictionary<long, SKPoint> dragDictionary = new Dictionary<long, SKPoint>();

    private void OnTouch(object sender, SKTouchEventArgs e)
    {
        switch (e.ActionType)
        {
            case SKTouchAction.Pressed:
                dragDictionary[e.Id] = e.Location;
                break;
            case SKTouchAction.Entered:
                break;
            case SKTouchAction.Moved:
                if (dragDictionary.Keys.Count > 1)
                {
                    dragDictionary[e.Id] = e.Location;
                    SKPoint? p1 = null;
                    SKPoint? p2 = null;
                    foreach (long key in dragDictionary.Keys)
                    {
                        if (p1 == null)
                        {
                            p1 = dragDictionary[key];
                        }
                        else if (p2 == null)
                        {
                            p2 = dragDictionary[key];
                        }
                    }
                    //MultiTouch handle
                }
                else
                {
                    //SingleTouch handle
                }
                break;
            case SKTouchAction.Released:
                dragDictionary.Remove(e.Id);
                break;
            case SKTouchAction.Exited:
                break;
        }
        // we have handled these events
        e.Handled = true;
        ((SKCanvasView)sender).InvalidateSurface();
    }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,256 Reputation points Microsoft Vendor
    2021-02-02T02:47:40.147+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you judge single tap or double tap in SKTouchAction.Moved tag, your finger cannot left your screen, it will not work for double tap.

    So, you can hanle the single tap or double tap in SKTouchAction.Pressed: like following code.

       private static int clickCount;  
               private static object _sender;  
                 
              // Dictionary<long, SKPoint> dragDictionary = new Dictionary<long, SKPoint>();  
               private void CanvasView_Touch(object sender, SKTouchEventArgs e)  
               {  
         
                   // throw new System.NotImplementedException();  
                    
                   switch (e.ActionType)  
                   {  
                       case SKTouchAction.Pressed:  
                           
         
         
                           if (clickCount < 1)  
                           {  
                               TimeSpan tt = new TimeSpan(0, 0, 0, 0, 250);  
                               _sender = sender;  
                               Device.StartTimer(tt, ClickHandle);  
                           }  
                           clickCount++;  
         
                           break;  
                           
                       case SKTouchAction.Entered:  
         
                          // Console.WriteLine("======================Entered================");  
                           break;  
                       case SKTouchAction.Moved:  
         
                           // Console.WriteLine("======================Moved================");  
                           //if (dragDictionary.Keys.Count > 1)  
                           //{  
                           //    dragDictionary[e.Id] = e.Location;  
                           //    SKPoint? p1 = null;  
                           //    SKPoint? p2 = null;  
                           //    foreach (long key in dragDictionary.Keys)  
                           //    {  
                           //        if (p1 == null)  
                           //        {  
                           //            p1 = dragDictionary[key];  
                           //        }  
                           //        else if (p2 == null)  
                           //        {  
                           //            p2 = dragDictionary[key];  
                           //        }  
                           //    }  
                           //    //MultiTouch handle  
         
                           //    Console.WriteLine("======================DOUBLE TAP================");  
                           //}  
                           //else  
                           //{  
                           //    //SingleTouch handle  
         
                           //    Console.WriteLine("======================SINGLE  TAP================");  
                           //}  
                           break;  
                       case SKTouchAction.Released:  
                           // dragDictionary.Remove(e.Id);  
         
                         
                           break;  
                       case SKTouchAction.Exited:  
         
                          
                           break;  
                   }  
                   // we have handled these events  
                   e.Handled = true;  
                   ((SKCanvasView)sender).InvalidateSurface();  
               }  
         
               private bool ClickHandle()  
               {  
                   if (clickCount > 1)  
                   {  
                        
                       DisplayAlert("info", "DOUBLE TAP","Ok");  
                      
                   }  
                   else  
                   {  
                        
                       DisplayAlert("info", "single TAP", "Ok");  
                     
                   }  
                   clickCount = 0;  
                   _sender = null;  
                   return false;  
               }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.