我正在尝试使用跟随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"];
答案 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个小时。