views:

127

answers:

1

I want an image to move according to the path drawn by programming codes.

I have asked a similar question (http://stackoverflow.com/questions/1571182/how-to-limit-a-uiimageview-to-move-in-a-specific-path) before, but I found that I asked in a wrong way. The image needs not to be a UIImageView. Also, the answer given above was later found that it is for Mac OS X only, not for iPhone.

Anyway, here is my codes for drawing a simple path.

CGContextSetRGBStrokeColor(theContext, 1.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(theContext, 2.0);

// Draw a bezier curve with end points s,e and control points cp1,cp2
CGPoint s = CGPointMake(30.0, 120.0);
CGPoint e = CGPointMake(300.0, 120.0);
CGPoint cp1 = CGPointMake(120.0, 30.0);
CGPoint cp2 = CGPointMake(210.0, 210.0);

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, s.x, s.y);
CGPathAddCurveToPoint(path, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
CGPathAddCurveToPoint(path, NULL, cp2.x, cp1.y, cp1.x, cp2.y, s.x, s.y);
CGPathCloseSubpath(path);

CGContextSetRGBStrokeColor(theContext, 1.0f, 0.0f, 0.0f, 1.0f);
CGContextAddPath(theContext, self.path);
CGContextSetLineWidth(theContext, 1.0);
CGContextStrokePath(theContext);

where theContext is CGContextRef. the result of above codes will draw a "8" shape rotated by 90 degree. But, how to make an image follow the path?

UPDATE #1: Sorry for duplicating questions, but I would like to further ask about how to make the image's rotation according to the path (i.e. the head vector should follow the tangent of the path) . Can anyone suggests some ideas?

UPDATE #2: How to make the path changing dynamically?

A: 

The answer you got to your other post applies to your situation. You'll have to do the translation from AppKit to UIKit yourself.

NSResponder