For循环中的iOS 7 CA层旋转

时间:2014-08-11 19:39:43

标签: ios ios7 calayer

它是纯粹的视觉效果所以我使用了CALayer。我想将每个部分分开旋转,但CALayer围绕Center旋转。目前这样做:

int r = 110;
int pieces = 19;

for (int i = 0; i < 19; i++)
{
    float x  = [UIScreen mainScreen].bounds.size.width/2 +  r * cos( (180/pieces * i) * M_PI/180);
    float y = [UIScreen mainScreen].bounds.size.width/2 + r * sin( (180/pieces * i) * M_PI/180);

    CALayer *sublayer  = [CALayer layer];
    sublayer.contents = (id) [UIImage imageNamed:@"hudDB"].CGImage;
    sublayer.frame = CGRectMake(x, y, 20, 15);

    sublayer.transform = CATransform3DMakeRotation((180/pieces) * M_PI/180), 0, 0, 1);

    [self.layer addSublayer:sublayer];
}

1 个答案:

答案 0 :(得分:1)

这是您需要的代码。 改变你的需求:)

- (void)createHalfCircle {

    self.container = [CALayer layer];


    CGFloat radius = 100;

    for (int i = 0; i <= 180;  i+=30) {

        CGFloat xPos = radius *  cos(i * ( M_PI / 180 ));
        CGFloat yPos = radius *  sin(i * ( M_PI / 180 ));

        CAShapeLayer *rectLayer = [CAShapeLayer layer];
        rectLayer.path = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, 20, 20)].CGPath;
        rectLayer.fillColor = [UIColor greenColor].CGColor;
        rectLayer.position = CGPointMake(xPos, yPos);

        [self.container addSublayer:rectLayer];

    }

    self.container.position = CGPointMake( self.frame.size.width / 2, self.frame.size.height / 2);
    [self.layer addSublayer:self.container];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (CAShapeLayer *rectShape in self.container.sublayers) {

        rectShape.transform = CATransform3DMakeRotation( M_PI, 0, 0, 1);

    }

}