@@ -91,6 +91,13 @@ extension CGSize {
91
91
92
92
// MARK: -
93
93
94
+ public enum DirectionalEdge {
95
+ case top
96
+ case bottom
97
+ case leading
98
+ case trailing
99
+ }
100
+
94
101
extension CGRect {
95
102
96
103
/// Initialize a CGRect with bounding coordinates (always with non-negative size).
@@ -141,10 +148,35 @@ extension CGRect {
141
148
public func offset( by offset: UIOffset ) -> CGRect {
142
149
return CGRect ( origin: origin. offset ( by: offset) , size: size)
143
150
}
144
-
151
+
152
+ /// Divides the receiver in two.
153
+ ///
154
+ /// - parameter edge: The edge from which the amount is interpreted.
155
+ /// - parameter amount: The size of the slice (absolute).
156
+ /// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of
157
+ /// the receiver reduced by `amount`).
158
+ public func slice(
159
+ from edge: DirectionalEdge ,
160
+ amount: CGFloat ,
161
+ in layoutDirectionProvider: LayoutDirectionProviding
162
+ ) -> ( slice: CGRect , remainder: CGRect ) {
163
+ switch ( edge, layoutDirectionProvider. effectiveUserInterfaceLayoutDirection) {
164
+ case ( . top, _) :
165
+ slice ( from: . minYEdge, amount: amount)
166
+ case ( . bottom, _) :
167
+ slice ( from: . maxYEdge, amount: amount)
168
+ case ( . leading, . leftToRight) , ( . trailing, . rightToLeft) :
169
+ slice ( from: . minXEdge, amount: amount)
170
+ case ( . trailing, . leftToRight) , ( . leading, . rightToLeft) :
171
+ slice ( from: . maxXEdge, amount: amount)
172
+ @unknown default :
173
+ fatalError ( " Unknown user interface layout direction " )
174
+ }
175
+ }
176
+
145
177
/// Divides the receiver in two.
146
178
///
147
- /// - parameter from : The edge from which the amount is interpreted.
179
+ /// - parameter edge : The edge from which the amount is interpreted.
148
180
/// - parameter amount: The size of the slice (absolute).
149
181
/// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of
150
182
/// the receiver reduced by `amount`).
0 commit comments