From e713b9da5b5c8edd711647592821b1ff53112cdb Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Thu, 4 Aug 2016 10:46:25 +1200 Subject: [PATCH] =?UTF-8?q?Add=20API=E2=80=99s=20for=20updating=20offset/i?= =?UTF-8?q?nset/priorty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/Constraint.swift | 43 +++++++++++++++++++++++++-- Source/ConstraintConstantTarget.swift | 11 ++++--- Source/ConstraintInsetTarget.swift | 26 ++++++++++++++++ Source/ConstraintMakerEditable.swift | 23 ++------------ Source/ConstraintOffsetTarget.swift | 22 ++++++++++++++ 5 files changed, 95 insertions(+), 30 deletions(-) diff --git a/Source/Constraint.swift b/Source/Constraint.swift index 8836ae1..eb6387a 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -36,8 +36,16 @@ public class Constraint { private let to: ConstraintItem private let relation: ConstraintRelation private let multiplier: ConstraintMultiplierTarget - private var constant: ConstraintConstantTarget - private var priority: ConstraintPriorityTarget + private var constant: ConstraintConstantTarget { + didSet { + self.updateConstantAndPriorityIfNeeded() + } + } + private var priority: ConstraintPriorityTarget { + didSet { + self.updateConstantAndPriorityIfNeeded() + } + } private var installInfo: ConstraintInstallInfo? = nil // MARK: Initialization @@ -78,8 +86,37 @@ public class Constraint { self.deactivateIfNeeded() } + @discardableResult + public func update(offset: ConstraintOffsetTarget) -> Constraint { + self.constant = offset.constraintOffsetTargetValue + return self + } + + @discardableResult + public func update(inset: ConstraintInsetTarget) -> Constraint { + self.constant = inset.constraintInsetTargetValue + return self + } + + @discardableResult + public func update(priority: ConstraintPriorityTarget) -> Constraint { + self.priority = priority.constraintPriorityTargetValue + return self + } + // MARK: Internal + internal func updateConstantAndPriorityIfNeeded() { + guard let installInfo = self.installInfo else { + return + } + for layoutConstraint in installInfo.layoutConstraints.allObjects as! [LayoutConstraint] { + let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute + layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute) + layoutConstraint.priority = self.priority.constraintPriorityTargetValue + } + } + internal func installIfNeeded(updateExisting: Bool = false) -> [NSLayoutConstraint] { let installOnView: ConstraintView? @@ -120,7 +157,7 @@ public class Constraint { let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute // get layout constant - let layoutConstant: CGFloat = self.constant.layoutConstantForLayoutAttribute(layoutToAttribute) + let layoutConstant: CGFloat = self.constant.constraintConstantTargetValueFor(layoutAttribute: layoutToAttribute) // get layout to #if os(iOS) || os(tvOS) diff --git a/Source/ConstraintConstantTarget.swift b/Source/ConstraintConstantTarget.swift index 00e07e3..40ed274 100644 --- a/Source/ConstraintConstantTarget.swift +++ b/Source/ConstraintConstantTarget.swift @@ -42,7 +42,11 @@ extension ConstraintInsets: ConstraintConstantTarget { extension ConstraintConstantTarget { - internal func layoutConstantForLayoutAttribute(_ layoutAttribute: NSLayoutAttribute) -> CGFloat { + internal func constraintConstantTargetValueFor(layoutAttribute: NSLayoutAttribute) -> CGFloat { + if let value = self as? CGFloat { + return value + } + if let value = self as? Float { return CGFloat(value) } @@ -59,10 +63,6 @@ extension ConstraintConstantTarget { return CGFloat(value) } - if let value = self as? CGFloat { - return value - } - if let value = self as? CGSize { if layoutAttribute == .width { return value.width @@ -73,7 +73,6 @@ extension ConstraintConstantTarget { } } - if let value = self as? CGPoint { #if os(iOS) || os(tvOS) switch layoutAttribute { diff --git a/Source/ConstraintInsetTarget.swift b/Source/ConstraintInsetTarget.swift index df528e2..7ca58a9 100644 --- a/Source/ConstraintInsetTarget.swift +++ b/Source/ConstraintInsetTarget.swift @@ -48,3 +48,29 @@ extension CGFloat: ConstraintInsetTarget { extension ConstraintInsets: ConstraintInsetTarget { } + +extension ConstraintInsetTarget { + + internal var constraintInsetTargetValue: ConstraintInsets { + let insets: ConstraintInsets + + if let amount = self as? ConstraintInsets { + insets = amount + } else if let amount = self as? Float { + insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) + } else if let amount = self as? Double { + insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) + } else if let amount = self as? CGFloat { + insets = ConstraintInsets(top: amount, left: amount, bottom: amount, right: amount) + } else if let amount = self as? Int { + insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) + } else if let amount = self as? UInt { + insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) + } else { + insets = ConstraintInsets(top: 0, left: 0, bottom: 0, right: 0) + } + + return ConstraintInsets(top: insets.top, left: insets.left, bottom: -insets.bottom, right: -insets.right) + } + +} diff --git a/Source/ConstraintMakerEditable.swift b/Source/ConstraintMakerEditable.swift index 8c125e5..fb88c41 100644 --- a/Source/ConstraintMakerEditable.swift +++ b/Source/ConstraintMakerEditable.swift @@ -43,32 +43,13 @@ public class ConstraintMakerEditable: ConstraintMakerPriortizable { @discardableResult public func offset(_ amount: ConstraintOffsetTarget) -> ConstraintMakerEditable { - self.description.constant = amount + self.description.constant = amount.constraintOffsetTargetValue return self } @discardableResult public func inset(_ amount: ConstraintInsetTarget) -> ConstraintMakerEditable { - let insets: ConstraintInsets - - if let amount = amount as? ConstraintInsets { - insets = amount - } else if let amount = amount as? Float { - insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) - } else if let amount = amount as? Double { - insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) - } else if let amount = amount as? CGFloat { - insets = ConstraintInsets(top: amount, left: amount, bottom: amount, right: amount) - } else if let amount = amount as? Int { - insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) - } else if let amount = amount as? UInt { - insets = ConstraintInsets(top: CGFloat(amount), left: CGFloat(amount), bottom: CGFloat(amount), right: CGFloat(amount)) - } else { - insets = ConstraintInsets(top: 0, left: 0, bottom: 0, right: 0) - } - - self.description.constant = ConstraintInsets(top: insets.top, left: insets.left, bottom: -insets.bottom, right: -insets.right) - + self.description.constant = amount.constraintInsetTargetValue return self } diff --git a/Source/ConstraintOffsetTarget.swift b/Source/ConstraintOffsetTarget.swift index 9ea140d..bd9e0a1 100644 --- a/Source/ConstraintOffsetTarget.swift +++ b/Source/ConstraintOffsetTarget.swift @@ -45,3 +45,25 @@ extension Double: ConstraintOffsetTarget { extension CGFloat: ConstraintOffsetTarget { } + +extension ConstraintOffsetTarget { + + internal var constraintOffsetTargetValue: CGFloat { + let offset: CGFloat + if let amount = self as? Float { + offset = CGFloat(amount) + } else if let amount = self as? Double { + offset = CGFloat(amount) + } else if let amount = self as? CGFloat { + offset = CGFloat(amount) + } else if let amount = self as? Int { + offset = CGFloat(amount) + } else if let amount = self as? UInt { + offset = CGFloat(amount) + } else { + offset = 0.0 + } + return offset + } + +}