Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for slicing from directional edges #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions Paralayout/GeometryAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ extension CGSize {

// MARK: -

public enum DirectionalEdge {
case top
case bottom
case leading
case trailing
}

extension CGRect {

/// Initialize a CGRect with bounding coordinates (always with non-negative size).
Expand Down Expand Up @@ -141,10 +148,35 @@ extension CGRect {
public func offset(by offset: UIOffset) -> CGRect {
return CGRect(origin: origin.offset(by: offset), size: size)
}


/// Divides the receiver in two.
///
/// - parameter edge: The edge from which the amount is interpreted.
/// - parameter amount: The size of the slice (absolute).
/// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of
/// the receiver reduced by `amount`).
public func slice(
NickEntin marked this conversation as resolved.
Show resolved Hide resolved
from edge: DirectionalEdge,
amount: CGFloat,
in layoutDirectionProvider: LayoutDirectionProviding
) -> (slice: CGRect, remainder: CGRect) {
switch (edge, layoutDirectionProvider.effectiveUserInterfaceLayoutDirection) {
case (.top, _):
slice(from: .minYEdge, amount: amount)
case (.bottom, _):
slice(from: .maxYEdge, amount: amount)
case (.leading, .leftToRight), (.trailing, .rightToLeft):
slice(from: .minXEdge, amount: amount)
case (.trailing, .leftToRight), (.leading, .rightToLeft):
slice(from: .maxXEdge, amount: amount)
@unknown default:
fatalError("Unknown user interface layout direction")
}
}

/// Divides the receiver in two.
///
/// - parameter from: The edge from which the amount is interpreted.
/// - parameter edge: The edge from which the amount is interpreted.
/// - parameter amount: The size of the slice (absolute).
/// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of
/// the receiver reduced by `amount`).
Expand Down
Loading