Question

I'm using this to animate an object along a path. I'd like to know how to get the position of the object at any point during the animation to check for a collision with another object. Any ideas how to go about this? Thanks.

- (void) animateCicleAlongPath {
    //Prepare the animation - we use keyframe animation for animations of this complexity
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    pathAnimation.calculationMode = kCAAnimationCubic;
    [pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];

    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 7.0;

    //Lets loop continuously for the demonstration
    pathAnimation.repeatCount = 1;

    //Setup the path for the animation - this is very similar as the code the draw the line
    //instead of drawing to the graphics context, instead we draw lines on a CGPathRef
    CGPoint endPoint = CGPointMake(endpointx, endpointy);
    CGMutablePathRef curvedPath = CGPathCreateMutable();

    CGPathMoveToPoint(curvedPath, NULL,startpointx,startpointy);


   // CGPathMoveToPoint(curvedPath, NULL, 10, 10);
     CGPathAddQuadCurveToPoint(curvedPath,NULL, controlpointx, controlpointy, endpointx, endpointy);




    //Now we have the path, we tell the animation we want to use this path - then we release the path
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);

    //We will now draw a circle at the start of the path which we will animate to follow the path
    //We use the same technique as before to draw to a bitmap context and then eventually create
    //a UIImageView which we add to our view
    UIGraphicsBeginImageContext(CGSizeMake(20,20));
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //Set context variables
    CGContextSetLineWidth(ctx, 1.5);
    CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    //Draw a circle - and paint it with a different outline (white) and fill color (green)
    CGContextAddEllipseInRect(ctx, CGRectMake(1, 1, 18, 18));
    CGContextDrawPath(ctx, kCGPathFillStroke);
    UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *circleView = [[UIImageView alloc] initWithImage:circle];
    circleView.frame = CGRectMake(1, 1, 20, 20);
    [throwingView addSubview:circleView];


    //Add the animation to the circleView - once you add the animation to the layer, the animation starts
    [circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];


}
Was it helpful?

Solution

I believe you have two problems here:

  • Check the collision
  • Find out where to verify the collision

Checking the collision

A CALayer has a a model layer and a presentation layer. The presentation layer give you the visual information you need. Basically this layer is responsible to have the information about where the layer is on the screen. You can get it doing: circleView.layer.presentationLayer

Find out where to verify the collision

You can do this with a NSTimer that runs every 1/60 seconds. However, this is not a good solution because the NSTimer only guarantees the minimum time it will take to perform a selector. This way you can have cases where you are going to check your colision after the object already colided.

You can, however, use CADisplayLink. This way you will get a call before the layer is render in the screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top