2014-07-25 12:24:17 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// SnapKit
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
|
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
|
|
|
|
2015-04-12 19:32:03 +08:00
|
|
|
/**
|
|
|
|
Used to expose API's for a Constraint
|
|
|
|
*/
|
2015-04-15 19:31:03 +08:00
|
|
|
public class Constraint {
|
2015-04-11 19:39:12 +08:00
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
public func install() -> [LayoutConstraint] { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func uninstall() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func activate() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func deactivate() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
public func updateOffset(amount: Float) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updateOffset(amount: Double) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updateOffset(amount: CGFloat) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updateOffset(amount: Int) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updateOffset(amount: UInt) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updateOffset(amount: CGPoint) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updateOffset(amount: CGSize) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updateOffset(amount: EdgeInsets) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
public func updateInsets(amount: EdgeInsets) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
public func updatePriority(priority: Float) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriority(priority: Double) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriority(priority: CGFloat) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriority(priority: UInt) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriority(priority: Int) -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriorityRequired() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
2015-04-12 19:32:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used internally to implement a ConcreteConstraint
|
|
|
|
*/
|
2015-04-22 20:31:40 +08:00
|
|
|
internal class ConcreteConstraint: Constraint {
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: Float) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.constant = amount
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: Double) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updateOffset(Float(amount))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: CGFloat) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updateOffset(Float(amount))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: Int) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updateOffset(Float(amount))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: UInt) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updateOffset(Float(amount))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: CGPoint) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.constant = amount
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: CGSize) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.constant = amount
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateOffset(amount: EdgeInsets) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.constant = amount
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updateInsets(amount: EdgeInsets) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.constant = EdgeInsets(top: amount.top, left: amount.left, bottom: -amount.bottom, right: -amount.right)
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriority(priority: Float) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.priority = priority
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriority(priority: Double) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updatePriority(Float(priority))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriority(priority: CGFloat) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updatePriority(Float(priority))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriority(priority: UInt) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updatePriority(Float(priority))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriority(priority: Int) -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updatePriority(Float(priority))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriorityRequired() -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updatePriority(Float(1000.0))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriorityHigh() -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updatePriority(Float(750.0))
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriorityMedium() -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
#if os(iOS)
|
|
|
|
self.updatePriority(Float(500.0))
|
|
|
|
#else
|
|
|
|
self.updatePriority(Float(501.0))
|
|
|
|
#endif
|
|
|
|
}
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func updatePriorityLow() -> Void {
|
2015-04-12 19:32:03 +08:00
|
|
|
self.updatePriority(Float(250.0))
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func install() -> [LayoutConstraint] {
|
2015-04-12 18:21:02 +08:00
|
|
|
return self.installOnView(updateExisting: false)
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func uninstall() -> Void {
|
2015-04-12 18:21:02 +08:00
|
|
|
self.uninstallFromView()
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
internal override func activate() -> Void {
|
2015-04-12 18:21:02 +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-15 19:31:03 +08:00
|
|
|
internal override func deactivate() -> Void {
|
2015-04-12 18:21:02 +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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private let fromItem: ConstraintItem
|
|
|
|
private let toItem: ConstraintItem
|
|
|
|
private let relation: ConstraintRelation
|
2015-04-12 19:32:03 +08:00
|
|
|
private let multiplier: Float
|
|
|
|
private var constant: Any {
|
|
|
|
didSet {
|
|
|
|
if let installInfo = self.installInfo {
|
|
|
|
for layoutConstraint in installInfo.layoutConstraints.allObjects as! [LayoutConstraint] {
|
|
|
|
let attribute = (layoutConstraint.secondAttribute == .NotAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute
|
|
|
|
layoutConstraint.constant = attribute.snp_constantForValue(self.constant)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private var priority: Float {
|
|
|
|
didSet {
|
|
|
|
if let installInfo = self.installInfo {
|
|
|
|
for layoutConstraint in installInfo.layoutConstraints.allObjects as! [LayoutConstraint] {
|
|
|
|
layoutConstraint.priority = self.priority
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-04-12 19:32:03 +08:00
|
|
|
private var installInfo: ConcreteConstraintInstallInfo? = nil
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
internal init(fromItem: ConstraintItem, toItem: ConstraintItem, relation: ConstraintRelation, constant: Any, multiplier: Float, priority: Float) {
|
|
|
|
self.fromItem = fromItem
|
|
|
|
self.toItem = toItem
|
|
|
|
self.relation = relation
|
|
|
|
self.constant = constant
|
|
|
|
self.multiplier = multiplier
|
|
|
|
self.priority = priority
|
|
|
|
}
|
|
|
|
|
|
|
|
internal func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
|
|
|
|
var installOnView: View? = nil
|
|
|
|
if self.toItem.view != nil {
|
|
|
|
installOnView = closestCommonSuperviewBetween(self.fromItem.view, self.toItem.view)
|
|
|
|
if installOnView == nil {
|
|
|
|
NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
installOnView = self.fromItem.view?.superview
|
|
|
|
if installOnView == nil {
|
|
|
|
if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
|
|
|
|
installOnView = self.fromItem.view
|
|
|
|
}
|
|
|
|
|
|
|
|
if installOnView == nil {
|
|
|
|
NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let installedOnView = self.installInfo?.view {
|
|
|
|
if installedOnView != installOnView {
|
|
|
|
NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
|
|
|
|
}
|
|
|
|
|
|
|
|
var newLayoutConstraints = [LayoutConstraint]()
|
|
|
|
let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
|
|
|
|
let layoutToAttributes = self.toItem.attributes.layoutAttributes
|
|
|
|
|
|
|
|
// get layout from
|
|
|
|
let layoutFrom: View? = self.fromItem.view
|
|
|
|
|
|
|
|
// get layout relation
|
|
|
|
let layoutRelation: NSLayoutRelation = self.relation.layoutRelation
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
// get layout to
|
|
|
|
var layoutTo: View? = self.toItem.view
|
|
|
|
if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
|
|
|
|
layoutTo = installOnView
|
|
|
|
}
|
|
|
|
|
|
|
|
// create layout constraint
|
|
|
|
let layoutConstraint = LayoutConstraint(
|
|
|
|
item: layoutFrom!,
|
|
|
|
attribute: layoutFromAttribute,
|
|
|
|
relatedBy: layoutRelation,
|
|
|
|
toItem: layoutTo,
|
|
|
|
attribute: layoutToAttribute,
|
|
|
|
multiplier: CGFloat(self.multiplier),
|
|
|
|
constant: layoutConstant)
|
|
|
|
|
|
|
|
// set priority
|
|
|
|
layoutConstraint.priority = self.priority
|
|
|
|
|
|
|
|
// set constraint
|
|
|
|
layoutConstraint.snp_constraint = self
|
|
|
|
|
|
|
|
newLayoutConstraints.append(layoutConstraint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// special logic for updating
|
|
|
|
if updateExisting {
|
|
|
|
// get existing constraints for this view
|
|
|
|
let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
|
|
|
|
|
|
|
|
// array that will contain only new layout constraints to keep
|
|
|
|
var newLayoutConstraintsToKeep = [LayoutConstraint]()
|
|
|
|
|
|
|
|
// begin looping
|
|
|
|
for layoutConstraint in newLayoutConstraints {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
// otherwise add this layout constraint to new keep list
|
|
|
|
else {
|
|
|
|
newLayoutConstraintsToKeep.append(layoutConstraint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set constraints to only new ones
|
|
|
|
newLayoutConstraints = newLayoutConstraintsToKeep
|
|
|
|
}
|
|
|
|
|
|
|
|
// add constraints
|
|
|
|
installOnView!.addConstraints(newLayoutConstraints)
|
|
|
|
|
|
|
|
// set install info
|
2015-04-12 19:32:03 +08:00
|
|
|
self.installInfo = ConcreteConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
// store which layout constraints are installed for this constraint
|
|
|
|
for layoutConstraint in newLayoutConstraints {
|
|
|
|
self.installInfo!.layoutConstraints.addObject(layoutConstraint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the layout constraints against the layout from view
|
|
|
|
layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
|
|
|
|
|
|
|
|
// return the new constraints
|
|
|
|
return newLayoutConstraints
|
|
|
|
}
|
|
|
|
|
|
|
|
internal func uninstallFromView() {
|
|
|
|
if let installInfo = self.installInfo,
|
|
|
|
let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
|
|
|
|
|
|
|
|
if installedLayoutConstraints.count > 0 {
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
self.installInfo = nil
|
|
|
|
}
|
2015-04-11 19:39:12 +08:00
|
|
|
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-04-12 19:32:03 +08:00
|
|
|
private struct ConcreteConstraintInstallInfo {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
weak var view: View? = nil
|
|
|
|
let layoutConstraints: NSHashTable
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NSLayoutAttribute {
|
|
|
|
|
|
|
|
private func snp_constantForValue(value: Any?) -> CGFloat {
|
|
|
|
// Float
|
|
|
|
if let float = value as? Float {
|
|
|
|
return CGFloat(float)
|
|
|
|
}
|
|
|
|
// Double
|
|
|
|
else if let double = value as? Double {
|
|
|
|
return CGFloat(double)
|
|
|
|
}
|
|
|
|
// UInt
|
|
|
|
else if let int = value as? Int {
|
|
|
|
return CGFloat(int)
|
|
|
|
}
|
|
|
|
// Int
|
|
|
|
else if let uint = value as? UInt {
|
|
|
|
return CGFloat(uint)
|
|
|
|
}
|
|
|
|
// CGFloat
|
|
|
|
else if let float = value as? CGFloat {
|
|
|
|
return float
|
|
|
|
}
|
|
|
|
// CGSize
|
|
|
|
else if let size = value as? CGSize {
|
|
|
|
if self == .Width {
|
|
|
|
return size.width
|
|
|
|
} else if self == .Height {
|
|
|
|
return size.height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// CGPoint
|
|
|
|
else if let point = value as? CGPoint {
|
|
|
|
#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 point.x
|
|
|
|
case .Trailing, .TrailingMargin: return point.x
|
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
|
|
|
}
|
2015-04-15 19:17:20 +08:00
|
|
|
#else
|
2015-04-12 18:21:02 +08:00
|
|
|
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 point.x
|
|
|
|
case .Trailing: return point.x
|
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// EdgeInsets
|
|
|
|
else if let insets = value as? EdgeInsets {
|
|
|
|
#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)
|
|
|
|
}
|
2015-04-15 19:17:20 +08:00
|
|
|
#else
|
2015-04-12 18:21:02 +08:00
|
|
|
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
|
2015-04-15 19:17:20 +08:00
|
|
|
case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
|
|
|
|
case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : -insets.left
|
2015-04-12 18:21:02 +08:00
|
|
|
case .Width, .Height, .NotAnAttribute: return CGFloat(0)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return CGFloat(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func closestCommonSuperviewBetween(fromView: View?, toView: View?) -> View? {
|
|
|
|
var views = Set<View>()
|
|
|
|
var fromView = fromView
|
|
|
|
var toView = toView
|
|
|
|
do {
|
|
|
|
if let view = toView {
|
|
|
|
if views.contains(view) {
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
views.insert(view)
|
|
|
|
toView = view.superview
|
|
|
|
}
|
|
|
|
if let view = fromView {
|
|
|
|
if views.contains(view) {
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
views.insert(view)
|
|
|
|
fromView = view.superview
|
|
|
|
}
|
|
|
|
} while (fromView != nil || toView != nil)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-12 19:32:03 +08:00
|
|
|
private func ==(left: ConcreteConstraint, right: ConcreteConstraint) -> Bool {
|
2015-04-12 18:21:02 +08:00
|
|
|
return (left.fromItem == right.fromItem &&
|
|
|
|
left.toItem == right.toItem &&
|
|
|
|
left.relation == right.relation &&
|
|
|
|
left.multiplier == right.multiplier &&
|
|
|
|
left.priority == right.priority)
|
2015-04-12 19:32:03 +08:00
|
|
|
}
|