Search code examples
arraysuibezierpathdotted-line

Make dotted line following uibezierpath


I have seen many examples of drawing a dashed line for iOS. I would like to draw a dotted line made of circles that follow my path. I want to use a shape layer so I can animate the stroke to simulate it being drawn onscreen.

Any suggestions for drawing little dots that follow my uibezierpath?

Conversely - is this not possible? If so can I array a bunch of circles along a path and then iterate through the array of circles making them visible? But how to array circles along a bezier?


Solution

  • I was able to draw a dashed line with circles between two uiviews:

    // dotted line
        CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
        CGContextSetLineWidth(ctx, 10); // relevant code
        CGFloat dash[] = {0.0, 10*2}; // relevant code
        CGContextSetLineCap(ctx, kCGLineCapRound); // relevant code
        CGContextSetLineDash(ctx, 0.0, dash, 2);
        CGContextMoveToPoint(ctx, view.center.x, view.center.y);
        CGContextAddLineToPoint(ctx, anotherView.center.x, anotherView.center.y);
        CGContextStrokePath(ctx);