From 546c6e60ee6d29aba863eb2680d2f4b2889597fa Mon Sep 17 00:00:00 2001 From: Jonas Budelmann Date: Fri, 25 Jul 2014 19:48:04 +1200 Subject: [PATCH] change Constraint to protocol --- .gitignore | 2 + Snappy/CompositeConstraint.swift | 75 +++++++++++++++++++++++++- Snappy/Constraint.swift | 91 +++++++++++--------------------- Snappy/ConstraintMaker.swift | 8 ++- Snappy/ViewConstraint.swift | 79 +++++++++++++++++++++++++-- 5 files changed, 189 insertions(+), 66 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b104b9f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +project.xcworkspace +xcuserdata \ No newline at end of file diff --git a/Snappy/CompositeConstraint.swift b/Snappy/CompositeConstraint.swift index 2b6868a..5dc7cb9 100644 --- a/Snappy/CompositeConstraint.swift +++ b/Snappy/CompositeConstraint.swift @@ -10,17 +10,23 @@ import UIKit class CompositeConstraint: Constraint, ConstraintDelegate { var children = [Constraint]() + weak var delegate: ConstraintDelegate? init(children: [Constraint]) { self.children = children - super.init() for constraint in children { constraint.delegate = self } } func constraint(constraint: Constraint, shouldBeReplacedWithConstraint replacementConstraint: Constraint) { - var index = find(self.children, constraint) + var index: Int? + for (i, c) in enumerate(self.children) { + if (c === constraint) { + index = i + } + } + if (index) { self.children[index!] = replacementConstraint } @@ -33,4 +39,69 @@ class CompositeConstraint: Constraint, ConstraintDelegate { return newConstraint } + var left: Constraint { return addConstraint(.Left) } + var top: Constraint { return addConstraint(.Top) } + var right: Constraint { return addConstraint(.Right) } + var bottom: Constraint { return addConstraint(.Bottom) } + var leading: Constraint { return addConstraint(.Leading) } + var trailing: Constraint { return addConstraint(.Trailing) } + var width: Constraint { return addConstraint(.Width) } + var height: Constraint { return addConstraint(.Height) } + var centerX: Constraint { return addConstraint(.CenterX) } + var centerY: Constraint { return addConstraint(.CenterY) } + var baseline: Constraint { return addConstraint(.Baseline) } + + var and: Constraint { return self } + var with: Constraint { return self } + + func addConstraint(NSLayoutAttribute) -> Constraint { + return self; + } + + func equalTo(attr: Any) -> Constraint { + return self + } + + func greaterThanOrEqualTo(attr: Any) -> Constraint { + return self + } + + func lessThanOrEqualTo(attr: Any) -> Constraint { + return self + } + + func insets(insets: Any) -> Constraint { + return self + } + + func offset(offset: Any) -> Constraint { + return self + } + + func multipliedBy(multiplier: Float) -> Constraint { + return self + } + + func dividedBy(divider: Float) -> Constraint { + return self + } + + func priority(priority: UILayoutPriority) -> Constraint { + return self + } + + func priorityLow() -> Constraint { + return self + } + + func priorityMedium() -> Constraint { + return self + } + + func priorityHigh() -> Constraint { + return self + } + + func install() { + } } \ No newline at end of file diff --git a/Snappy/Constraint.swift b/Snappy/Constraint.swift index 14d1109..7d25c3b 100644 --- a/Snappy/Constraint.swift +++ b/Snappy/Constraint.swift @@ -13,78 +13,49 @@ import UIKit func constraint(constraint: Constraint?, addConstraintWithLayoutAttribute layoutAttribute: NSLayoutAttribute) -> Constraint } -class Constraint: Equatable { - weak var delegate: ConstraintDelegate? +typealias Delegate = ConstraintDelegate? + +@class_protocol protocol Constraint { + weak var delegate: Delegate { get set } + + var left: Constraint { get } + var top: Constraint { get } + var right: Constraint { get } + var bottom: Constraint { get } + var leading: Constraint { get } + var trailing: Constraint { get } + var width: Constraint { get } + var height: Constraint { get } + var centerX: Constraint { get } + var centerY: Constraint { get } + var baseline: Constraint { get } - init() { - - } + var and: Constraint { get } + var with: Constraint { get } - var left: Constraint { return Constraint() } - var top: Constraint { return Constraint() } - var right: Constraint { return Constraint() } - var bottom: Constraint { return Constraint() } - var leading: Constraint { return Constraint() } - var trailing: Constraint { return Constraint() } - var width: Constraint { return Constraint() } - var height: Constraint { return Constraint() } - var centerX: Constraint { return Constraint() } - var centerY: Constraint { return Constraint() } - var baseline: Constraint { return Constraint() } + func addConstraint(NSLayoutAttribute) -> Constraint - var and: Constraint { return self } - var with: Constraint { return self } + func equalTo(attr: Any) -> Constraint - func equalTo(attr: Any) -> Constraint { - return self - } + func greaterThanOrEqualTo(attr: Any) -> Constraint - func greaterThanOrEqualTo(attr: Any) -> Constraint { - return self - } + func lessThanOrEqualTo(attr: Any) -> Constraint - func lessThanOrEqualTo(attr: Any) -> Constraint { - return self - } + func insets(insets: Any) -> Constraint - func insets(insets: Any) -> Constraint { - return self - } + func offset(offset: Any) -> Constraint - func offset(offset: Any) -> Constraint { - return self - } + func multipliedBy(multiplier: Float) -> Constraint - func multipliedBy(multiplier: Float) -> Constraint { - return self - } + func dividedBy(divider: Float) -> Constraint - func dividedBy(divider: Float) -> Constraint { - return self - } + func priority(priority: UILayoutPriority) -> Constraint - func priority(priority: UILayoutPriority) -> Constraint { - return self - } + func priorityLow() -> Constraint - func priorityLow() -> Constraint { - return self - } + func priorityMedium() -> Constraint - func priorityMedium() -> Constraint { - return self - } + func priorityHigh() -> Constraint - func priorityHigh() -> Constraint { - return self - } - - func install() { - - } + func install() } - - -@infix func ==(lhs: Constraint, rhs: Constraint) -> Bool { - return lhs === rhs; -} \ No newline at end of file diff --git a/Snappy/ConstraintMaker.swift b/Snappy/ConstraintMaker.swift index c69f9a1..5d20e8e 100644 --- a/Snappy/ConstraintMaker.swift +++ b/Snappy/ConstraintMaker.swift @@ -59,7 +59,13 @@ class ConstraintMaker: ConstraintDelegate { } func constraint(constraint: Constraint, shouldBeReplacedWithConstraint replacementConstraint: Constraint) { - var index = find(self.constraints, constraint) + var index: Int? + for (i, c) in enumerate(self.constraints) { + if (c === constraint) { + index = i + } + } + if (index) { self.constraints[index!] = replacementConstraint } diff --git a/Snappy/ViewConstraint.swift b/Snappy/ViewConstraint.swift index 6482ce1..5d23d71 100644 --- a/Snappy/ViewConstraint.swift +++ b/Snappy/ViewConstraint.swift @@ -10,14 +10,87 @@ import UIKit class ViewConstraint: Constraint { weak var view: View? + var firstViewAttribute: ViewAttribute - var layoutPriority: UILayoutPriority + var secondViewAttribute: ViewAttribute? + + var layoutPriority = 1000.0// UILayoutPriorityRequired gives error?! var layoutMultiplier = 1.0 + var layoutConstraint: NSLayoutConstraint? + var layoutRelation = NSLayoutRelation.Equal + var layoutConstant = 0.0 + var hasLayoutRelation = false; + + weak var delegate: ConstraintDelegate? init(view: View, firstViewAttribute: ViewAttribute) { self.view = view; self.firstViewAttribute = firstViewAttribute; - self.layoutPriority = 750 //UILayoutPriorityRequired gives build error!? - super.init() + } + + var left: Constraint { return addConstraint(.Left) } + var top: Constraint { return addConstraint(.Top) } + var right: Constraint { return addConstraint(.Right) } + var bottom: Constraint { return addConstraint(.Bottom) } + var leading: Constraint { return addConstraint(.Leading) } + var trailing: Constraint { return addConstraint(.Trailing) } + var width: Constraint { return addConstraint(.Width) } + var height: Constraint { return addConstraint(.Height) } + var centerX: Constraint { return addConstraint(.CenterX) } + var centerY: Constraint { return addConstraint(.CenterY) } + var baseline: Constraint { return addConstraint(.Baseline) } + + var and: Constraint { return self } + var with: Constraint { return self } + + func addConstraint(NSLayoutAttribute) -> Constraint { + return self; + } + + func equalTo(attr: Any) -> Constraint { + return self + } + + func greaterThanOrEqualTo(attr: Any) -> Constraint { + return self + } + + func lessThanOrEqualTo(attr: Any) -> Constraint { + return self + } + + func insets(insets: Any) -> Constraint { + return self + } + + func offset(offset: Any) -> Constraint { + return self + } + + func multipliedBy(multiplier: Float) -> Constraint { + return self + } + + func dividedBy(divider: Float) -> Constraint { + return self + } + + func priority(priority: UILayoutPriority) -> Constraint { + return self + } + + func priorityLow() -> Constraint { + return self + } + + func priorityMedium() -> Constraint { + return self + } + + func priorityHigh() -> Constraint { + return self + } + + func install() { } } \ No newline at end of file