2

我正在尝试UIView使用 aCAKeyframeAnimation沿用创建的路径为a 制作动画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"];
4

1 回答 1

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.

于 2011-10-26T19:53:01.460 回答