9.12 · Rectangle containment and moving a point between coordinate spaces
The small geometry exercises that interviews use: does one rectangle fit inside another, and is this touch inside that button once you account for the view’s position and scrolling?
Mental model. A point or rectangle only has meaning together with the coordinate space it is measured in; convert first, then compare.
Containment and spaces
Normalise rectangles, then convert between views.
- Write a function that checks whether one CGRect fully contains another, then explain how you decide whether a touch lies inside a view that sits inside a scrolled parent.
Containment is four comparisons: the inner rectangle’s minX and minY must be at least the outer’s, and its maxX and maxY at most the outer’s. Two details make…
More in Layout, rendering & animation
- 9.1 · Auto Layout, intrinsic size, and when layoutSubviews and draw run
- 9.2 · From layout to the GPU, and the two layer trees
- 9.3 · Offscreen passes, rasterization, blending, shadows and masks
- 9.4 · Implicit and explicit animation, UIView animation, and animators
- 9.5 · Core Graphics, UIBezierPath and draw(_:)
- 9.6 · TextKit 1 and 2, and Text in SwiftUI
- 9.7 · Frame budgets, ProMotion, hitches and hangs
- 9.8 · Margins, stack views, guides, constraint ownership, the layout cycle and measuring
- 9.9 · Coordinate conversion, 3D transforms and layer contents
- 9.10 · How UIView animation really works, springs, timing and display links
- 9.11 · Transform order, fill rules, text measurement, text containers and bitmaps