CAShapeLayer
CAShapeLayer represents a shape layer: it draws a path (UIBezierPath). It first fills the area enclosed by the path, then strokes along the path. Line charts, pie charts, and progress rings are all drawn with it.
It is a layer, and like any other sublayer you set its frame yourself and add it under some view's layer. It does not resize itself to fit the path: coordinates in the path are points in the layer's own coordinate system, with the origin at the layer's top-left corner.
Note:
fillColordefaults to opaque black, andstrokeColordefaults tonull. When you want just a line, setfillColortonulland givestrokeColora color.
CAShapeLayer fires no events. Its properties are changed only by the app's code, so there are no change notifications.
Inheritance: EventTarget → NSObject → CALayer → CAShapeLayer
For drawing polylines, pie slices, progress rings, and dashed lines, see the guide Drawing rounded corners, shadows, gradients, and shapes with layers.
Constructor
Creates a shape layer. Its size is 0 × 0 pt and it has no path.
Instance properties
Also inherits the properties of CALayer; commonly used ones are frame and masksToBounds.
| Property | Description |
|---|---|
path | The path to draw |
Fill:
| Property | Description |
|---|---|
fillColor | Fill color |
fillRule | Which areas count as inside the path |
Stroke:
| Property | Description |
|---|---|
strokeColor | Line color |
lineWidth | Line width |
lineCap | Shape of line ends |
lineJoin | Shape of corners |
miterLimit | Length limit for sharp corners |
strokeStart | Where along the path the stroke starts |
strokeEnd | Where along the path the stroke ends |
lineDashPattern | How dashes are drawn |
lineDashPhase | Where in the dash pattern drawing starts |
Instance methods
CAShapeLayer has no methods of its own. It also inherits the methods of CALayer.
Examples
A polyline
const card = new UIView();
card.frame = new CGRect(20, 100, 300, 160);
const path = new UIBezierPath();
path.moveToPoint(new CGPoint(24, 120));
path.addLineToPoint(new CGPoint(96, 60));
path.addLineToPoint(new CGPoint(168, 96));
const line = new CAShapeLayer();
line.path = path;
line.fillColor = null;
line.strokeColor = 'systemBlue';
line.lineWidth = 3;
line.frame = card.bounds;
card.layer.addSublayer(line);Effect: a 3 pt blue polyline with two segments appears on the card.
Corresponding iOS API
| ObjC | JS |
|---|---|
[CAShapeLayer layer] | new CAShapeLayer() |
shape.path = path.CGPath | shape.path = path |
shape.fillColor = UIColor.clearColor.CGColor | shape.fillColor = null |
shape.fillRule = kCAFillRuleEvenOdd | shape.fillRule = 'evenOdd' |
shape.lineCap = kCALineCapRound | shape.lineCap = 'round' |
shape.lineJoin = kCALineJoinBevel | shape.lineJoin = 'bevel' |
shape.lineDashPattern = @[@4, @2] | shape.lineDashPattern = [4, 2] |
See also
UIBezierPath: the type ofpath, and how to draw pathsCALayer: the parent class; sublayer sizes and colors on layersCABasicAnimation: animatingstrokeEndso the line draws itself bit by bit- Drawing rounded corners, shadows, gradients, and shapes with layers