CAShapeLayer doesn't rotate with CGAffineTransformRotate on devices older than iPhone 5s

♀尐吖头ヾ 提交于 2019-12-11 11:14:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!