2014-07-25 12:24:17 +08:00
|
|
|
//
|
2015-01-09 04:43:43 +08:00
|
|
|
// Snap
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2014-07-29 08:39:59 +08:00
|
|
|
// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2014-07-29 08:39:59 +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.
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-30 08:55:31 +08:00
|
|
|
#if os(iOS)
|
2014-07-25 12:24:17 +08:00
|
|
|
import UIKit
|
2014-07-30 08:55:31 +08:00
|
|
|
#else
|
|
|
|
import AppKit
|
|
|
|
#endif
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
/**
|
2015-04-11 19:39:12 +08:00
|
|
|
* Constraint is a protocol that exposes the public methods on a Constraint
|
|
|
|
*/
|
|
|
|
public protocol Constraint: class {
|
|
|
|
|
|
|
|
func install() -> [LayoutConstraint]
|
|
|
|
func uninstall() -> Void
|
|
|
|
func activate() -> Void
|
|
|
|
func deactivate() -> Void
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public protocol ConstraintFinalizable: class {
|
|
|
|
|
|
|
|
var constraint: Constraint { get }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConstraintPriortizable is a protocol that allows a constraint to be prioritized
|
2014-07-29 08:39:59 +08:00
|
|
|
*/
|
2015-04-11 19:39:12 +08:00
|
|
|
public protocol ConstraintPriortizable: ConstraintFinalizable {
|
|
|
|
|
|
|
|
func priority(priority: Float) -> ConstraintFinalizable
|
|
|
|
func priority(priority: Double) -> ConstraintFinalizable
|
|
|
|
func priority(priority: CGFloat) -> ConstraintFinalizable
|
|
|
|
func priority(priority: UInt) -> ConstraintFinalizable
|
|
|
|
func priority(priority: Int) -> ConstraintFinalizable
|
|
|
|
func priorityRequired() -> ConstraintFinalizable
|
|
|
|
func priorityHigh() -> ConstraintFinalizable
|
|
|
|
func priorityMedium() -> ConstraintFinalizable
|
|
|
|
func priorityLow() -> ConstraintFinalizable
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConstraintMultipliable is a protocol that allows a constraint to be multiplied
|
|
|
|
*/
|
|
|
|
public protocol ConstraintMultipliable: ConstraintPriortizable {
|
|
|
|
|
|
|
|
func multipliedBy(amount: Float) -> ConstraintPriortizable
|
|
|
|
func multipliedBy(amount: Double) -> ConstraintPriortizable
|
|
|
|
func multipliedBy(amount: CGFloat) -> ConstraintPriortizable
|
|
|
|
func multipliedBy(amount: Int) -> ConstraintPriortizable
|
|
|
|
func multipliedBy(amount: UInt) -> ConstraintPriortizable
|
|
|
|
|
|
|
|
func dividedBy(amount: Float) -> ConstraintPriortizable
|
|
|
|
func dividedBy(amount: Double) -> ConstraintPriortizable
|
|
|
|
func dividedBy(amount: CGFloat) -> ConstraintPriortizable
|
|
|
|
func dividedBy(amount: Int) -> ConstraintPriortizable
|
|
|
|
func dividedBy(amount: UInt) -> ConstraintPriortizable
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConstraintOffsetable is a protocol that allows a constraint to be offset
|
|
|
|
*/
|
|
|
|
public protocol ConstraintOffsetable: ConstraintMultipliable {
|
|
|
|
|
|
|
|
func offset(amount: Float) -> ConstraintMultipliable
|
|
|
|
func offset(amount: Double) -> ConstraintMultipliable
|
|
|
|
func offset(amount: CGFloat) -> ConstraintMultipliable
|
|
|
|
func offset(amount: Int) -> ConstraintMultipliable
|
|
|
|
func offset(amount: UInt) -> ConstraintMultipliable
|
|
|
|
func offset(amount: CGPoint) -> ConstraintMultipliable
|
|
|
|
func offset(amount: CGSize) -> ConstraintMultipliable
|
|
|
|
func offset(amount: EdgeInsets) -> ConstraintMultipliable
|
|
|
|
|
|
|
|
func insets(amount: EdgeInsets) -> ConstraintMultipliable
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConstraintRelatable is a protocol that allows a constraint to be set related to another item/constant by equalTo, lessThanOrEqualTo or greaterThanOrEqualTo
|
|
|
|
*/
|
|
|
|
public protocol ConstraintRelatable: class {
|
|
|
|
|
|
|
|
func equalTo(other: ConstraintItem) -> ConstraintOffsetable
|
|
|
|
func equalTo(other: View) -> ConstraintOffsetable
|
|
|
|
#if os(iOS)
|
|
|
|
func equalTo(other: UILayoutSupport) -> ConstraintOffsetable
|
|
|
|
#endif
|
|
|
|
func equalTo(other: Float) -> ConstraintMultipliable
|
|
|
|
func equalTo(other: Double) -> ConstraintMultipliable
|
|
|
|
func equalTo(other: CGFloat) -> ConstraintMultipliable
|
|
|
|
func equalTo(other: Int) -> ConstraintMultipliable
|
|
|
|
func equalTo(other: UInt) -> ConstraintMultipliable
|
|
|
|
func equalTo(other: CGSize) -> ConstraintMultipliable
|
|
|
|
func equalTo(other: CGPoint) -> ConstraintMultipliable
|
|
|
|
func equalTo(other: EdgeInsets) -> ConstraintMultipliable
|
|
|
|
|
|
|
|
func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable
|
|
|
|
func lessThanOrEqualTo(other: View) -> ConstraintOffsetable
|
|
|
|
#if os(iOS)
|
|
|
|
func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable
|
|
|
|
#endif
|
|
|
|
func lessThanOrEqualTo(other: Float) -> ConstraintMultipliable
|
|
|
|
func lessThanOrEqualTo(other: Double) -> ConstraintMultipliable
|
|
|
|
func lessThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable
|
|
|
|
func lessThanOrEqualTo(other: Int) -> ConstraintMultipliable
|
|
|
|
func lessThanOrEqualTo(other: UInt) -> ConstraintMultipliable
|
|
|
|
func lessThanOrEqualTo(other: CGSize) -> ConstraintMultipliable
|
|
|
|
func lessThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable
|
|
|
|
func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable
|
|
|
|
|
|
|
|
func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable
|
|
|
|
func greaterThanOrEqualTo(other: View) -> ConstraintOffsetable
|
|
|
|
#if os(iOS)
|
|
|
|
func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable
|
|
|
|
#endif
|
|
|
|
func greaterThanOrEqualTo(other: Float) -> ConstraintMultipliable
|
|
|
|
func greaterThanOrEqualTo(other: Double) -> ConstraintMultipliable
|
|
|
|
func greaterThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable
|
|
|
|
func greaterThanOrEqualTo(other: Int) -> ConstraintMultipliable
|
|
|
|
func greaterThanOrEqualTo(other: UInt) -> ConstraintMultipliable
|
|
|
|
func greaterThanOrEqualTo(other: CGSize) -> ConstraintMultipliable
|
|
|
|
func greaterThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable
|
|
|
|
func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConstraintExtendable is a protocol that allows a constraint to be extended
|
|
|
|
*/
|
|
|
|
public protocol ConstraintExtendable: ConstraintRelatable {
|
|
|
|
|
|
|
|
var left: ConstraintExtendable { get }
|
|
|
|
var top: ConstraintExtendable { get }
|
|
|
|
var bottom: ConstraintExtendable { get }
|
|
|
|
var leading: ConstraintExtendable { get }
|
|
|
|
var trailing: ConstraintExtendable { get }
|
|
|
|
var width: ConstraintExtendable { get }
|
|
|
|
var height: ConstraintExtendable { get }
|
|
|
|
var centerX: ConstraintExtendable { get }
|
|
|
|
var centerY: ConstraintExtendable { get }
|
|
|
|
var baseline: ConstraintExtendable { get }
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
var firstBaseline: ConstraintExtendable { get }
|
|
|
|
var leftMargin: ConstraintExtendable { get }
|
|
|
|
var rightMargin: ConstraintExtendable { get }
|
|
|
|
var topMargin: ConstraintExtendable { get }
|
|
|
|
var bottomMargin: ConstraintExtendable { get }
|
|
|
|
var leadingMargin: ConstraintExtendable { get }
|
|
|
|
var trailingMargin: ConstraintExtendable { get }
|
|
|
|
var centerXWithinMargins: ConstraintExtendable { get }
|
|
|
|
var centerYWithinMargins: ConstraintExtendable { get }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MutableConstraint is a single item that defines all the properties for a single ConstraintMaker chain
|
|
|
|
*/
|
|
|
|
final internal class MutableConstraint: Constraint, ConstraintExtendable, ConstraintOffsetable, ConstraintFinalizable {
|
|
|
|
|
|
|
|
var left: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Left) }
|
|
|
|
var top: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Top) }
|
|
|
|
var right: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Right) }
|
|
|
|
var bottom: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Bottom) }
|
|
|
|
var leading: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Leading) }
|
|
|
|
var trailing: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Trailing) }
|
|
|
|
var width: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Width) }
|
|
|
|
var height: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Height) }
|
|
|
|
var centerX: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterX) }
|
|
|
|
var centerY: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
|
|
|
|
var baseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
var firstBaseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
|
|
|
|
var leftMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) }
|
|
|
|
var rightMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) }
|
|
|
|
var topMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) }
|
|
|
|
var bottomMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) }
|
|
|
|
var leadingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
|
|
|
|
var trailingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
|
|
|
|
var centerXWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
|
|
|
|
var centerYWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
|
|
|
|
#endif
|
2014-07-29 08:39:59 +08:00
|
|
|
|
|
|
|
// MARK: initializer
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
init(fromItem: ConstraintItem) {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.fromItem = fromItem
|
2015-01-08 09:58:12 +08:00
|
|
|
self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: equalTo
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: ConstraintItem) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: View) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#if os(iOS)
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: UILayoutSupport) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2015-01-08 09:58:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: Float) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: Double) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: CGFloat) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: Int) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: UInt) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: CGSize) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: CGPoint) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func equalTo(other: EdgeInsets) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: lessThanOrEqualTo
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: View) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#if os(iOS)
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2015-01-08 09:58:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: Float) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: Double) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: Int) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: UInt) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: CGSize) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: greaterThanOrEqualTo
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: View) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#if os(iOS)
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2015-01-08 09:58:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: Float) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: Double) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: Int) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: UInt) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: CGSize) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: multiplier
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func multipliedBy(amount: Float) -> ConstraintPriortizable {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.multiplier = amount
|
|
|
|
return self
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func multipliedBy(amount: Double) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func multipliedBy(amount: CGFloat) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func multipliedBy(amount: Int) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func multipliedBy(amount: UInt) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func dividedBy(amount: Float) -> ConstraintPriortizable {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.multiplier = 1.0 / amount;
|
|
|
|
return self
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func dividedBy(amount: Double) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func dividedBy(amount: CGFloat) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func dividedBy(amount: Int) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func dividedBy(amount: UInt) -> ConstraintPriortizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: priority
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func priority(priority: Float) -> ConstraintFinalizable {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.priority = priority
|
|
|
|
return self
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priority(priority: Double) -> ConstraintFinalizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priority(priority: CGFloat) -> ConstraintFinalizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priority(priority: UInt) -> ConstraintFinalizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priority(priority: Int) -> ConstraintFinalizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priorityRequired() -> ConstraintFinalizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(1000.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priorityHigh() -> ConstraintFinalizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(750.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priorityMedium() -> ConstraintFinalizable {
|
2015-04-11 15:44:11 +08:00
|
|
|
#if os(iOS)
|
2015-04-11 15:43:20 +08:00
|
|
|
return self.priority(500.0)
|
2015-04-11 15:44:11 +08:00
|
|
|
#else
|
|
|
|
return self.priority(501.0)
|
|
|
|
#endif
|
2015-04-11 15:43:20 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func priorityLow() -> ConstraintFinalizable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(250.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: offset
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: Float) -> ConstraintMultipliable {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: Double) -> ConstraintMultipliable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.offset(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: CGFloat) -> ConstraintMultipliable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.offset(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: Int) -> ConstraintMultipliable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.offset(Float(amount))
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: UInt) -> ConstraintMultipliable {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.offset(Float(amount))
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: CGPoint) -> ConstraintMultipliable {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: CGSize) -> ConstraintMultipliable {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
func offset(amount: EdgeInsets) -> ConstraintMultipliable {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: insets
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func insets(amount: EdgeInsets) -> ConstraintMultipliable {
|
2014-07-29 19:50:50 +08:00
|
|
|
self.offset = amount
|
2014-07-29 08:39:59 +08:00
|
|
|
return self
|
|
|
|
}
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
// MARK: Constraint
|
|
|
|
|
|
|
|
var constraint: Constraint {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:07:28 +08:00
|
|
|
// MARK: install / uninstall
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func install() -> [LayoutConstraint] {
|
2015-01-14 09:07:28 +08:00
|
|
|
return self.installOnView(updateExisting: false)
|
|
|
|
}
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func uninstall() {
|
2015-01-14 09:07:28 +08:00
|
|
|
self.uninstallFromView()
|
|
|
|
}
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func activate() {
|
2015-04-11 17:44:46 +08:00
|
|
|
if NSLayoutConstraint.respondsToSelector("activateConstraints:") && self.installInfo != nil {
|
|
|
|
let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
|
|
|
|
if layoutConstraints.count > 0 {
|
|
|
|
NSLayoutConstraint.activateConstraints(layoutConstraints)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.install()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func deactivate() {
|
2015-04-11 17:44:46 +08:00
|
|
|
if NSLayoutConstraint.respondsToSelector("deactivateConstraints:") && self.installInfo != nil {
|
|
|
|
let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
|
|
|
|
if layoutConstraints.count > 0 {
|
|
|
|
NSLayoutConstraint.deactivateConstraints(layoutConstraints)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.uninstall()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
|
2014-07-29 08:39:59 +08:00
|
|
|
var installOnView: View? = nil
|
2014-08-05 09:23:13 +08:00
|
|
|
if self.toItem.view != nil {
|
2015-04-11 19:39:12 +08:00
|
|
|
installOnView = MutableConstraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
|
2014-08-05 09:23:13 +08:00
|
|
|
if installOnView == nil {
|
2014-07-29 08:39:59 +08:00
|
|
|
NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
installOnView = self.fromItem.view?.superview
|
2014-08-05 09:23:13 +08:00
|
|
|
if installOnView == nil {
|
2015-01-26 20:27:14 +08:00
|
|
|
if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
|
|
|
|
installOnView = self.fromItem.view
|
|
|
|
}
|
|
|
|
|
2015-02-17 08:41:45 +08:00
|
|
|
if installOnView == nil {
|
2015-01-26 20:27:14 +08:00
|
|
|
NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
if let installedOnView = self.installInfo?.view {
|
|
|
|
if installedOnView != installOnView {
|
2015-02-17 08:41:45 +08:00
|
|
|
NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
2015-04-11 13:41:05 +08:00
|
|
|
return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
|
2015-02-17 08:41:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
var newLayoutConstraints = [LayoutConstraint]()
|
2014-07-29 08:39:59 +08:00
|
|
|
let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
|
|
|
|
let layoutToAttributes = self.toItem.attributes.layoutAttributes
|
|
|
|
|
|
|
|
// get layout from
|
|
|
|
let layoutFrom: View? = self.fromItem.view
|
|
|
|
|
|
|
|
// get layout relation
|
2014-08-05 09:23:13 +08:00
|
|
|
let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
|
2014-07-29 08:39:59 +08:00
|
|
|
|
|
|
|
for layoutFromAttribute in layoutFromAttributes {
|
|
|
|
// get layout to attribute
|
|
|
|
let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
|
|
|
|
|
|
|
|
// get layout constant
|
|
|
|
var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
|
|
|
|
layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
|
|
|
|
|
2014-08-05 14:51:42 +08:00
|
|
|
// get layout to
|
|
|
|
var layoutTo: View? = self.toItem.view
|
|
|
|
if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
|
|
|
|
layoutTo = installOnView
|
|
|
|
}
|
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// create layout constraint
|
|
|
|
let layoutConstraint = LayoutConstraint(
|
2014-09-23 12:28:00 +08:00
|
|
|
item: layoutFrom!,
|
2014-07-29 08:39:59 +08:00
|
|
|
attribute: layoutFromAttribute,
|
|
|
|
relatedBy: layoutRelation,
|
|
|
|
toItem: layoutTo,
|
|
|
|
attribute: layoutToAttribute,
|
|
|
|
multiplier: CGFloat(self.multiplier),
|
|
|
|
constant: layoutConstant)
|
|
|
|
|
|
|
|
// set priority
|
|
|
|
layoutConstraint.priority = self.priority
|
|
|
|
|
|
|
|
// set constraint
|
2015-04-11 13:41:05 +08:00
|
|
|
layoutConstraint.snp_constraint = self
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-02-17 06:11:58 +08:00
|
|
|
newLayoutConstraints.append(layoutConstraint)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
2015-01-08 14:20:34 +08:00
|
|
|
// special logic for updating
|
|
|
|
if updateExisting {
|
|
|
|
// get existing constraints for this view
|
|
|
|
let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
|
|
|
|
|
2015-02-17 06:11:58 +08:00
|
|
|
// array that will contain only new layout constraints to keep
|
2015-04-11 13:41:05 +08:00
|
|
|
var newLayoutConstraintsToKeep = [LayoutConstraint]()
|
2015-02-17 06:11:58 +08:00
|
|
|
|
2015-01-08 14:20:34 +08:00
|
|
|
// begin looping
|
2015-02-17 06:11:58 +08:00
|
|
|
for layoutConstraint in newLayoutConstraints {
|
2015-01-08 14:20:34 +08:00
|
|
|
// layout constraint that should be updated
|
|
|
|
var updateLayoutConstraint: LayoutConstraint? = nil
|
|
|
|
|
|
|
|
// loop through existing and check for match
|
|
|
|
for existingLayoutConstraint in existingLayoutConstraints {
|
|
|
|
if existingLayoutConstraint == layoutConstraint {
|
|
|
|
updateLayoutConstraint = existingLayoutConstraint
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have existing one lets just update the constant
|
|
|
|
if updateLayoutConstraint != nil {
|
|
|
|
updateLayoutConstraint!.constant = layoutConstraint.constant
|
|
|
|
}
|
2015-02-17 06:11:58 +08:00
|
|
|
// otherwise add this layout constraint to new keep list
|
2015-01-08 14:20:34 +08:00
|
|
|
else {
|
2015-02-17 06:11:58 +08:00
|
|
|
newLayoutConstraintsToKeep.append(layoutConstraint)
|
2015-01-08 14:20:34 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 06:11:58 +08:00
|
|
|
|
2015-01-08 14:20:34 +08:00
|
|
|
// set constraints to only new ones
|
2015-02-17 06:11:58 +08:00
|
|
|
newLayoutConstraints = newLayoutConstraintsToKeep
|
2015-01-08 14:20:34 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 09:16:26 +08:00
|
|
|
// add constraints
|
2015-02-17 06:11:58 +08:00
|
|
|
installOnView!.addConstraints(newLayoutConstraints)
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
// set install info
|
|
|
|
self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
|
2015-02-17 06:11:58 +08:00
|
|
|
|
|
|
|
// store which layout constraints are installed for this constraint
|
|
|
|
for layoutConstraint in newLayoutConstraints {
|
2015-04-11 13:41:05 +08:00
|
|
|
self.installInfo!.layoutConstraints.addObject(layoutConstraint)
|
2015-01-26 20:27:14 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 14:32:33 +08:00
|
|
|
// store the layout constraints against the layout from view
|
2015-04-11 13:41:05 +08:00
|
|
|
layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
|
2015-02-17 06:11:58 +08:00
|
|
|
|
|
|
|
// return the new constraints
|
|
|
|
return newLayoutConstraints
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
func uninstallFromView() {
|
2015-04-11 13:41:05 +08:00
|
|
|
if let installInfo = self.installInfo,
|
|
|
|
let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
if installedLayoutConstraints.count > 0 {
|
|
|
|
|
2015-04-11 14:32:33 +08:00
|
|
|
if let installedOnView = installInfo.view {
|
|
|
|
// remove the constraints from the UIView's storage
|
|
|
|
installedOnView.removeConstraints(installedLayoutConstraints)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the constraints from the from item view
|
|
|
|
if let fromView = self.fromItem.view {
|
|
|
|
fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter {
|
|
|
|
return !contains(installedLayoutConstraints, $0)
|
|
|
|
}
|
2015-01-14 09:16:26 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 13:41:05 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 13:41:05 +08:00
|
|
|
self.installInfo = nil
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: private
|
|
|
|
|
|
|
|
private let fromItem: ConstraintItem
|
|
|
|
private var toItem: ConstraintItem
|
|
|
|
private var relation: ConstraintRelation?
|
2015-04-11 13:41:05 +08:00
|
|
|
private var constant: Any = Float(0.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
private var multiplier: Float = 1.0
|
|
|
|
private var priority: Float = 1000.0
|
2015-04-11 13:41:05 +08:00
|
|
|
private var offset: Any = Float(0.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
private var installInfo: ConstraintInstallInfo?
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
private func addConstraint(attributes: ConstraintAttributes) -> MutableConstraint {
|
2014-08-05 09:23:13 +08:00
|
|
|
if self.relation == nil {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.fromItem.attributes += attributes
|
|
|
|
}
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> MutableConstraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
if other.attributes != ConstraintAttributes.None {
|
2015-04-11 12:11:20 +08:00
|
|
|
let toLayoutAttributes = other.attributes.layoutAttributes
|
2014-07-29 08:39:59 +08:00
|
|
|
if toLayoutAttributes.count > 1 {
|
2015-04-11 12:11:20 +08:00
|
|
|
let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
|
2014-07-29 08:39:59 +08:00
|
|
|
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-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: View, relation: ConstraintRelation) -> MutableConstraint {
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
2015-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> MutableConstraint {
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#endif
|
2015-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: Float, relation: ConstraintRelation) -> MutableConstraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: Double, relation: ConstraintRelation) -> MutableConstraint {
|
2014-10-03 09:59:47 +08:00
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: CGSize, relation: ConstraintRelation) -> MutableConstraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> MutableConstraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> MutableConstraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
|
2015-04-11 12:11:20 +08:00
|
|
|
var views = Set<View>()
|
2015-02-17 06:37:43 +08:00
|
|
|
var fromView = fromView
|
|
|
|
var toView = toView
|
|
|
|
do {
|
|
|
|
if let view = toView {
|
2015-04-11 12:11:20 +08:00
|
|
|
if views.contains(view) {
|
2015-02-17 06:37:43 +08:00
|
|
|
return view
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 12:11:20 +08:00
|
|
|
views.insert(view)
|
2015-02-17 06:37:43 +08:00
|
|
|
toView = view.superview
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-02-17 06:37:43 +08:00
|
|
|
if let view = fromView {
|
2015-04-11 12:11:20 +08:00
|
|
|
if views.contains(view) {
|
2015-02-17 06:37:43 +08:00
|
|
|
return view
|
|
|
|
}
|
2015-04-11 12:11:20 +08:00
|
|
|
views.insert(view)
|
2015-02-17 06:37:43 +08:00
|
|
|
fromView = view.superview
|
|
|
|
}
|
|
|
|
} while (fromView != nil || toView != nil)
|
|
|
|
|
|
|
|
return nil
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NSLayoutAttribute {
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
private func snp_offsetForValue(value: Any?) -> CGFloat {
|
2014-07-29 08:39:59 +08:00
|
|
|
// Float
|
|
|
|
if let float = value as? Float {
|
|
|
|
return CGFloat(float)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Double
|
|
|
|
else if let double = value as? Double {
|
|
|
|
return CGFloat(double)
|
|
|
|
}
|
|
|
|
// UInt
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let int = value as? Int {
|
|
|
|
return CGFloat(int)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Int
|
|
|
|
else if let uint = value as? UInt {
|
|
|
|
return CGFloat(uint)
|
|
|
|
}
|
|
|
|
// CGFloat
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let float = value as? CGFloat {
|
|
|
|
return float
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// CGSize
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let size = value as? CGSize {
|
|
|
|
if self == .Width {
|
|
|
|
return size.width
|
|
|
|
} else if self == .Height {
|
|
|
|
return size.height
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// CGPoint
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let point = value as? CGPoint {
|
2015-04-11 18:10:02 +08:00
|
|
|
#if os(iOS)
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
|
|
|
|
case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
|
|
|
|
case .Right, .RightMargin: return -point.x
|
|
|
|
case .Bottom, .BottomMargin: return -point.y
|
|
|
|
case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
|
|
|
|
case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
|
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 18:10:02 +08:00
|
|
|
#else
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX: return point.x
|
|
|
|
case .Top, .CenterY, .Baseline: return point.y
|
|
|
|
case .Right: return -point.x
|
|
|
|
case .Bottom: return -point.y
|
|
|
|
case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
|
|
|
|
case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
|
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// EdgeInsets
|
2014-07-30 08:55:31 +08:00
|
|
|
else if let insets = value as? EdgeInsets {
|
2015-04-11 18:10:02 +08:00
|
|
|
#if os(iOS)
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
|
|
|
|
case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
|
|
|
|
case .Right, .RightMargin: return -insets.right
|
|
|
|
case .Bottom, .BottomMargin: return -insets.bottom
|
|
|
|
case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
|
|
|
|
case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
|
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX: return insets.left
|
|
|
|
case .Top, .CenterY, .Baseline: return insets.top
|
|
|
|
case .Right: return -insets.right
|
|
|
|
case .Bottom: return -insets.bottom
|
|
|
|
case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
|
|
|
|
case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
|
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 18:10:02 +08:00
|
|
|
#endif
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return CGFloat(0)
|
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
private func snp_constantForValue(value: Any?) -> CGFloat {
|
2014-07-29 08:39:59 +08:00
|
|
|
// Float
|
|
|
|
if let float = value as? Float {
|
|
|
|
return CGFloat(float)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Double
|
|
|
|
else if let double = value as? Double {
|
|
|
|
return CGFloat(double)
|
|
|
|
}
|
|
|
|
// UInt
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let int = value as? Int {
|
|
|
|
return CGFloat(int)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Int
|
|
|
|
else if let uint = value as? UInt {
|
|
|
|
return CGFloat(uint)
|
|
|
|
}
|
|
|
|
// CGFloat
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let float = value as? CGFloat {
|
|
|
|
return float
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// CGSize
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let size = value as? CGSize {
|
|
|
|
if self == .Width {
|
|
|
|
return size.width
|
|
|
|
} else if self == .Height {
|
|
|
|
return size.height
|
|
|
|
}
|
|
|
|
}
|
2015-04-11 18:16:50 +08:00
|
|
|
// CGPoint
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let point = value as? CGPoint {
|
2015-04-11 18:10:02 +08:00
|
|
|
#if os(iOS)
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
|
|
|
|
case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
|
2015-04-11 18:16:50 +08:00
|
|
|
case .Right, .RightMargin: return point.x
|
|
|
|
case .Bottom, .BottomMargin: return point.y
|
|
|
|
case .Leading, .LeadingMargin: return point.x
|
|
|
|
case .Trailing, .TrailingMargin: return point.x
|
2015-04-11 18:10:02 +08:00
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 18:10:02 +08:00
|
|
|
#else
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX: return point.x
|
|
|
|
case .Top, .CenterY, .Baseline: return point.y
|
2015-04-11 18:16:50 +08:00
|
|
|
case .Right: return point.x
|
|
|
|
case .Bottom: return point.y
|
|
|
|
case .Leading: return point.x
|
|
|
|
case .Trailing: return point.x
|
2015-04-11 18:10:02 +08:00
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 18:16:50 +08:00
|
|
|
// EdgeInsets
|
2014-07-30 08:55:31 +08:00
|
|
|
else if let insets = value as? EdgeInsets {
|
2015-04-11 18:10:02 +08:00
|
|
|
#if os(iOS)
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
|
|
|
|
case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
|
2015-04-11 18:16:50 +08:00
|
|
|
case .Right, .RightMargin: return insets.right
|
|
|
|
case .Bottom, .BottomMargin: return insets.bottom
|
|
|
|
case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right
|
|
|
|
case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left
|
2015-04-11 18:10:02 +08:00
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
switch self {
|
|
|
|
case .Left, .CenterX: return insets.left
|
|
|
|
case .Top, .CenterY, .Baseline: return insets.top
|
2015-04-11 18:16:50 +08:00
|
|
|
case .Right: return insets.right
|
|
|
|
case .Bottom: return insets.bottom
|
|
|
|
case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right
|
|
|
|
case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left
|
2015-04-11 18:10:02 +08:00
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 18:10:02 +08:00
|
|
|
#endif
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return CGFloat(0);
|
|
|
|
}
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
private struct ConstraintInstallInfo {
|
|
|
|
|
2015-04-11 17:47:27 +08:00
|
|
|
weak var view: View? = nil
|
2015-04-11 13:41:05 +08:00
|
|
|
let layoutConstraints: NSHashTable
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-11 19:39:12 +08:00
|
|
|
internal func ==(left: MutableConstraint, right: MutableConstraint) -> Bool {
|
2015-01-08 14:04:29 +08:00
|
|
|
return (left.fromItem == right.fromItem &&
|
|
|
|
left.toItem == right.toItem &&
|
|
|
|
left.relation == right.relation &&
|
|
|
|
left.multiplier == right.multiplier &&
|
|
|
|
left.priority == right.priority)
|
2014-09-23 12:28:00 +08:00
|
|
|
}
|