Установка начальной позиции 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 ответ:
Используйте
CGPathAddArc
вместоCGPathAddEllipseInRect
. Например:Обратите внимание, что если вы хотите, чтобы он двигался через 12 часов, параметрCGFloat radiansForHour(CGFloat hour) { return 2 * M_PI * (hour - 3) / 12; } ... CGPathAddArc(curvedPath, NULL, 160, 270, 100, radiansForHour(11), radiansForHour(12 + 11), NO);
endAngle
должен быть на 12 часов больше, чем параметрstartAngle
.