2012-12-29 43 views
0

我有下面的代碼,使其中心的橢圓旋轉。我希望能夠將其定位在UIImageView的某個位置(比如視圖的中間位置)。如何在UIImageView的特定位置放置旋轉的CAShapeLayer?

如果有人能幫助我解決這個問題,我將非常感激。

-(void)drawCircle 
{ 
CGMutablePathRef circlePath = CGPathCreateMutable(); 
int radius = 400; 
CGRect circleRect = CGRectMake(CENTER_Y-radius*0.7, CENTER_X-radius, 1.4*radius, 2*radius); 
float midX = CGRectGetMidX(circleRect); 
float midY = CGRectGetMidY(circleRect); 

CGAffineTransform transform = CGAffineTransformMakeTranslation(-midX, -midY); 
CGPathAddEllipseInRect(circlePath ,&transform , circleRect); 

CAShapeLayer *circle = [[CAShapeLayer alloc] init]; 
circle.path = circlePath; 
circle.opacity = 0.1; 
circle.contentsScale = SCREEN_SCALE; 
circle.fillColor = [UIColor blueColor].CGColor; 

[aView.layer addSublayer:circle]; 
CABasicAnimation* theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
theAnimation.fromValue = 0; 
theAnimation.toValue = @(2*M_PI); 
theAnimation.duration=3.5; 
theAnimation.autoreverses=FALSE; 
theAnimation.repeatCount=HUGE_VALF; 
theAnimation.fillMode = kCAFillModeBoth; 
theAnimation.removedOnCompletion=FALSE; 
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
[circle addAnimation:theAnimation forKey:@"transform.rotation.z"]; 

} 

回答

0

一個缺失:

[circle setPosition:CGPointMake(CENTER_X, CENTER_Y)]; 
+0

是問題或實際問題的答案的一部分?這是你的問題的額外信息,你應該編輯問題,而不是在答案中發佈更多信息。 (編輯按鈕在您的問題的左下方) –

+0

這是我的問題的答案。 – GrAnD

+0

看起來你也忽略了設置你的形狀圖層的邊界。 '[circle setBounds:circleRect];'所以我很驚訝它顯示。 –