如何在UIImageView中的某个位置定位旋转的CAShapeLayer?

时间:2012-12-29 19:51:48

标签: objective-c ios core-animation cashapelayer

我有以下代码,使椭圆绕其中心旋转。我希望能够将它放在UIImageView的某个位置(比如视图的中间)。

如果有人可以帮我解决这个问题,我真的很感激。

-(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"];

}

1 个答案:

答案 0 :(得分:0)

缺少一件:

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