Skip to content

Instantly share code, notes, and snippets.

@jweinberg
Created June 8, 2014 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweinberg/9b44edcc44d08a3e0ca5 to your computer and use it in GitHub Desktop.
Save jweinberg/9b44edcc44d08a3e0ca5 to your computer and use it in GitHub Desktop.
//
// Constraints.swift
// ConstraintsTest
//
// Created by Joshua Weinberg on 6/6/14.
// Copyright (c) 2014 Joshua Weinberg. All rights reserved.
//
import UIKit
func ==(lhs: ConstraintDescriptor, rhs: ConstraintDescriptor) -> Bool {
return true
}
struct ConstraintDescriptor: Hashable, Equatable {
enum ConstraintSide {
case Left
case Right
func toAttribute(axis: UILayoutConstraintAxis) -> NSLayoutAttribute {
switch (axis) {
case .Horizontal:
switch (self) {
case .Left:
return .Left
case .Right:
return .Right
}
case .Vertical:
switch (self) {
case .Left:
return .Top
case .Right:
return .Bottom
}
}
}
}
let multiplier: Double
let constant: Double
let toView: UIView?
let fromView: UIView?
let relation: NSLayoutRelation
let fromAttribute: ConstraintSide
let toAttribute: ConstraintSide
init(item: UIView?, attribute fromAttribute: ConstraintSide, relatedBy: NSLayoutRelation, toItem: UIView?, attribute toAttribute: ConstraintSide, multiplier: Double, constant: Double) {
self.fromView = item
self.toView = toItem
self.toAttribute = toAttribute
self.fromAttribute = fromAttribute
self.relation = relatedBy
self.multiplier = multiplier
self.constant = constant
}
var hashValue: Int {
var value = self.toAttribute.hashValue ^ self.fromAttribute.hashValue ^ self.relation.hashValue ^ self.multiplier.hashValue ^ self.constant.hashValue
if let hash = fromView?.hash {
value ^= hash
}
if let hash = toView?.hash {
value ^= hash
}
return value
}
func toConstraint(direction: UILayoutConstraintAxis) -> NSLayoutConstraint {
var adjustedFromAttribute:NSLayoutAttribute = fromAttribute.toAttribute(direction)
var adjustedToAttribute:NSLayoutAttribute = toAttribute.toAttribute(direction)
var constraint = NSLayoutConstraint(item: fromView, attribute: adjustedFromAttribute, relatedBy:relation, toItem: toView, attribute: adjustedToAttribute, multiplier: CGFloat(multiplier), constant: CGFloat(constant))
return constraint
}
}
struct LayoutContext {
var leftView: UIView?
var rightView: UIView?
var constraints: Set<ConstraintDescriptor>
init(_ left: LayoutContext, _ right: LayoutContext) {
leftView = left.leftView
rightView = right.rightView
constraints = Set<ConstraintDescriptor>(ConstraintDescriptor[](left.constraints))
constraints.add(right.constraints)
}
init(_ view: LayoutContext) {
leftView = view.leftView
rightView = view.rightView
constraints = Set<ConstraintDescriptor>(ConstraintDescriptor[](view.constraints))
}
init(_ view: UIView) {
leftView = view
rightView = view
constraints = Set<ConstraintDescriptor>()
}
}
operator prefix |- {}
@prefix func |-(view: UIView) -> LayoutContext {
var ctx = LayoutContext(view)
return (|-ctx)
}
@prefix func |-(ctx: LayoutContext) -> LayoutContext {
var c = LayoutContext(ctx)
var constraint = ConstraintDescriptor(item: ctx.leftView, attribute: .Left, relatedBy: .Equal, toItem: ctx.leftView?.superview, attribute: .Left, multiplier: 1.0, constant: 8.0)
c.constraints.add(constraint)
return c
}
operator postfix -| {}
@postfix func -|(view: UIView) -> LayoutContext {
var ctx = LayoutContext(view)
return (ctx-|)
}
@postfix func -|(ctx: LayoutContext) -> LayoutContext {
var c = LayoutContext(ctx)
var constraint = ConstraintDescriptor(item: ctx.rightView, attribute: .Right, relatedBy: .Equal, toItem: ctx.rightView?.superview, attribute: .Right, multiplier: 1.0, constant: -8.0)
c.constraints.add(constraint)
return c
}
func -(left: UIView, right: UIView) -> LayoutContext {
return LayoutContext(left) - LayoutContext(right)
}
func -(left: UIView, right: LayoutContext) -> LayoutContext {
return LayoutContext(left) - right
}
func -(left: LayoutContext, right: UIView) -> LayoutContext {
return left - LayoutContext(right)
}
func -(left: LayoutContext, right: LayoutContext) -> LayoutContext {
var ctx = LayoutContext(left, right)
var constraint = ConstraintDescriptor(item: ctx.leftView, attribute: .Right, relatedBy: .Equal, toItem: ctx.rightView, attribute: .Left, multiplier: 1.0, constant: -8.0)
ctx.constraints.add(constraint)
return ctx
}
func H(contex: LayoutContext) -> NSLayoutConstraint[] {
var constraints = NSLayoutConstraint[]()
for constraint in contex.constraints {
constraints.append(constraint.toConstraint(.Horizontal))
}
return constraints
}
func V(contex: LayoutContext) -> NSLayoutConstraint[] {
var constraints = NSLayoutConstraint[]()
for constraint in contex.constraints {
constraints.append(constraint.toConstraint(.Vertical))
}
return constraints
}
//
// Set.swift
// ConstraintsTest
//
// Created by Joshua Weinberg on 6/8/14.
// Copyright (c) 2014 Joshua Weinberg. All rights reserved.
//
import Foundation
extension NSObject: Hashable {
var hashValue: Int {
get {
return self.hash
}
}
}
struct Set<T: Hashable>: Sequence, ArrayLiteralConvertible, Printable {
typealias DictType = Dictionary<Int,T>
var storage = DictType()
static func convertFromArrayLiteral(elements: T...) -> Set<T> {
return Set(elements)
}
init() {
}
init(_ elements: T[]) {
for obj in elements {
self.add(obj)
}
}
func contains(obj: T) -> Bool {
if let x = storage[obj.hashValue] {
return true
} else {
return false
}
}
mutating func add(objs: T[]) {
for obj:T in objs {
self.add(obj)
}
}
mutating func add(objs: Set<T>) {
self.add(T[](objs))
}
mutating func add(obj: T) {
storage[obj.hashValue] = obj
}
mutating func remove(obj: T) {
storage.removeValueForKey(obj.hashValue)
}
var description: String {
return storage.description
}
func generate() -> MapSequenceGenerator<DictType.GeneratorType, T> {
return storage.values.generate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment