2017-10-03 5 views
1

Je fais une application où l'utilisateur peut dessiner quelque chose avec ses doigts ... Je ne sais pas comment je peux le faire ...doigt dessin dans Xamarin.iOS App (C#)

Après la recherche, j'ai trouvé le code ci-dessous.

Mais avec ce code je ne peux dessiner qu'une ligne. Si le contact est terminé et qu'un nouvel événement tactile commence, la ligne continue sur ce point ... L'ancienne ligne se connecte automatiquement à la nouvelle. Donc, je veux créer une nouvelle ligne à chaque toucher.

Est-ce que quelqu'un sait comment cela fonctionne?

Code

CGPath path; 
    CGPoint initialPoint; 
    CGPoint latestPoint; 

    public DrawView (IntPtr handle) : base (handle) 
    { 
     BackgroundColor = UIColor.White; 

     path = new CGPath(); 
    } 

    public override void TouchesBegan(NSSet touches, UIEvent evt) 
    { 
     base.TouchesBegan(touches, evt); 

     UITouch touch = touches.AnyObject as UITouch; 

     if (touch != null) 
     { 
      initialPoint = touch.LocationInView(this); 
     } 
    } 

    public override void TouchesMoved(NSSet touches, UIEvent evt) 
    { 
     base.TouchesMoved(touches, evt); 

     UITouch touch = touches.AnyObject as UITouch; 

     if (touch != null) 
     { 
      latestPoint = touch.LocationInView(this); 
      SetNeedsDisplay(); 
     } 
    } 

    public override void Draw(CGRect rect) 
    { 
     base.Draw(rect); 

     if (!initialPoint.IsEmpty) 
     { 

      //get graphics context 
      using (CGContext g = UIGraphics.GetCurrentContext()) 
      { 
       //set up drawing attributes 
       g.SetLineWidth(2); 
       UIColor.Black.SetStroke(); 

       //add lines to the touch points 
       if (path.IsEmpty) 
       { 
        path.AddLines(new CGPoint[] { initialPoint, latestPoint }); 
       } 
       else 
       { 
        path.AddLineToPoint(latestPoint); 
       } 

       //add geometry to graphics context and draw it 
       g.AddPath(path); 

       g.DrawPath(CGPathDrawingMode.Stroke); 
      } 
     } 
    } 

Répondre

1

Vous pouvez créer un CGPath supplémentaire pour enregistrer les chemins et les dessiner avec CGContext

code:

public partial class DrawLine : UIView 
{ 

    CGPath pathtotal; 
    CGPath path; 

    CGPoint initialPoint; 
    CGPoint latestPoint; 
    public DrawLine(IntPtr handle) : base(handle) 
    { 
     BackgroundColor = UIColor.White; 
     pathtotal = new CGPath(); 
    } 
    public override void TouchesBegan(NSSet touches, UIEvent evt) 
    { 
     base.TouchesBegan(touches, evt); 
     path = new CGPath(); 
     UITouch touch = touches.AnyObject as UITouch; 

     if (touch != null) 
     { 
      initialPoint = touch.LocationInView(this); 
     } 
    } 

    public override void TouchesMoved(NSSet touches, UIEvent evt) 
    { 
     base.TouchesMoved(touches, evt); 

     UITouch touch = touches.AnyObject as UITouch; 

     if (touch != null) 
     { 
      latestPoint = touch.LocationInView(this); 
      SetNeedsDisplay(); 
     } 
    } 

    public override void Draw(CGRect rect) 
    { 
     base.Draw(rect); 

     if (!initialPoint.IsEmpty) 
     { 

      //get graphics context 
      using (CGContext g = UIGraphics.GetCurrentContext()) 
      { 
       //set up drawing attributes 
       g.SetLineWidth(2); 
       UIColor.Black.SetStroke(); 

       //add lines to the touch points 
       if (path.IsEmpty) 
       { 
        path.AddLines(new CGPoint[] { initialPoint, latestPoint }); 
       } 
       else 
       { 
        path.AddLineToPoint(latestPoint); 
       } 

       //add geometry to graphics context and draw it 
       pathtotal.AddPath(path); 

       g.AddPath(pathtotal); 
       g.DrawPath(CGPathDrawingMode.Stroke); 
      } 
     } 
    } 
} 

Résultat du test:

enter image description here

+0

Merci beaucoup ... Cela fonctionne parfaitement – Blanko