Skip to content

Instantly share code, notes, and snippets.

@loromits
Created November 9, 2017 11:40
Show Gist options
  • Save loromits/9a0f121282ebf01f68850cbcf4ab19ae to your computer and use it in GitHub Desktop.
Save loromits/9a0f121282ebf01f68850cbcf4ab19ae to your computer and use it in GitHub Desktop.
Extend SnapKit with struct that is able to switch two sets of constraints
import SnapKit
struct ConstraintSwitcher {
private var initialActive = [Constraint]()
private var initialInactive = [Constraint]()
private(set) var isInitialActive = true
mutating func append(_ constraint: Constraint, active: Bool) {
if active == isInitialActive {
initialActive.append(constraint)
} else {
initialInactive.append(constraint)
}
}
mutating func switchInitial(active: Bool) {
if active {
initialActive.forEach { $0.deactivate() }
initialInactive.forEach { $0.activate() }
} else {
initialInactive.forEach { $0.deactivate() }
initialActive.forEach { $0.activate() }
}
isInitialActive = active
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment