How to move an UIView along a curved CGPath according to user dragging the view

Posted by Felipe Cypriano on Game Development See other posts from Game Development or by Felipe Cypriano
Published on 2011-06-16T21:11:05Z Indexed on 2012/06/07 10:49 UTC
Read the original article Hit count: 327

Filed under:
|
|

I'm trying to build a interface that the user can move his finger around the screen an a list of images moves along a path. The idea is that the images center nevers leaves de path.

Most of the things I found was about how to animate using CGPath and not about actually using the path as the track to a user movement.

I need to objects to be tracked on the path even if the user isn't moving his fingers over the path.

For example (image bellow), if the object is at the beginning of the path and the user touches anywhere on the screen and moves his fingers from left to right I need that the object moves from left to right but following the path, that is, going up as it goes to the right towards the path's end.

This is the path I've draw, imagine that I'll have a view (any image) that the user can touch and drag it along the path, there's no need to move the finger exactly over the path. If the user move from left to right the image should move from left to right but going up if need following the path.

The curved path

This is how I'm creating the path:

   CGPoint endPointUp = CGPointMake(315, 124);
    CGPoint endPointDown = CGPointMake(0, 403);
    CGPoint controlPoint1 = CGPointMake(133, 187);
    CGPoint controlPoint2 = CGPointMake(174, 318);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, endPointUp.x, endPointUp.y);
    CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPointDown.x, endPointDown.y);

Any idead how can I achieve this?

© Game Development or respective owner

Related posts about iphone

Related posts about movement