iPhone - How to make a circle path for a CAKeyframeAnimation?

后端 未结 2 1620
[愿得一人]
[愿得一人] 2021-01-14 16:42

I\'m trying to make an key frame animation that lets a image view fly around a circle. I\'m using CGPathAddCurveToPoint to make the key frame points. The proble

相关标签:
2条回答
  • 2021-01-14 16:56

    Would this do the trick?

      CAKeyframeAnimation* circlePathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"center"];
    
      CGMutablePathRef circularPath = CGPathCreateMutable();
      CGRect pathRect = ...; // some rect you define. A circle has equal width/height; radius is half the values you specify here
      CGPathAddEllipseInRect(circularPath, NULL, pathRect);
      spinAnimation.path = circularPath;
      CGPathRelease(circularPath);
    

    You will notice the keypath of the animation is the center property of the UIView you wish to make fly around in a circle.

    0 讨论(0)
  • 2021-01-14 16:57
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
    CGPathAddQuadCurveToPoint(path, NULL,middlePoint.x, middlePoint.y,endPoint.x,endPoint.y);
    
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.path = path;
    [pathAnimation setCalculationMode:kCAAnimationCubic];
    [pathAnimation setFillMode:kCAFillModeForwards];
    pathAnimation.duration = 2.0;
    
    [viewTobemoved.layer addAnimation:pathAnimation forKey:nil];
    

    This animation uses only one bezier point and makes a soft circle from beginning point to end point.If you want to define the curve exactly by also defining the radius of the circle you can refer to the following link

    http://blog.shoguniphicus.com/2011/05/19/keyframe-animation-ios-cakeyframeanimation-objective-c/

    0 讨论(0)
提交回复
热议问题