SnapKit/Snappy/CompositeConstraint.swift

107 lines
2.9 KiB
Swift
Raw Normal View History

2014-07-25 12:24:17 +08:00
//
// CompositeConstraint.swift
// Snappy
//
// Created by Jonas Budelmann on 25/07/14.
// Copyright (c) 2014 Jonas Budelmann. All rights reserved.
//
import UIKit
class CompositeConstraint: Constraint, ConstraintDelegate {
var children = [Constraint]()
2014-07-25 15:48:04 +08:00
weak var delegate: ConstraintDelegate?
2014-07-25 12:24:17 +08:00
init(children: [Constraint]) {
self.children = children
for constraint in children {
constraint.delegate = self
}
}
func constraint(constraint: Constraint, shouldBeReplacedWithConstraint replacementConstraint: Constraint) {
2014-07-25 15:48:04 +08:00
var index: Int?
for (i, c) in enumerate(self.children) {
if (c === constraint) {
index = i
}
}
2014-07-25 12:24:17 +08:00
if (index) {
self.children[index!] = replacementConstraint
}
}
func constraint(constraint: Constraint?, addConstraintWithLayoutAttribute layoutAttribute: NSLayoutAttribute) -> Constraint {
var newConstraint = self.delegate!.constraint(self, addConstraintWithLayoutAttribute: layoutAttribute)
newConstraint.delegate = self
self.children.append(newConstraint)
return newConstraint
}
2014-07-25 15:48:04 +08:00
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() {
}
2014-07-25 12:24:17 +08:00
}