我正在创建这样的简单Arc类型图表:
- (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];
以下是用于旋转的功能,因此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的模拟设备上测试)。
expected result on newer devices
在早期设备上失败
我找到的唯一解决方法是使用:
self.transform = CGAffineTransformMakeRotation(-1.5707965);
关于包含UIView