CAShapeLayer

来源:互联网 发布:炮姐脸型数据 编辑:程序博客网 时间:2024/05/21 08:41

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 .

0 0
原创粉丝点击