Ver código fonte

Add API’s for updating offset/inset/priorty

Robert Payne 9 anos atrás
pai
commit
e713b9da5b

+ 40 - 3
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)

+ 5 - 6
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 {

+ 26 - 0
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)
+    }
+    
+}

+ 2 - 21
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
     }
     

+ 22 - 0
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
+    }
+    
+}