How to not stroke() every UIBezierPath in array

82 views Asked by At

I am working on a handwriting app and I am using three different UIBezierPath variables:

  • [paths] - an array of UIBezierPath's
  • temporaryPath - to help with smoothing
  • realPath - the path that will ultimately be added to the 'paths' array

I am using an array of paths rather than a single path because I have a pan tool to move the lines around the screen - so they need to be separate entities. The problem with this is that I have to redraw the whole array every time draw(:_) is called:

override func draw(_ rect: CGRect) {
    strokeColor.setStroke()

    for path in paths{
        path.stroke()
    }

    realPath?.stroke()
    temporaryPath?.stroke()
}

This is creating performance issues after drawing around 20 paths. Is there any solution to not redrawing the whole array every time draw(:_) is called?

1

There are 1 answers

0
AudioBubble On

I worked out that by only passing in the rectangle of the path I am drawing to setNeedsDisplay(), in conjunction with a condition that only draws the lines in the array when touchesEnded(_:), is an OK solution. However, other apps with similar functionality obviously don't do this so I assume there is a better solution.

    override func draw(_ rect: CGRect) {
    strokeColor.setStroke()
    realPath?.stroke()
    if yesnewline == true{
        for path in paths{
            path.stroke()
        }
        yesnewline=false
    }

    temporaryPath?.stroke()

}