0

I'm using subclass of UIView, in order to draw shapes with UIBezierPath and touchEvents.

I'm making an app (like paint), in which I have two modes of drawing (change stroke color and width in drawRect) and when I switch between them, the already drew paths change their colors and widhts to the new ones. In order to fix this problem, when touchesEnded, I save the current Path in array and create a new one. I want to redraw the saved path(s) on the view when the new one is made, so they can be visible.

In drawRect, foreach path in my array, I do path.Stroke(), in order to visualize it, but when I switch to a certain mode of drawing, the saved paths again change their width and color.

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

        this.Path = new UIBezierPath ();
        var touch = touches.AnyObject as UITouch;
        var point = touch.LocationInView (this);
        this.Path.MoveTo (point);
    }

    public override void TouchesMoved (Foundation.NSSet touches, UIEvent evt)
    {
        base.TouchesMoved (touches, evt);
        var touch = touches.AnyObject as UITouch;
        this.Path.AddLineTo(touch.LocationInView(this));
        this.SetNeedsDisplay ();
    }

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

        paths.Add (Path);
    }

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

        if (isScribbling)
        {
            this.Path.LineWidth = 2.5f;
            this.PathColor.SetStroke ();
        } else if (isMarking)
        {
            this.Path.LineWidth = 15.0f;
            UIColor.FromRGBA (255, 0, 0, 0.3f).SetStroke ();
        }
        this.Path.Stroke ();

    }

private void DrawSavedPaths()
    {
        if (paths.Count > 0)
        {
            foreach (UIBezierPath path in paths)
            {
                path.Stroke ();
            }
        }
    }

Basically, in the same UIView, where I'm drawing, I want to have the previous paths drewn, preventing them from being changed.

P.Todorov
  • 43
  • 10

0 Answers0