How do I create a circle with CALayer?
Change radius and fillColor as you want. :)
import Foundationimport UIKitclass CircleLayerView: UIView { var circleLayer: CAShapeLayer! override func draw(_ rect: CGRect) { super.draw(rect) if circleLayer == nil { circleLayer = CAShapeLayer() let radius: CGFloat = 150.0 circleLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 2.0 * radius, height: 2.0 * radius), cornerRadius: radius).cgPath circleLayer.position = CGPoint(x: self.frame.midX - radius, y: self.frame.midY - radius) circleLayer.fillColor = UIColor.blue.cgColor self.layer.addSublayer(circleLayer) } }}
The rect
being passed into drawRect
is the area that needs to be updated, not the size of the drawing. In your case, you would probably just ignore the rect being passed in and set the circle to the size you want.
//// Oval Drawing var ovalPath = UIBezierPath(ovalInRect: CGRectMake(0, 0, 300, 300)) UIColor.whiteColor().setFill() ovalPath.fill()