Draw Ellipse in Swift using UIBezierPath
I believe this is what you are asking for:
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(160, 160, 240, 320))
UIColor.grayColor().setFill()
ovalPath.fill()
For complex shapes I would suggest checking out PaintCode. It creates the swift code for you as you draw your shapes on screen (it has personally saved me a lot of time in the past).
Edit:
import Foundation
import UIKit
class CustomOval: UView {
override func drawRect(rect: CGRect)
{
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(0, 0, 240, 320))
UIColor.grayColor().setFill()
ovalPath.fill()
}
}
Then :
var exampleView = CustomOval()
And then position it with constraints etc. afterwards.
Swift 4
var ovalPath = UIBezierPath(ovalIn: CGRect(x: 160, y: 160, width: 240, height: 320))
UIColor.gray.setFill()
ovalPath.fill()
let shapeLayer = CAShapeLayer()
shapeLayer.path = ovalPath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.lineWidth = 5.0
self.layer.addSublayer(shapeLayer)
Write this code in override func drawRect(rect: CGRect) in Custom UIView, then edit as you want.
override func drawRect(rect: CGRect) {
let ellipsePath = UIBezierPath(ovalInRect: CGRectMake(100, 100, 100, 200))
let shapeLayer = CAShapeLayer()
shapeLayer.path = ellipsePath.CGPath
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.strokeColor = UIColor.blueColor().CGColor
shapeLayer.lineWidth = 5.0
self.layer.addSublayer(shapeLayer)
}