create a two-colour view with the colours of the turkish flag in swift

Here's how you can create a two-color view in Swift with the colors of the Turkish flag:

main.swift
import UIKit

class TurkishFlagView: UIView {

    override func draw(_ rect: CGRect) {
        let halfWidth = rect.width/2
        
        // Draw the red background
        UIColor.red.setFill()
        UIRectFill(CGRect(x: 0, y: 0, width: halfWidth, height: rect.height))
        
        // Draw the white background
        UIColor.white.setFill()
        UIRectFill(CGRect(x: halfWidth, y: 0, width: halfWidth, height: rect.height))
        
        // Draw the crescent and star
        let path = UIBezierPath()
        path.addArc(withCenter: CGPoint(x: halfWidth/2, y: rect.height/2), radius: halfWidth/2, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true)
        path.move(to: CGPoint(x: halfWidth * 3/2, y: rect.height/2))
        let starPoints: [CGPoint] = [
            CGPoint(x: halfWidth * 3/2 - 10, y: rect.height/2 - 10),
            CGPoint(x: halfWidth * 3/2 + 10, y: rect.height/2 + 10),
            CGPoint(x: halfWidth * 3/2 - 15, y: rect.height/2),
            CGPoint(x: halfWidth * 3/2 + 15, y: rect.height/2),
            CGPoint(x: halfWidth * 3/2 - 10, y: rect.height/2 + 10),
        ]
        path.addLines(between: starPoints)
        path.close()
        UIColor.white.setFill()
        path.fill()
    }

}

// To use the TurkishFlagView in your UIViewController:
let flagView = TurkishFlagView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
view.addSubview(flagView)
1419 chars
38 lines

This will create a view with a red background on the left half of the view and a white background on the right half of the view, with a crescent and star in the center. This matches the colors and design of the Turkish flag.

gistlibby LogSnag