Basic Shapes

Episode #281 | 7 minutes | published on July 7, 2017 | Uses swift-3.0, Xcode-8.3
Subscribers Only
Learn how to create basic shapes using Core Graphics. We’ll start by creating a playground that we can use to quickly see the results of our work.

Basic Playground Setup

import UIKit
import PlaygroundSupport

final class CustomView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }

}

let view = CustomView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))

PlaygroundPage.current.liveView = view

Getting the context

    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
    }

Drawing Basic Shapes

    context.setFillColor(UIColor.red.cgColor)
    context.fill(CGRect(x: 10, y: 10, width: 50, height: 50))

    context.setStrokeColor(UIColor.blue.cgColor)
    context.stroke(CGRect(x: 20, y: 80, width: 50, height: 50))

    context.fillEllipse(in: CGRect(x: 20, y: 80, width: 50, height: 50))
blog comments powered by Disqus