Skip to content

Instantly share code, notes, and snippets.

@yurrriq
Created May 5, 2017 18:07
Show Gist options
  • Save yurrriq/a0a875425f31697b0f179bda3f12d907 to your computer and use it in GitHub Desktop.
Save yurrriq/a0a875425f31697b0f179bda3f12d907 to your computer and use it in GitHub Desktop.
Subtract a CGRect from another
import CoreGraphics
public extension CGRect {
func subtract(_ subtrahend: CGRect, edge: CGRectEdge) -> CGRect {
guard intersects(subtrahend) else { return self }
let intersectSize: CGSize = intersection(subtrahend).size
let distance: CGFloat = (edge == .minXEdge || edge == .maxXEdge)
? intersectSize.width
: intersectSize.height
return divided(atDistance: distance, from: edge).remainder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment