Created
July 9, 2015 14:03
-
-
Save felixvisee/3b945e18f7373c1915b8 to your computer and use it in GitHub Desktop.
A `Box`-like struct returning a value based on the current traits of a view.
This file contains 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
public class View: UIView { | |
public override var traitCollection: UITraitCollection { | |
return UITraitCollection(traitsFromCollections: [ | |
UITraitCollection(horizontalSizeClass: .Compact), | |
UITraitCollection(verticalSizeClass: .Compact) | |
]) | |
} | |
} | |
let view = View() | |
let variable = Variable(view: view, definition: [ | |
(UITraitCollection(horizontalSizeClass: .Compact), 1), | |
(UITraitCollection(horizontalSizeClass: .Unspecified), 5) | |
]) | |
variable.value |
This file contains 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
import UIKit | |
public struct Variable<T> { | |
public var view: UIView | |
public var definition: [(traitCollection: UITraitCollection, value: T)] | |
public var value: T? { | |
for (traitCollection, value) in definition { | |
if view.traitCollection.containsTraitsInCollection(traitCollection) { | |
return value | |
} | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Much easier: