import UIKit
class CustomShapeView: UIView {
var shouldDrawShape = false
override func draw(_ rect: CGRect) {
super.draw(rect)
guard shouldDrawShape else { return }
let width = rect.width
let height = rect.height
// Draw square base
let baseRect = CGRect(x: width * 0.2, y: height * 0.5, width: width * 0.6, height: height * 0.4)
let basePath = UIBezierPath(rect: baseRect)
UIColor.lightGray.setFill()
basePath.fill()
UIColor.black.setStroke()
basePath.lineWidth = 2
basePath.stroke()
// Draw triangular roof
let roofPath = UIBezierPath()
roofPath.move(to: CGPoint(x: width * 0.2, y: height * 0.5))
roofPath.addLine(to: CGPoint(x: width * 0.5, y: height * 0.2))
roofPath.addLine(to: CGPoint(x: width * 0.8, y: height * 0.5))
roofPath.close()
UIColor.lightGray.setFill()
roofPath.fill()
UIColor.black.setStroke()
roofPath.lineWidth = 2
roofPath.stroke()
}
}
class ViewController: UIViewController {
let shapeView = CustomShapeView()
let drawButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
shapeView.translatesAutoresizingMaskIntoConstraints = false
shapeView.backgroundColor = .white
view.addSubview(shapeView)
drawButton.setTitle("Draw Shape", for: .normal)
drawButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(drawButton)
NSLayoutConstraint.activate([
shapeView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
shapeView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40),
shapeView.widthAnchor.constraint(equalToConstant: 200),
shapeView.heightAnchor.constraint(equalToConstant: 200),
drawButton.topAnchor.constraint(equalTo: shapeView.bottomAnchor, constant: 20),
drawButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
drawButton.addTarget(self, action: #selector(drawShape), for: .touchUpInside)
}
@objc func drawShape() {
shapeView.shouldDrawShape = true
shapeView.setNeedsDisplay()
}
}We created a CustomShapeView subclass of UIView that overrides the draw(_:) method. It draws a house shape using UIBezierPath: a square base and a triangular roof. We use simple colors: light gray fill and black stroke for clear visibility.
We added a Boolean shouldDrawShape to control when to draw the shape. Initially, the shape is not drawn.
In the ViewController, we set up the layout with Auto Layout constraints. The button labeled "Draw Shape" triggers the drawing by setting shouldDrawShape to true and calling setNeedsDisplay() to refresh the view.
This approach keeps drawing logic inside the custom view and user interaction in the controller, which is a clean separation of concerns.