I am working on CAShapeLayer.And trying to draw non-linear path.I want to set frame to CAShapeLayer.So i can use CGPathGetPathBoundingBox
The following is a quote from the CALayer documentation:
The frame rectangle is position and size of the layer specified in the superlayer’s coordinate space. For layers, the frame rectangle is a computed property that is derived from the values in the bounds, anchorPoint and position properties. When you assign a new value to this property, the layer changes its position and bounds properties to match the rectangle you specified. The values of each coordinate in the rectangle are measured in points.
It looks like with the CAShapeLayer when setting the frame by itself it does't keep the origin values from the assigned CGRect, only the size. When you set the bounds the origin is kept and therefore displays in the proper location. You also need to set the position value to the center point of where you want you shape to be displayed:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100, (0), (M_PI_2), NO);
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100-50, (M_PI_2), (0), YES);
CGPathCloseSubpath(path);
CAShapeLayer* arcLayer = [CAShapeLayer layer];
arcLayer.path = path;
CGRect pathRect = CGPathGetPathBoundingBox(path);
arcLayer.bounds = pathRect;
arcLayer.position = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMidY(pathRect));
[self.layer addSublayer:arcLayer];