From 6212ee9575b4e7bbc483a04e9841e158e4a34c47 Mon Sep 17 00:00:00 2001 From: Jonas Budelmann Date: Sun, 27 Jul 2014 16:09:38 +1200 Subject: [PATCH] ViewConstraint offset methods --- Snappy/CompositeConstraint.swift | 6 +- Snappy/Constraint.swift | 2 +- Snappy/ViewConstraint.swift | 109 +++++++++++++++++++++++++++-- SnappyExample/ViewController.swift | 38 +++++----- 4 files changed, 125 insertions(+), 30 deletions(-) diff --git a/Snappy/CompositeConstraint.swift b/Snappy/CompositeConstraint.swift index 5dc7cb9..ad7a81d 100644 --- a/Snappy/CompositeConstraint.swift +++ b/Snappy/CompositeConstraint.swift @@ -70,12 +70,12 @@ class CompositeConstraint: Constraint, ConstraintDelegate { return self } - func insets(insets: Any) -> Constraint { + func offset(offset: Any) -> Constraint { return self } - func offset(offset: Any) -> Constraint { - return self + func insets(insets: UIEdgeInsets) -> Constraint { + return self; } func multipliedBy(multiplier: Float) -> Constraint { diff --git a/Snappy/Constraint.swift b/Snappy/Constraint.swift index 7d25c3b..e48ea0f 100644 --- a/Snappy/Constraint.swift +++ b/Snappy/Constraint.swift @@ -41,7 +41,7 @@ typealias Delegate = ConstraintDelegate? func lessThanOrEqualTo(attr: Any) -> Constraint - func insets(insets: Any) -> Constraint + func insets(insets: UIEdgeInsets) -> Constraint func offset(offset: Any) -> Constraint diff --git a/Snappy/ViewConstraint.swift b/Snappy/ViewConstraint.swift index 5d23d71..fe1f3f6 100644 --- a/Snappy/ViewConstraint.swift +++ b/Snappy/ViewConstraint.swift @@ -17,15 +17,18 @@ class ViewConstraint: Constraint { 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; + var layoutRelation: NSLayoutRelation? + var layoutConstant: Float weak var delegate: ConstraintDelegate? init(view: View, firstViewAttribute: ViewAttribute) { self.view = view; self.firstViewAttribute = firstViewAttribute; + + self.layoutPriority = 1000.0// UILayoutPriorityRequired gives error?! + self.layoutMultiplier = 1.0 + self.layoutConstant = 0.0 } var left: Constraint { return addConstraint(.Left) } @@ -43,20 +46,112 @@ class ViewConstraint: Constraint { var and: Constraint { return self } var with: Constraint { return self } - func addConstraint(NSLayoutAttribute) -> Constraint { + func addConstraint(attr: NSLayoutAttribute) -> Constraint { + if (self.layoutRelation) { + //TODO use assert + println("Attributes should be chained before defining the constraint relation") + } + + return self.delegate!.constraint(self, addConstraintWithLayoutAttribute:attr) + } + + func equality(relation: NSLayoutRelation, attr: Any) -> Constraint { + layoutRelation = relation + + switch attr { + case let view as View: + secondViewAttribute = ViewAttribute(view: firstViewAttribute.view, layoutAttribute: firstViewAttribute.layoutAttribute) + case let offset as Float: + layoutConstant = offset + case let number as NSNumber: + layoutConstant = number + case let viewAttribute as ViewAttribute: + secondViewAttribute = viewAttribute + case let size as CGSize: + sizeOffset(size) + case let point as CGPoint: + centerOffset(point) + case let inset as UIEdgeInsets: + insets(inset) + default: + println("unsupported value: \(attr)") + } + +// } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) { +// CGPoint point; +// [value getValue:&point]; +// self.centerOffset = point; +// } else if (strcmp(value.objCType, @encode(CGSize)) == 0) { +// CGSize size; +// [value getValue:&size]; +// self.sizeOffset = size; +// } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) { +// MASEdgeInsets insets; +// [value getValue:&insets]; +// self.insets = insets; +// } else { +// NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value); +// } + + return self; + } + + private func sizeOffset(size: CGSize) { + switch (firstViewAttribute.layoutAttribute) { + case .Width: + layoutConstant = Float(size.width); + break; + case .Height: + layoutConstant = Float(size.height); + break; + default: + break; + } + } + + private func centerOffset(point: CGPoint) { + switch (firstViewAttribute.layoutAttribute) { + case .CenterX: + layoutConstant = Float(point.x); + break; + case .CenterY: + layoutConstant = Float(point.y); + break; + default: + break; + } + } + + func insets(insets: UIEdgeInsets) -> Constraint { + switch (firstViewAttribute.layoutAttribute) { + case .Left: + layoutConstant = Float(insets.left); + break; + case .Top: + layoutConstant = Float(insets.top); + break; + case .Bottom: + layoutConstant = Float(-insets.bottom); + break; + case .Right: + layoutConstant = Float(-insets.right); + break; + default: + break; + } return self; } func equalTo(attr: Any) -> Constraint { - return self + return equality(.Equal, attr: attr) } func greaterThanOrEqualTo(attr: Any) -> Constraint { - return self + return equality(.GreaterThanOrEqual, attr: attr) } func lessThanOrEqualTo(attr: Any) -> Constraint { - return self + return equality(.LessThanOrEqual, attr: attr) } func insets(insets: Any) -> Constraint { diff --git a/SnappyExample/ViewController.swift b/SnappyExample/ViewController.swift index 871bddb..839faf1 100644 --- a/SnappyExample/ViewController.swift +++ b/SnappyExample/ViewController.swift @@ -35,35 +35,35 @@ class ViewController: UIViewController { let padding = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10) - view1.mas_makeConstraints({ make in - make.top.and.left.greaterThanOrEqualTo(superview).insets(padding); + view1.mas_makeConstraints { make in + make.top.and.left.greaterThanOrEqualTo(superview).insets(padding) - make.bottom.equalTo(view3.mas_top).insets(padding); - make.right.equalTo(view2.mas_left).insets(padding); - make.width.equalTo(view2.mas_width); + make.bottom.equalTo(view3.mas_top).insets(padding) + make.right.equalTo(view2.mas_left).insets(padding) + make.width.equalTo(view2.mas_width) - make.height.equalTo([view2, view3]); - }); + make.height.equalTo([view2, view3]) + } - view2.mas_makeConstraints({ make in + view2.mas_makeConstraints { make in // chain attributes - make.top.and.right.equalTo(superview).insets(padding); + make.top.and.right.equalTo(superview).insets(padding) - make.left.equalTo(view1.mas_right).insets(padding); - make.bottom.equalTo(view3.mas_top).insets(padding); - make.width.equalTo(view1.mas_width); + make.left.equalTo(view1.mas_right).insets(padding) + make.bottom.equalTo(view3.mas_top).insets(padding) + make.width.equalTo(view1.mas_width) - make.height.equalTo([view1, view3]); - }); + make.height.equalTo([view1, view3]) + } - view3.mas_makeConstraints({ make in - make.top.equalTo(view1.mas_bottom).insets(padding); + view3.mas_makeConstraints { make in + make.top.equalTo(view1.mas_bottom).insets(padding) // chain attributes - make.left.right.and.bottom.equalTo(superview).insets(padding); + make.left.right.and.bottom.equalTo(superview).insets(padding) - make.height.equalTo([view1, view2]); - }); + make.height.equalTo([view1, view2]) + } }