Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>iOS doesn't have a public API that directly gives you points spaced along a path. But there is a roundabout way to do it. Suppose you want points along the path spaced a distance of X apart.</p> <p>First, create a <code>CGPathRef</code> containing your path. (You can construct a <code>UIBezierPath</code> if you prefer and then gets its <code>CGPath</code> property.)</p> <p>Then, call <code>CGPathCreateCopyByDashingPath</code>, using a dash pattern of <code>{ X, X }</code>. For example:</p> <pre><code>static CGFloat const kSpace = 10; CGPathRef dashedPath = CGPathCreateCopyByDashingPath(path, NULL, 0, (CGFloat const []){ kSpace, kSpace }, 2); </code></pre> <p>This returns a new path containing multiple subpaths. Each subpath is a length X segment of the original path, and is separated from its neighboring subpaths by a distance of X along the original path. Thus the endpoints of the subpaths are spaced along the original path at an interval of length X.</p> <p>So, finally, enumerate the dashed path using <code>CGPathApply</code>, picking the endpoints and creating buttons there. First, you'll want to wrap it in a function that takes a block:</p> <pre><code>static void applyBlockToPathElement(void *info, const CGPathElement *element) { void (^block)(const CGPathElement *) = (__bridge void (^)(const CGPathElement *))(info); block(element); } void MyCGPathApplyBlock(CGPathRef path, void (^block)(const CGPathElement *element)) { CGPathApply(path, (__bridge void *)(block), applyBlockToPathElement); } </code></pre> <p>Then you can apply a block that finds the each subpath endpoint and creates a button there. Assuming you have a method named <code>createButtonAtPoint:</code>, something like this should work:</p> <pre><code>__block BOOL isInSubpath = NO; __block CGPoint subpathStart = CGPointZero; __block CGPoint currentPoint = CGPointZero; MyCGPathApplyBlock(dashedPath, ^(const CGPathElement *element) { switch (element-&gt;type) { case kCGPathElementMoveToPoint: if (isInSubpath) { [self createButtonAtPoint:currentPoint]; isInSubpath = NO; } currentPoint = element-&gt;points[0]; break; case kCGPathElementCloseSubpath: // This should not appear in a dashed path. break; case kCGPathElementAddLineToPoint: case kCGPathElementAddQuadCurveToPoint: case kCGPathElementAddCurveToPoint: if (!isInSubpath) { [self createButtonAtPoint:currentPoint]; isInSubpath = YES; } int pointIndex = element-&gt;type == kCGPathElementAddLineToPoint ? 0 : element-&gt;type == kCGPathElementAddQuadCurveToPoint ? 1 : /* element-&gt;type == kCGPathElementAddCurveToPoint ? */ 2; currentPoint = element-&gt;points[pointIndex]; break; } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload