iOS Drawing - kgleong/software-engineering GitHub Wiki

Drawing in iOS

Paths, Strokes, and Fills

The code below draws a circle with a vertical line down the center.

import UIKit

@IBDesignable

class PushButtonView: UIButton {
    override func draw(_ rect: CGRect) {
        // Circle
        let path = UIBezierPath(ovalIn: rect)
        UIColor.red.setFill()
        path.fill()

        // Vertical Line
        let linePath = UIBezierPath()
        let lineLength = bounds.width * 0.8

        // Position cursor at a CGPoint
        linePath.move(to: CGPoint(x: bounds.width/2.0, y: bounds.height/2.0 - lineLength/2.0))

        // Draw line to a CGPoint
        linePath.addLine(to: CGPoint(x: bounds.width/2.0, y: bounds.height/2.0 + lineLength/2.0))

        // Configure stroke and draw
        linePath.lineWidth = 3.0
        UIColor.white.setStroke()
        linePath.stroke()
    }
}

Frame vs Alignment Rectangles

References

Layers

References

References