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-04-12 18:21:02 +08:00
|
|
|
/**
|
|
|
|
Used to expose the final API of a `ConstraintDescription` which allows getting a constraint from it
|
|
|
|
*/
|
|
|
|
public protocol ConstraintDescriptionFinalizable: class {
|
|
|
|
|
|
|
|
var constraint: Constraint { get }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to expose priority APIs
|
|
|
|
*/
|
|
|
|
public protocol ConstraintDescriptionPriortizable: ConstraintDescriptionFinalizable {
|
|
|
|
|
2015-10-12 12:57:59 +08:00
|
|
|
func priority(priority: FloatConvertible) -> ConstraintDescriptionFinalizable
|
2015-04-12 18:21:02 +08:00
|
|
|
func priorityRequired() -> ConstraintDescriptionFinalizable
|
|
|
|
func priorityHigh() -> ConstraintDescriptionFinalizable
|
|
|
|
func priorityMedium() -> ConstraintDescriptionFinalizable
|
|
|
|
func priorityLow() -> ConstraintDescriptionFinalizable
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-16 06:07:51 +08:00
|
|
|
Used to expose multiplier & constant APIs
|
2015-04-12 18:21:02 +08:00
|
|
|
*/
|
2015-04-16 06:07:51 +08:00
|
|
|
public protocol ConstraintDescriptionEditable: ConstraintDescriptionPriortizable {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-10-12 12:57:59 +08:00
|
|
|
func multipliedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable
|
|
|
|
|
|
|
|
func dividedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable
|
2015-04-16 06:07:51 +08:00
|
|
|
|
2015-10-12 12:57:59 +08:00
|
|
|
func offset(amount: FloatConvertible) -> ConstraintDescriptionEditable
|
2015-04-16 06:07:51 +08:00
|
|
|
func offset(amount: CGPoint) -> ConstraintDescriptionEditable
|
|
|
|
func offset(amount: CGSize) -> ConstraintDescriptionEditable
|
|
|
|
func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable
|
|
|
|
|
2015-10-12 12:57:59 +08:00
|
|
|
func inset(amount: FloatConvertible) -> ConstraintDescriptionEditable
|
2015-07-30 15:01:33 +08:00
|
|
|
func inset(amount: EdgeInsets) -> ConstraintDescriptionEditable
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to expose relation APIs
|
|
|
|
*/
|
|
|
|
public protocol ConstraintDescriptionRelatable: class {
|
|
|
|
|
2015-04-16 06:07:51 +08:00
|
|
|
func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable
|
2015-10-12 13:16:34 +08:00
|
|
|
func equalTo(other: RelationTarget) -> ConstraintDescriptionEditable
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
|
|
|
func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable
|
2015-04-16 06:07:51 +08:00
|
|
|
|
|
|
|
func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
|
2015-10-12 13:16:34 +08:00
|
|
|
func lessThanOrEqualTo(other: RelationTarget) -> ConstraintDescriptionEditable
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
|
|
|
func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
|
2015-04-16 06:07:51 +08:00
|
|
|
|
|
|
|
func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
|
2015-10-12 13:16:34 +08:00
|
|
|
func greaterThanOrEqualTo(other: RelationTarget) -> ConstraintDescriptionEditable
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
|
|
|
func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to expose chaining APIs
|
|
|
|
*/
|
|
|
|
public protocol ConstraintDescriptionExtendable: ConstraintDescriptionRelatable {
|
|
|
|
|
|
|
|
var left: ConstraintDescriptionExtendable { get }
|
|
|
|
var top: ConstraintDescriptionExtendable { get }
|
|
|
|
var bottom: ConstraintDescriptionExtendable { get }
|
|
|
|
var right: ConstraintDescriptionExtendable { get }
|
|
|
|
var leading: ConstraintDescriptionExtendable { get }
|
|
|
|
var trailing: ConstraintDescriptionExtendable { get }
|
|
|
|
var width: ConstraintDescriptionExtendable { get }
|
|
|
|
var height: ConstraintDescriptionExtendable { get }
|
|
|
|
var centerX: ConstraintDescriptionExtendable { get }
|
|
|
|
var centerY: ConstraintDescriptionExtendable { get }
|
|
|
|
var baseline: ConstraintDescriptionExtendable { get }
|
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var firstBaseline: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var leftMargin: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var rightMargin: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var topMargin: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var bottomMargin: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var leadingMargin: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var trailingMargin: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var centerXWithinMargins: ConstraintDescriptionExtendable { get }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
var centerYWithinMargins: ConstraintDescriptionExtendable { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used to internally manage building constraint
|
|
|
|
*/
|
2015-04-22 20:31:40 +08:00
|
|
|
internal class ConstraintDescription: ConstraintDescriptionExtendable, ConstraintDescriptionEditable, ConstraintDescriptionFinalizable {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
internal var left: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Left) }
|
|
|
|
internal var top: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Top) }
|
|
|
|
internal var right: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Right) }
|
|
|
|
internal var bottom: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Bottom) }
|
|
|
|
internal var leading: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Leading) }
|
|
|
|
internal var trailing: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Trailing) }
|
|
|
|
internal var width: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Width) }
|
|
|
|
internal var height: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Height) }
|
|
|
|
internal var centerX: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterX) }
|
|
|
|
internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
|
|
|
|
internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
|
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var leftMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var rightMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var topMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var bottomMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var leadingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var trailingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var centerXWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var centerYWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
|
|
|
|
|
|
|
|
// MARK: initializer
|
|
|
|
|
|
|
|
init(fromItem: ConstraintItem) {
|
|
|
|
self.fromItem = fromItem
|
|
|
|
self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: equalTo
|
|
|
|
|
2015-04-16 06:07:51 +08:00
|
|
|
internal func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
|
|
|
}
|
2015-10-12 13:16:34 +08:00
|
|
|
internal func equalTo(other: RelationTarget) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
|
|
|
}
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
|
|
|
internal func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: lessThanOrEqualTo
|
|
|
|
|
2015-04-16 06:07:51 +08:00
|
|
|
internal func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
|
|
|
}
|
2015-10-12 13:16:34 +08:00
|
|
|
internal func lessThanOrEqualTo(other: RelationTarget) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
|
|
|
}
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
|
|
|
internal func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: greaterThanOrEqualTo
|
|
|
|
|
2015-04-16 06:07:51 +08:00
|
|
|
internal func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
|
|
|
}
|
2015-10-12 13:16:34 +08:00
|
|
|
internal func greaterThanOrEqualTo(other: RelationTarget) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
|
|
|
}
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
|
|
|
internal func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: multiplier
|
|
|
|
|
2015-10-12 12:57:59 +08:00
|
|
|
internal func multipliedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
self.multiplier = amount.floatValue
|
2015-04-12 18:21:02 +08:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-10-12 12:57:59 +08:00
|
|
|
internal func dividedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
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 12:57:59 +08:00
|
|
|
internal func offset(amount: FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
self.constant = amount.floatValue
|
2015-04-16 06:07:51 +08:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
internal func offset(amount: CGPoint) -> ConstraintDescriptionEditable {
|
|
|
|
self.constant = amount
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
internal func offset(amount: CGSize) -> ConstraintDescriptionEditable {
|
|
|
|
self.constant = amount
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
internal func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
|
|
|
|
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 12:57:59 +08:00
|
|
|
internal func inset(amount: FloatConvertible) -> ConstraintDescriptionEditable {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
internal func inset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
|
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 12:57:59 +08:00
|
|
|
internal func priority(priority: FloatConvertible) -> ConstraintDescriptionFinalizable {
|
|
|
|
self.priority = priority.floatValue
|
2015-04-12 18:21:02 +08:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
internal func priorityRequired() -> ConstraintDescriptionFinalizable {
|
|
|
|
return self.priority(1000.0)
|
|
|
|
}
|
|
|
|
internal func priorityHigh() -> ConstraintDescriptionFinalizable {
|
|
|
|
return self.priority(750.0)
|
|
|
|
}
|
|
|
|
internal func priorityMedium() -> ConstraintDescriptionFinalizable {
|
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
|
|
|
|
}
|
|
|
|
internal func priorityLow() -> ConstraintDescriptionFinalizable {
|
|
|
|
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,
|
|
|
|
priority: self.priority)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> ConstraintDescription {
|
|
|
|
if other.attributes != ConstraintAttributes.None {
|
|
|
|
let toLayoutAttributes = other.attributes.layoutAttributes
|
|
|
|
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
|
|
|
|
}
|
|
|
|
other.attributes = ConstraintAttributes.None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.toItem = other
|
|
|
|
self.relation = relation
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:16:34 +08:00
|
|
|
private func constrainTo(other: RelationTarget, relation: ConstraintRelation) -> ConstraintDescription {
|
|
|
|
return constrainTo(other.constraintItem, relation: relation)
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 15:38:50 +08:00
|
|
|
@available(iOS 7.0, *)
|
|
|
|
private func constrainTo(other: LayoutSupport, relation: ConstraintRelation) -> ConstraintDescription {
|
2015-10-12 13:16:34 +08:00
|
|
|
return constrainTo(ConstraintItem(object: self, attributes: ConstraintAttributes.None), relation: relation)
|
2015-09-24 01:43:29 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 12:57:59 +08:00
|
|
|
private func constrainTo(other: FloatConvertible, relation: ConstraintRelation) -> ConstraintDescription {
|
|
|
|
self.constant = other.floatValue
|
2015-04-12 18:21:02 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
|
|
|
}
|
|
|
|
|
2015-09-24 01:43:29 +08:00
|
|
|
}
|