设置遵循椭圆路径的CAKeyframeAnimation的起始位置

时间:2011-10-26 18:14:45

标签: objective-c cocoa-touch uiview core-animation cakeyframeanimation

我正在尝试使用跟随UIView创建的路径的CAKeyframeAnimation为圆圈中的CGPathAddEllipseInRect设置动画,我已经设置好了。但是,有没有办法设置动画的开始位置?我的代码是:

//set up animation
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 10.0;
pathAnimation.repeatCount = 1000;
CGMutablePathRef curvedPath = CGPathCreateMutable();

//path as a circle
CGRect bounds = CGRectMake(60,170,200,200);
CGPathAddEllipseInRect(curvedPath, NULL, bounds);

//tell animation to use this path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

//add subview
circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];    
[testView addSubview:circleView];

//animate
[circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];

1 个答案:

答案 0 :(得分:7)

使用CGPathAddArc代替CGPathAddEllipseInRect。例如:

CGFloat radiansForHour(CGFloat hour)
{
    return 2 * M_PI * (hour - 3) / 12;
}

...
    CGPathAddArc(curvedPath, NULL, 160, 270, 100, radiansForHour(11), radiansForHour(12 + 11), NO);

请注意,如果您希望它移动12小时,则endAngle参数必须比startAngle多12个小时。