2011-01-23 68 views
5

我沿着圓圈使用遵循CGPathAddEllipseInRect一個CAKeyframeAnimation動畫一個UIView。然而,視圖始終似乎始於同一個地方,無論它最初放置在哪個框架中。是否有某種方法可以調整路徑上視圖的起始位置?設置起始點CGAffineTransform

謝謝!

CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
myAnimation.fillMode = kCAFillModeForwards; 
myAnimation.repeatCount = 5; 
myAnimation.calculationMode = kCAAnimationPaced; 
myAnimation.duration = 10.0; 

CGMutablePathRef animationPath = CGPathCreateMutable(); 

CGPathAddEllipseInRect(animationPath, NULL, rect); 

myAnimation.path = animationPath; 
CGPathRelease(animationPath); 

[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"]; 

回答

1

我不認爲有可能爲CGPathAddEllipseInRect設置起始點。 (至少我是沒有成功)的

而不是使用:CGPathAddEllipseInRect, 你應該使用:CGPathAddArc!例如:

CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
myAnimation.fillMode = kCAFillModeForwards; 
myAnimation.repeatCount = 5; 
myAnimation.calculationMode = kCAAnimationPaced; 
myAnimation.duration = 10.0; 

CGMutablePathRef animationPath = CGPathCreateMutable(); 

NSInteger startVal = M_PI/2; 

//seventh value (end point) must be always 2*M_PI bigger for a full circle rotation 
CCGPathAddArc(animationPath, NULL, 100, 100, 100, startVal, startVal + 2*M_PI, NO); 

myAnimation.path = animationPath; 
CGPathRelease(animationPath); 

[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"]; 

將從底部開始 - 順時針旋轉整圈。更改startVal值以更改旋轉開始位置。 (完整旋轉是2 * M_PI)。

0

以前的答案是正確的,工作的,但應該有

CGFloat startVal = M_PI/2; 

,而不是

NSInteger startVal = M_PI/2; 

否則,M_PI將四捨五入到整數,你將不能夠實現精確的角度。 P.S.以前的回答評論聲譽較低,不好意思。