Wednesday, 15 August 2012

ios - How to create several UIButtons along a path/BezierCurve? -


How do I create an object with a path / bezel crow? In other words, how can I create several UIButtons with a given path with the given interval with a similar path?

I have seen dozens of questions about moving objects. But I really need a solution to make them.

I want to go along the path and want to make an object for every x digit / distance. In this way:

  .... @ .... @ .... @ .... @ ....   

In this case In, for every 4 points, get the position, and make a UIButton.

iOS is a public API that directly places you a location with a path. But there is a roundabout way to do this. Let's say that you want to separate the digits with distance of x distance.

First, make a CGPathRef in your way. (You can create a UIBezierPath if you like and then it becomes the CGPath property for it)

Then, call CGPathCreateCopyByDashingPath , using the Dash pattern of {X, X} . For example: Fixed CGFloat const kSpace = 10; CGPathRef dashedPath = CGPathCreateCopyByDashingPath (Path, Zero, 0, (CGFloat constant []) {Kaspace, Kaspace}, 2);

This gives a new path with many episodes. Each epistle is a length x segment of the original path, and is different from its neighboring episodes by the distance of x with the original path. Thus, the last points of the subway have been placed along the root path at the interval of length X.

Then, in the end, picking the last points using the dashed path, button CGPathApply and calculating what you are present there. First, you want to wrap it in a function that takes a block:

  static void applyBlockToPathElement (void * information, constant CGPathElement * element) {void (^ Block) (constant CGPathElement *) = (__Bridge zero (^) (CONC CGPathElement *)) (info); Block (element); } Void MyCGPathApplyBlock (CGPathRef path, zero (^ Block) (constant CGPathElement * element)) {CGPathApply (path, (__bridge void *) (Block), applyBlockToPathElement); }   

Then you can apply a block that finds every epic end point and creates a button there. Assume that you have createButtonAtPoint: a method name, it should perform certain tasks:

  __ block BOOL isInSubpath = NO; __bloc CGPoint subpathStart = CGPointZero; __bloc 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: // It should not appear in a dashed path. Brake; case: kCGPathElementAddLineToPoint: Case: kCGPathElementAddQuadCurveToPoint: Case: kCGPathElementAddCurveToPoint: If (! InSubpath) {[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; a The [pointIndex]; break;}});    

No comments:

Post a Comment