2015-04-12 18:21:02 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// SnapKit
|
2015-04-12 18:21:02 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// Copyright (c) 2011-2015 SnapKit Team - https://github.com/SnapKit
|
2015-04-12 18:21:02 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2015-04-12 18:21:02 +08:00
|
|
|
import UIKit
|
|
|
|
#else
|
|
|
|
import AppKit
|
|
|
|
#endif
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
// Type of object that can be a target of relation constraint.
|
|
|
|
// Doesn't cover system protocols which can't be extended with additional conformances.
|
|
|
|
public protocol RelationTarget {
|
|
|
|
var constraintItem : ConstraintItem { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of things that can be converted to floats. Used to provide a catch all for
|
|
|
|
// different numeric types.
|
|
|
|
public protocol FloatConvertible : RelationTarget {
|
2015-10-12 12:57:59 +08:00
|
|
|
var floatValue : Float { get }
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
extension FloatConvertible {
|
|
|
|
public var constraintItem : ConstraintItem {
|
|
|
|
return ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Float : FloatConvertible, RelationTarget {
|
2015-10-12 12:57:59 +08:00
|
|
|
public var floatValue : Float {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
extension Int : FloatConvertible, RelationTarget {
|
2015-10-12 12:57:59 +08:00
|
|
|
public var floatValue : Float {
|
|
|
|
return Float(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
extension UInt : FloatConvertible, RelationTarget {
|
2015-10-12 12:57:59 +08:00
|
|
|
public var floatValue : Float {
|
|
|
|
return Float(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
extension Double : FloatConvertible, RelationTarget {
|
2015-10-12 12:57:59 +08:00
|
|
|
public var floatValue : Float {
|
|
|
|
return Float(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
extension CGFloat : FloatConvertible, RelationTarget {
|
2015-10-12 12:57:59 +08:00
|
|
|
public var floatValue : Float {
|
|
|
|
return Float(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
@available(iOS 9.0, OSX 10.11, *)
|
|
|
|
extension NSLayoutAnchor : RelationTarget {
|
|
|
|
public var constraintItem : ConstraintItem {
|
|
|
|
return ConstraintItem(object: self, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension CGPoint : RelationTarget {
|
|
|
|
public var constraintItem : ConstraintItem {
|
|
|
|
return ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension CGSize : RelationTarget {
|
|
|
|
public var constraintItem : ConstraintItem {
|
|
|
|
return ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension EdgeInsets : RelationTarget {
|
|
|
|
public var constraintItem : ConstraintItem {
|
|
|
|
return ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension View : RelationTarget {
|
|
|
|
public var constraintItem : ConstraintItem {
|
|
|
|
return ConstraintItem(object: self, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
extension ConstraintItem : RelationTarget {
|
|
|
|
public var constraintItem : ConstraintItem {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/**
|
|
|
|
Used to expose the final API of a `ConstraintDescription` which allows getting a constraint from it
|
|
|
|
*/
|
2015-10-12 13:53:46 +08:00
|
|
|
public class ConstraintDescriptionFinalizable {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
private let backing : ConstraintDescription
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
internal init(_ backing : ConstraintDescription) {
|
|
|
|
self.backing = backing
|
|
|
|
}
|
|
|
|
|
|
|
|
public var constraint: Constraint {
|
|
|
|
return backing.constraint
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to expose priority APIs
|
|
|
|
*/
|
2015-10-12 13:53:46 +08:00
|
|
|
public class ConstraintDescriptionPriortizable: ConstraintDescriptionFinalizable {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
public func priority(priority: FloatConvertible) -> ConstraintDescriptionFinalizable {
|
|
|
|
return ConstraintDescriptionFinalizable(self.backing.priority(priority))
|
|
|
|
}
|
|
|
|
|
|
|
|
public func priorityRequired() -> ConstraintDescriptionFinalizable {
|
|
|
|
return ConstraintDescriptionFinalizable(self.backing.priorityRequired())
|
|
|
|
}
|
|
|
|
public func priorityHigh() -> ConstraintDescriptionFinalizable {
|
|
|
|
return ConstraintDescriptionFinalizable(self.backing.priorityHigh())
|
|
|
|
}
|
|
|
|
public func priorityMedium() -> ConstraintDescriptionFinalizable {
|
|
|
|
return ConstraintDescriptionFinalizable(self.backing.priorityMedium())
|
|
|
|
}
|
|
|
|
public func priorityLow() -> ConstraintDescriptionFinalizable {
|
|
|
|
return ConstraintDescriptionFinalizable(self.backing.priorityLow())
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-16 06:07:51 +08:00
|
|
|
Used to expose multiplier & constant APIs
|
2015-04-12 18:21:02 +08:00
|
|
|
*/
|
2015-10-12 13:53:46 +08:00
|
|
|
public class ConstraintDescriptionEditable: ConstraintDescriptionPriortizable {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
public func multipliedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
return ConstraintDescriptionEditable(self.backing.multipliedBy(amount))
|
|
|
|
}
|
2015-10-12 12:57:59 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
public func dividedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
return self.multipliedBy(1 / amount.floatValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func offset(amount : FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
return ConstraintDescriptionEditable(self.backing.offset(amount))
|
|
|
|
}
|
|
|
|
public func offset(amount: CGPoint) -> ConstraintDescriptionEditable {
|
|
|
|
return ConstraintDescriptionEditable(self.backing.offset(amount))
|
|
|
|
}
|
|
|
|
public func offset(amount: CGSize) -> ConstraintDescriptionEditable {
|
|
|
|
return ConstraintDescriptionEditable(self.backing.offset(amount))
|
|
|
|
}
|
|
|
|
public func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
|
|
|
|
return ConstraintDescriptionEditable(self.backing.offset(amount))
|
|
|
|
}
|
2015-04-16 06:07:51 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
public func inset(amount: FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
return ConstraintDescriptionEditable(self.backing.inset(amount))
|
|
|
|
}
|
|
|
|
public func inset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
|
|
|
|
return ConstraintDescriptionEditable(self.backing.inset(amount))
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to expose relation APIs
|
|
|
|
*/
|
2015-10-12 13:53:46 +08:00
|
|
|
public class ConstraintDescriptionRelatable {
|
|
|
|
|
|
|
|
private let backing : ConstraintDescription
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
init(_ backing : ConstraintDescription) {
|
|
|
|
self.backing = backing
|
|
|
|
}
|
2015-04-16 06:07:51 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
|
2015-10-12 14:11:45 +08:00
|
|
|
public func equalTo(other: RelationTarget, file : String = __FILE__, line : UInt = __LINE__) -> ConstraintDescriptionEditable {
|
|
|
|
let location = SourceLocation(file: file, line: line)
|
|
|
|
return ConstraintDescriptionEditable(self.backing.constrainTo(other, relation : .Equal, location: location))
|
2015-10-12 13:53:46 +08:00
|
|
|
}
|
2015-10-12 14:11:45 +08:00
|
|
|
public func equalTo(other: LayoutSupport, file : String = __FILE__, line : UInt = __LINE__) -> ConstraintDescriptionEditable {
|
|
|
|
let location = SourceLocation(file: file, line: line)
|
|
|
|
return ConstraintDescriptionEditable(self.backing.constrainTo(other, relation : .Equal, location: location))
|
2015-10-12 13:53:46 +08:00
|
|
|
}
|
2015-04-16 06:07:51 +08:00
|
|
|
|
2015-10-12 14:11:45 +08:00
|
|
|
public func lessThanOrEqualTo(other: RelationTarget, file : String = __FILE__, line : UInt = __LINE__) -> ConstraintDescriptionEditable {
|
|
|
|
let location = SourceLocation(file: file, line: line)
|
|
|
|
return ConstraintDescriptionEditable(self.backing.constrainTo(other, relation : .LessThanOrEqualTo, location: location))
|
2015-10-12 13:53:46 +08:00
|
|
|
}
|
2015-10-12 14:11:45 +08:00
|
|
|
public func lessThanOrEqualTo(other: LayoutSupport, file : String = __FILE__, line : UInt = __LINE__) -> ConstraintDescriptionEditable {
|
|
|
|
let location = SourceLocation(file: file, line: line)
|
|
|
|
return ConstraintDescriptionEditable(self.backing.constrainTo(other, relation : .LessThanOrEqualTo, location: location))
|
2015-10-12 13:53:46 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 14:11:45 +08:00
|
|
|
public func greaterThanOrEqualTo(other: RelationTarget, file : String = __FILE__, line : UInt = __LINE__) -> ConstraintDescriptionEditable {
|
|
|
|
let location = SourceLocation(file: file, line: line)
|
|
|
|
return ConstraintDescriptionEditable(self.backing.constrainTo(other, relation : .GreaterThanOrEqualTo, location: location))
|
2015-10-12 13:53:46 +08:00
|
|
|
}
|
2015-10-12 14:11:45 +08:00
|
|
|
public func greaterThanOrEqualTo(other: LayoutSupport, file : String = __FILE__, line : UInt = __LINE__) -> ConstraintDescriptionEditable {
|
|
|
|
let location = SourceLocation(file: file, line: line)
|
|
|
|
return ConstraintDescriptionEditable(self.backing.constrainTo(other, relation : .GreaterThanOrEqualTo, location: location))
|
2015-10-12 13:53:46 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to expose chaining APIs
|
|
|
|
*/
|
2015-10-12 13:53:46 +08:00
|
|
|
public class ConstraintDescriptionExtendable: ConstraintDescriptionRelatable {
|
|
|
|
|
|
|
|
public var left: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.left)
|
|
|
|
}
|
|
|
|
public var top: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.top)
|
|
|
|
}
|
|
|
|
public var bottom: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.bottom)
|
|
|
|
}
|
|
|
|
public var right: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.right)
|
|
|
|
}
|
|
|
|
public var leading: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.leading)
|
|
|
|
}
|
|
|
|
public var trailing: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.trailing)
|
|
|
|
}
|
|
|
|
public var width: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.width)
|
|
|
|
}
|
|
|
|
public var height: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.height)
|
|
|
|
}
|
|
|
|
public var centerX: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.centerX)
|
|
|
|
}
|
|
|
|
public var centerY: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.centerY)
|
|
|
|
}
|
|
|
|
public var baseline: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.baseline)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var firstBaseline: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.firstBaseline)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var leftMargin: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.leftMargin)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var rightMargin: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.rightMargin)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var topMargin: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.topMargin)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var bottomMargin: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.bottomMargin)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var leadingMargin: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.leadingMargin)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var trailingMargin: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.trailingMargin)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var centerXWithinMargins: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.centerXWithinMargins)
|
|
|
|
}
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
public var centerYWithinMargins: ConstraintDescriptionExtendable {
|
|
|
|
return ConstraintDescriptionExtendable(self.backing.centerYWithinMargins)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to internally manage building constraint
|
|
|
|
*/
|
2015-10-12 13:53:46 +08:00
|
|
|
internal class ConstraintDescription {
|
|
|
|
|
2015-10-12 14:11:45 +08:00
|
|
|
private var location : SourceLocation?
|
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
private var left: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Left) }
|
|
|
|
private var top: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Top) }
|
|
|
|
private var right: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Right) }
|
|
|
|
private var bottom: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Bottom) }
|
|
|
|
private var leading: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Leading) }
|
|
|
|
private var trailing: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Trailing) }
|
|
|
|
private var width: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Width) }
|
|
|
|
private var height: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Height) }
|
|
|
|
private var centerX: ConstraintDescription { return self.addConstraint(ConstraintAttributes.CenterX) }
|
|
|
|
private var centerY: ConstraintDescription { return self.addConstraint(ConstraintAttributes.CenterY) }
|
|
|
|
private var baseline: ConstraintDescription { return self.addConstraint(ConstraintAttributes.Baseline) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var firstBaseline: ConstraintDescription { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var leftMargin: ConstraintDescription { return self.addConstraint(ConstraintAttributes.LeftMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var rightMargin: ConstraintDescription { return self.addConstraint(ConstraintAttributes.RightMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var topMargin: ConstraintDescription { return self.addConstraint(ConstraintAttributes.TopMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var bottomMargin: ConstraintDescription { return self.addConstraint(ConstraintAttributes.BottomMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var leadingMargin: ConstraintDescription { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var trailingMargin: ConstraintDescription { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var centerXWithinMargins: ConstraintDescription { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-10-12 13:53:46 +08:00
|
|
|
private var centerYWithinMargins: ConstraintDescription { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
// MARK: initializer
|
|
|
|
|
|
|
|
init(fromItem: ConstraintItem) {
|
|
|
|
self.fromItem = fromItem
|
|
|
|
self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: multiplier
|
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
private func multipliedBy(amount: FloatConvertible) -> ConstraintDescription {
|
2015-10-12 12:57:59 +08:00
|
|
|
self.multiplier = amount.floatValue
|
2015-04-12 18:21:02 +08:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
private func dividedBy(amount: FloatConvertible) -> ConstraintDescription {
|
2015-10-12 12:57:59 +08:00
|
|
|
self.multiplier = 1.0 / amount.floatValue;
|
2015-04-12 18:21:02 +08:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:07:51 +08:00
|
|
|
// MARK: offset
|
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
private func offset(amount: FloatConvertible) -> ConstraintDescription {
|
2015-10-12 12:57:59 +08:00
|
|
|
self.constant = amount.floatValue
|
2015-04-16 06:07:51 +08:00
|
|
|
return self
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func offset(amount: CGPoint) -> ConstraintDescription {
|
2015-04-16 06:07:51 +08:00
|
|
|
self.constant = amount
|
|
|
|
return self
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func offset(amount: CGSize) -> ConstraintDescription {
|
2015-04-16 06:07:51 +08:00
|
|
|
self.constant = amount
|
|
|
|
return self
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func offset(amount: EdgeInsets) -> ConstraintDescription {
|
2015-04-16 06:07:51 +08:00
|
|
|
self.constant = amount
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:01:33 +08:00
|
|
|
// MARK: inset
|
2015-04-16 06:07:51 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
private func inset(amount: FloatConvertible) -> ConstraintDescription {
|
2015-10-12 12:57:59 +08:00
|
|
|
let value = CGFloat(amount.floatValue)
|
2015-07-30 15:01:33 +08:00
|
|
|
self.constant = EdgeInsets(top: value, left: value, bottom: -value, right: -value)
|
|
|
|
return self
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func inset(amount: EdgeInsets) -> ConstraintDescription {
|
2015-04-16 06:07:51 +08:00
|
|
|
self.constant = EdgeInsets(top: amount.top, left: amount.left, bottom: -amount.bottom, right: -amount.right)
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
// MARK: priority
|
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
private func priority(priority: FloatConvertible) -> ConstraintDescription {
|
2015-10-12 12:57:59 +08:00
|
|
|
self.priority = priority.floatValue
|
2015-04-12 18:21:02 +08:00
|
|
|
return self
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func priorityRequired() -> ConstraintDescription {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.priority(1000.0)
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func priorityHigh() -> ConstraintDescription {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.priority(750.0)
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func priorityMedium() -> ConstraintDescription {
|
2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.priority(500.0)
|
|
|
|
#else
|
|
|
|
return self.priority(501.0)
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
private func priorityLow() -> ConstraintDescription {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.priority(250.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Constraint
|
|
|
|
|
|
|
|
internal var constraint: Constraint {
|
2015-04-12 19:32:03 +08:00
|
|
|
if self.concreteConstraint == nil {
|
2015-04-12 18:21:02 +08:00
|
|
|
if self.relation == nil {
|
|
|
|
fatalError("Attempting to create a constraint from a ConstraintDescription before it has been fully chained.")
|
|
|
|
}
|
2015-04-12 19:32:03 +08:00
|
|
|
self.concreteConstraint = ConcreteConstraint(
|
2015-04-12 18:21:02 +08:00
|
|
|
fromItem: self.fromItem,
|
|
|
|
toItem: self.toItem,
|
|
|
|
relation: self.relation!,
|
|
|
|
constant: self.constant,
|
|
|
|
multiplier: self.multiplier,
|
2015-10-12 14:11:45 +08:00
|
|
|
priority: self.priority,
|
|
|
|
location: self.location
|
|
|
|
)
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
2015-04-12 19:32:03 +08:00
|
|
|
return self.concreteConstraint!
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let fromItem: ConstraintItem
|
|
|
|
private var toItem: ConstraintItem {
|
|
|
|
willSet {
|
2015-04-12 19:32:03 +08:00
|
|
|
if self.concreteConstraint != nil {
|
2015-04-12 19:44:32 +08:00
|
|
|
fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private var relation: ConstraintRelation? {
|
|
|
|
willSet {
|
2015-04-12 19:32:03 +08:00
|
|
|
if self.concreteConstraint != nil {
|
2015-04-12 19:44:32 +08:00
|
|
|
fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private var constant: Any = Float(0.0) {
|
|
|
|
willSet {
|
2015-04-12 19:32:03 +08:00
|
|
|
if self.concreteConstraint != nil {
|
2015-04-12 19:44:32 +08:00
|
|
|
fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private var multiplier: Float = 1.0 {
|
|
|
|
willSet {
|
2015-04-12 19:32:03 +08:00
|
|
|
if self.concreteConstraint != nil {
|
2015-04-12 19:44:32 +08:00
|
|
|
fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private var priority: Float = 1000.0 {
|
|
|
|
willSet {
|
2015-04-12 19:32:03 +08:00
|
|
|
if self.concreteConstraint != nil {
|
2015-04-12 19:44:32 +08:00
|
|
|
fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-12 19:32:03 +08:00
|
|
|
private var concreteConstraint: ConcreteConstraint? = nil
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
private func addConstraint(attributes: ConstraintAttributes) -> ConstraintDescription {
|
|
|
|
if self.relation == nil {
|
|
|
|
self.fromItem.attributes += attributes
|
|
|
|
}
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-10-12 14:11:45 +08:00
|
|
|
private func constrainTo(other: RelationTarget, relation: ConstraintRelation, location : SourceLocation) -> ConstraintDescription {
|
|
|
|
|
|
|
|
self.location = location
|
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
if let constant = other as? FloatConvertible {
|
|
|
|
self.constant = constant.floatValue
|
|
|
|
}
|
|
|
|
|
|
|
|
let item = other.constraintItem
|
|
|
|
|
|
|
|
if item.attributes != ConstraintAttributes.None {
|
|
|
|
let toLayoutAttributes = item.attributes.layoutAttributes
|
2015-04-12 18:21:02 +08:00
|
|
|
if toLayoutAttributes.count > 1 {
|
|
|
|
let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
|
|
|
|
if toLayoutAttributes != fromLayoutAttributes {
|
|
|
|
NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
|
|
|
|
return self
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
item.attributes = ConstraintAttributes.None
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-12 13:53:46 +08:00
|
|
|
self.toItem = item
|
2015-04-12 18:21:02 +08:00
|
|
|
self.relation = relation
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
2015-10-12 14:11:45 +08:00
|
|
|
private func constrainTo(other: LayoutSupport, relation: ConstraintRelation, location : SourceLocation) -> ConstraintDescription {
|
2015-10-12 13:53:46 +08:00
|
|
|
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation, location: location)
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 01:43:29 +08:00
|
|
|
}
|