UIView animation along a round path?

前端 未结 3 1158
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 14:05

I need to make my little guy (in a UIIamgeView) jump forward and it has to look natural. I want to know is there a way to do it with CoreAnimation (beginAnimation/commitAnim

相关标签:
3条回答
  • 2020-12-15 14:29

    To follow up on Ole's answer, my answer to this question provides code to perform such a curved animation using a CAKeyframeAnimation.

    0 讨论(0)
  • 2020-12-15 14:32

    Reading Brad Larson's post, I ended up with a nice function to make my image follow a circle.

    You need to import QuartzCore framework: #import

    Here is the code:

    // Set up path movement
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.repeatCount = INFINITY;
    //pathAnimation.rotationMode = @"auto";
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnimation.duration = 5.0;
    
    // Create a circle path
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGRect circleContainer = CGRectMake(0, 0, 400, 400); // create a circle from this square, it could be the frame of an UIView
    CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
    
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);
    
    [self.imageView.layer addAnimation:pathAnimation forKey:@"myCircleAnimation"];
    
    0 讨论(0)
  • 2020-12-15 14:50

    Create a CAKeyframeAnimation and assign a CGPath to its path property.

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