在iPhone 5s以上的设备上,CAShapeLayer不会随CGAffineTransformRotate旋转

我正在创build简单的弧形图表,如下所示:

- (void)drawRect:(CGRect)rect { CGFloat lineWidth = 18.5f; CGFloat margin = 11; CAShapeLayer *circleLayer = [CAShapeLayer layer]; [circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect: CGRectMake(margin + lineWidth/2, margin + lineWidth/2, CGRectGetWidth(rect) - margin * 2 - lineWidth, CGRectGetWidth(rect)- margin * 2 - lineWidth) ] CGPath]]; [circleLayer setStrokeColor:[_fillColor CGColor]]; [circleLayer setFillColor:[[UIColor clearColor] CGColor]]; [circleLayer setLineWidth:18.5f]; [circleLayer setLineCap:kCALineCapRound]; [circleLayer setStrokeEnd: _fill]; CGPathRef path = createPathRotatedAroundBoundingBoxCenter(circleLayer.path, -1.5707965); circleLayer.path = path; CGPathRelease(path); [[self layer] addSublayer:circleLayer]; 

下面是用于进行旋转的function,以便将0点指向北方。

 static CGPathRef createPathRotatedAroundBoundingBoxCenter(CGPathRef path, CGFloat radians) { CGRect bounds = CGPathGetBoundingBox(path); CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); CGAffineTransform transform = CGAffineTransformIdentity; transform = CGAffineTransformTranslate(transform, center.x, center.y); transform = CGAffineTransformRotate(transform, radians); transform = CGAffineTransformTranslate(transform, -center.x, -center.y); return CGPathCreateCopyByTransformingPath(path, &transform); } 

现在棘手的部分:

为什么只能在iPhone 5s开始的设备(5s,6)和模拟器(在从iOS 8.3到9.3的模拟设备上testing)上运行。

预期的结果在较新的设备上

在早期的设备上失败

旋转失败

我发现唯一的解决方法是使用:

 self.transform = CGAffineTransformMakeRotation(-1.5707965); 

包含UIView