Comparing two CGRects
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
CGRect comparison appears in many UIKit tasks, including collision checks, layout verification, hit testing, and animation logic. The right comparison method depends on what you mean by match, overlap, or containment. This guide covers equality, intersection, containment, and tolerance-based comparisons with practical Swift examples.
Equality Comparison
If you need exact geometry equality, compare CGRect values directly.
Exact equality is useful in deterministic layout code, but small floating-point differences can make equal-looking rectangles fail this check.
Compare with Tolerance for Floating Values
For animation and transformed coordinates, use epsilon tolerance.
Tolerance checks are more stable when values come from transforms, scaling, or interpolation.
Intersection and Overlap Checks
To detect whether rectangles overlap, use intersects.
To compute the overlap region, use intersection.
This is useful for clipping, visible-area calculations, and collision response.
Containment Checks
Containment answers whether one rectangle fully includes another.
You can also test whether a point lies inside a rectangle.
Containment checks are common in gesture handling and drag constraints.
Coordinate Space Matters
Two rectangles from different coordinate systems are not directly comparable. Convert views into a shared coordinate space before comparing.
Always normalize coordinate space first for reliable overlap and equality logic.
Practical Layout Debug Pattern
When debugging layout regressions, print key rectangle values and compare expected against actual.
Pair this with assertions in unit tests for deterministic, repeatable UI geometry validation.
Use Integral Rectangles for Pixel Alignment
On some UI paths, tiny fractional coordinates can cause blurry rendering. If your comparison depends on visual alignment, normalize rectangles first.
Comparing normalized rectangles can reduce false mismatches in snapshot tests and layout assertions. Keep in mind that integral conversion changes geometry, so use it only for rendering-oriented checks, not physical collision logic.
Common Pitfalls
- Using exact equality for floating-point values that should use tolerance.
- Comparing rectangles from different coordinate spaces.
- Confusing overlap with containment in collision logic.
- Ignoring zero-width or zero-height rectangles that can affect intersection checks.
- Assuming transformed view bounds match displayed frame without conversion.
Summary
- Use direct equality only when exact matches are expected.
- Use epsilon-based comparison for float-heavy geometry.
- Use
intersectsandintersectionfor overlap logic. - Use
containsfor full enclosure checks. - Normalize coordinate spaces before any rectangle comparison.
Related reading
- Comparison between Corona, Phonegap, Titanium
- Comparison of Android networking libraries OkHTTP, Retrofit, and Volley
- Completion block for popViewController
- Completion handler for UINavigationController pushViewControlleranimated?
- Computed read-only property vs function in Swift
- Concurrent vs serial queues in GCD
- Concurrent vs serial queues in GCD
- Conditional Binding if let error – Initializer for conditional binding must have Optional type
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.