change Constraint to protocol

This commit is contained in:
Jonas Budelmann 2014-07-25 19:48:04 +12:00
parent 83e5edf516
commit 546c6e60ee
5 changed files with 189 additions and 66 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
project.xcworkspace
xcuserdata

View File

@ -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() {
}
}

View File

@ -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;
}

View File

@ -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
}

View File

@ -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() {
}
}