Created
October 25, 2017 16:22
-
-
Save davidbjames/52f47010610c2a1d4b345ecfe24c9e06 to your computer and use it in GitHub Desktop.
Example of switch contains.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code via Olivier Halligon and Nate Cook | |
struct ContainmentMatcher<T: Hashable> { | |
let set: Set<T> | |
static func ~=(lhs: ContainmentMatcher<T>, rhs: Set<T>) -> Bool { | |
return rhs.isSuperset(of: lhs.set) | |
} | |
} | |
func contains<T>(_ set: Set<T>) -> ContainmentMatcher<T> { | |
return ContainmentMatcher(set: set) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ..part of a larger thing.. | |
/// Pin item to the sides of the parent or reference item with optional insets. | |
/// Specifying opposing sides will flex width/height. | |
func pin(to sides:Layout.Sides, with insets:InsetGroup) { | |
// .. | |
let topInset = insets.value(for: .top) | |
let bottomInset = insets.value(for: .bottom) | |
let leadingInset = insets.value(for: .leading) | |
let trailingInset = insets.value(for: .trailing) | |
var x:CGFloat! = item.frame.origin.x | |
var y:CGFloat! = item.frame.origin.y | |
var width:CGFloat! = item.frame.width | |
var height:CGFloat! = item.frame.height | |
let _edges = Set(sides.edges) | |
switch _edges { | |
case contains([.top, .bottom]) : | |
// pin top and bottom, flex height | |
y = topInset | |
height = referenceBounds.height - topInset - bottomInset | |
case contains([.top]) : | |
// pin top, maintain height | |
y = topInset | |
case contains([.bottom]) : | |
// pin bottom, maintain height | |
y = referenceBounds.height - height - bottomInset | |
default : break | |
} | |
switch _edges { // TODO support layout direction flip | |
case contains([.leading, .trailing]) : | |
// pin leading/trailing, flex width | |
x = leadingInset | |
width = referenceBounds.width - leadingInset - trailingInset | |
case contains([.leading]) : | |
// pin leading, maintain width | |
x = leadingInset | |
case contains([.trailing]) : | |
// pin trailing, maintain width | |
x = referenceBounds.width - width - trailingInset | |
default : break | |
} | |
let newFrame = CGRect(x:x, y:y, width:width, height:height) | |
// .. update frame | |
// .. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment