CAShapeLayer is a CALayer subclass that let you draw various shapes on screen, from the very simple to the most complex.
First at all,
let photoLayer = CALayer()
let circleLayer = CAShapeLayer()
let maskLayer = CAShapeLayer()
the code above creates three layers,one CALayer,two CAShapeLayers
photoLayer.contents = image.CGImage
assign an image to photoLayer.contents , the image will fill into photoLayer.
photoLayer.frame = CGRect(
x: (bounds.size.width - image.size.width + lineWidth)/2,
y: (bounds.size.height - image.size.height - lineWidth)/2,
width: image.size.width,
height: image.size.height)
//Draw the circle
circleLayer.path = UIBezierPath(ovalInRect: bounds).CGPath
circleLayer.strokeColor = UIColor.whiteColor().CGColor
circleLayer.lineWidth = lineWidth
circleLayer.fillColor = UIColor.clearColor().CGColor
//Size the layer
maskLayer.path = circleLayer.path
maskLayer.position = CGPoint(x: 0.0, y: 10.0)
layer.addSublayer(photoLayer);
photoLayer.mask = maskLayer;
layer.addSublayer(circleLayer);
We should set a CALayer’s frame, and set a CAShapeLayer’s path instead .