沿着圆形路径的UIView动画?

时间:2010-03-31 18:47:55

标签: iphone uiview core-animation uiimageview

我需要让我的小家伙(在UIIamgeView中)向前跳,它必须看起来很自然。我想知道有没有办法用CoreAnimation(beginAnimation / commitAnimation)做到这一点?

我可以通过在空中设置一个点,但运动看起来不自然:P

3 个答案:

答案 0 :(得分:14)

阅读Brad Larson的帖子,我最终得到了一个很好的功能,让我的图像跟随一个圆圈。

您需要导入QuartzCore框架:#import

以下是代码:

// 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"];

答案 1 :(得分:3)

创建CAKeyframeAnimation并为其path属性分配CGPath。

答案 2 :(得分:3)

为了跟进Ole的回答,my answerthis question提供了使用CAKeyframeAnimation执行此类曲面动画的代码。