Question

i am trying to create an animation, in which a circle rotates around the center of the view.

I want the circle to appear under the location of the tap gesture, therefore i calculate distance and angle between the center of the view and the circle.

I then set these values inside the CGPathAddArc for the animation path.

This seem to work rather well, but once in a while, after having performed a whole rotation, the circle will stop for a short while, before restarting. By playing around, i found out that this must be related to the start and stop angles of the CGPathAddArc function, as setting standard values like -M_PI to M_PI will not cause the issue.

My code is:

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.fillMode = kCAFillModeRemoved;
    pathAnimation.removedOnCompletion = YES;
    pathAnimation.duration = 4.0;
    pathAnimation.repeatCount = HUGE_VALF;

    CGMutablePathRef curvedPath = CGPathCreateMutable();



    //get the angle and dinstance of location in relation to center of view (mathematics)
    CGFloat angle = atan2f(location.y -self.view.center.y, location.x - self.view.center.x);
    CGFloat distance = hypotf(location.x - self.view.center.x, location.y - self.view.center.y);
    NSLog(@"angle: %f", angle);
    CGPathAddArc(curvedPath, NULL, self.view.center.x, self.view.center.y, distance, angle, angle + 2 * M_PI, NO);

    //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);

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

location holds the coordinates of the touch event, the circle object is created before this code snippet. Any help would be greatly appreciated.

Was it helpful?

Solution

Adding this would smooth the animation out evenly:

pathAnimation.calculationMode = kCAAnimationPaced;

Just tested it.

Nice calculation, by the way.

Hope this helps.

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