How to generate a CGPoint-Array from UIBezierPath (to move an object along a given path)

I have UIBezierPath (curved like "8", only 4 points), and I need to make some kind of CGPoint-Array out of it. Any ideas? thanks!

edit:

I have my bezier initialized this way

-(void) initBezier { theBezierPath = [UIBezierPath bezierPath]; [theBezierPath moveToPoint:P(211.00, 31.00)]; [theBezierPath addCurveToPoint:P(870.00, 191.00) controlPoint1:P(432.00, -11.00) controlPoint2:P(593.00, 209.00)]; [theBezierPath addCurveToPoint:P(731.00, 28.00) controlPoint1:P(1061.95, 178.53) controlPoint2:P(944.69, 5.78)]; [theBezierPath addCurveToPoint:P(189.00, 190.00) controlPoint1:P(529.00, 49.00) controlPoint2:P(450.00, 189.00)]; [theBezierPath addCurveToPoint:P(211.00, 31.00) controlPoint1:P(-33.01, 190.85) controlPoint2:P(71.00, 37.00)]; } 

and I am animating an object on it with

 anim = [CAKeyframeAnimation animationWithKeyPath:@"emitterPosition"]; anim.path = theBezierPath.CGPath; anim.calculationMode = kCAAnimationCubicPaced; anim.repeatCount = HUGE_VALF; anim.duration = tme; 

I want to animate an object pixel by pixel by pixel (via touch position). I want the object to "snap" the specified touch coordinate to the nearest point on the curve, so that it touches - the object slides along the path.

+4
source share
2 answers

Use the CGPathApply() function to iterate over all the elements in the path. As one of the arguments, you need to specify a pointer to a function, which will then be called for each element of the path. The data structure of the path element (of type CGPathElement ) then contains the point (s) that describe it.

Use NSValue as a wrapper to add dots to the NSMutableArray .

+7
source

Neither UIBezierPath nor CGPath allow you to evaluate points along an arbitrary Bezier path or a way to find the nearest point on the path. You will have to write this code yourself.

Fortunately, this is a very well-studied topic. This tutorial has a lot of useful information to get you started. You are interested in Bezier's β€œcubic” or β€œthird order” curves.

+4
source

All Articles