旋转CAShapeLayer

时间:2016-11-07 19:46:56

标签: objective-c cashapelayer

我正在尝试将CAShapeLayer旋转到位

             UIBezierPath *aPath = [UIBezierPath bezierPath];
             CGFloat originx = (97.5+250);
             CGFloat originy = (205+250);
             [aPath moveToPoint:CGPointMake(originx,originy)];
             [aPath addArcWithCenter:CGPointMake(originx+15, originy-30) radius:15 startAngle:DEGREES_TO_RADIANS(180) endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];
             [aPath addLineToPoint:CGPointMake(originx+30,originy)];
             CAShapeLayer *shapeLayer = [CAShapeLayer layer];
             shapeLayer.anchorPoint = CGPointMake(originx, originy);
             shapeLayer.path = [aPath CGPath];
             shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
             shapeLayer.lineWidth = 3.0;
             shapeLayer.fillColor = [[UIColor blueColor] CGColor];
             [aPath closePath];
             shapeLayer.transform = CATransform3DMakeRotation(310 * M_PI/180, 0.0, 0.0, 1.0);
             [self.view.layer addSublayer:shapeLayer];

我想围绕形状本身内的轴旋转形状(上面的代码使我的小形状围绕距离实际中心几百个像素的轴旋转。

我觉得这应该用这样的锚点来控制:

shapeLayer.anchorPoint = CGPointMake(originx, originy); 

但它似乎在代码中什么都不做。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

anchorPoint相对于图层的边界。您无法使用定位点来放置图层。相反,您可以将形状相对于零点放置,旋转它,然后将其移动到所需的点。旋转和平移是通过变换完成的。