问题
I'm creating simple Arc type chart like this:
- (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];
And below is function used for making rotation, so that the 0 points north.
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);
}
Now tricky part:
Why is that only works on devices (5s, 6) and simulators (tested on range of simulated devices with iOS from 8.3 to 9.3 ) starting from iPhone 5s.
expected result on newer devices
On earlier devices it fails
Failed rotation
The only workaround I found is use:
self.transform = CGAffineTransformMakeRotation(-1.5707965);
on containing UIView
来源:https://stackoverflow.com/questions/36613165/cashapelayer-doesnt-rotate-with-cgaffinetransformrotate-on-devices-older-than-i