Episode #281

Basic Shapes

Series: Dive Into Core Graphics

7 minutes
Published on July 7, 2017

This video is only available to subscribers. Get access to this video and 490 others.

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))

This episode uses Swift 3.0, Xcode 8.3.

Comments are temporarily disabled.