SnapKit/Source/Constraint.swift

321 lines
12 KiB
Swift
Raw Normal View History

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
#if os(iOS) || os(tvOS)
2016-01-26 18:49:04 +08:00
import UIKit
#else
2016-01-26 18:49:04 +08:00
import AppKit
#endif
2014-07-25 12:24:17 +08:00
2016-01-26 18:49:04 +08:00
public class Constraint {
2015-04-11 19:39:12 +08:00
2016-01-26 18:49:04 +08:00
internal let sourceLocation: (String, UInt)
2016-01-26 18:49:04 +08:00
private let from: ConstraintItem
private let to: ConstraintItem
private let relation: ConstraintRelation
private let multiplier: ConstraintMultiplierTarget
private var constant: ConstraintConstantTarget
private var priority: ConstraintPriorityTarget
private var installInfo: ConstraintInstallInfo? = nil
2016-01-26 18:49:04 +08:00
// MARK: Initialization
2016-01-26 18:49:04 +08:00
internal init(from: ConstraintItem,
to: ConstraintItem,
relation: ConstraintRelation,
sourceLocation: (String, UInt),
multiplier: ConstraintMultiplierTarget,
constant: ConstraintConstantTarget,
priority: ConstraintPriorityTarget) {
self.from = from
self.to = to
self.relation = relation
self.sourceLocation = sourceLocation
self.multiplier = multiplier
self.constant = constant
self.priority = priority
}
2016-01-26 18:49:04 +08:00
// MARK: Public
2016-01-26 18:49:04 +08:00
public func install() -> [NSLayoutConstraint] {
return self.installIfNeeded(updateExisting: false)
}
2016-01-26 18:49:04 +08:00
public func uninstall() {
self.uninstallIfNeeded()
}
2016-01-26 18:49:04 +08:00
@available(iOS 8.0, OSX 10.10, *)
public func activate() {
self.activateIfNeeded()
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
@available(iOS 8.0, OSX 10.10, *)
public func deactivate() {
self.deactivateIfNeeded()
}
2015-04-12 18:21:02 +08:00
2016-01-26 18:49:04 +08:00
// MARK: Internal
2015-04-12 18:21:02 +08:00
2016-06-15 09:49:49 +08:00
internal func installIfNeeded(updateExisting: Bool = false) -> [NSLayoutConstraint] {
2016-01-26 18:49:04 +08:00
let installOnView: ConstraintView?
if let view = self.to.view {
guard let closestSuperview = closestCommonSuperviewFromView(self.from.view, toView: view) else {
fatalError("Cannot Install Constraint. No common superview. (\(self.sourceLocation.0), \(self.sourceLocation.1))")
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
installOnView = closestSuperview
2016-06-15 09:49:49 +08:00
} else if self.from.attributes.isSubset(of: ConstraintAttributes.Width + ConstraintAttributes.Height) {
2016-01-26 18:49:04 +08:00
installOnView = self.from.view
2015-04-12 18:21:02 +08:00
} else {
2016-01-26 18:49:04 +08:00
guard let superview = self.from.view?.superview else {
fatalError("Cannot Install Constraint. No superview. (\(self.sourceLocation.0), \(self.sourceLocation.1))")
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
installOnView = superview
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
guard self.installInfo?.view == nil ||
self.installInfo?.view == installOnView else {
fatalError("Cannot Install Constraint. Already installed on different view. (\(self.sourceLocation.0), \(self.sourceLocation.1))")
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
// setup array to store new layout constraints
2015-04-12 18:21:02 +08:00
var newLayoutConstraints = [LayoutConstraint]()
2016-01-26 18:49:04 +08:00
// get attributes
let layoutFromAttributes = self.from.attributes.layoutAttributes
let layoutToAttributes = self.to.attributes.layoutAttributes
2015-04-12 18:21:02 +08:00
// get layout from
2016-01-26 18:49:04 +08:00
let layoutFrom: ConstraintView = self.from.view!
2015-04-12 18:21:02 +08:00
2016-01-26 18:49:04 +08:00
// get relation
let layoutRelation = self.relation.layoutRelation
2015-04-12 18:21:02 +08:00
for layoutFromAttribute in layoutFromAttributes {
// get layout to attribute
let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
// get layout constant
2016-01-26 18:49:04 +08:00
let layoutConstant: CGFloat = self.constant.layoutConstantForLayoutAttribute(layoutToAttribute)
2015-04-12 18:21:02 +08:00
// get layout to
#if os(iOS) || os(tvOS)
2016-01-26 18:49:04 +08:00
var layoutTo: AnyObject? = self.to.view ?? self.to.layoutSupport
#else
2016-01-26 18:49:04 +08:00
var layoutTo: AnyObject? = self.to.view
#endif
2016-06-15 09:49:49 +08:00
if layoutTo == nil && layoutToAttribute != .width && layoutToAttribute != .height {
2015-04-12 18:21:02 +08:00
layoutTo = installOnView
}
// create layout constraint
let layoutConstraint = LayoutConstraint(
2016-01-26 18:49:04 +08:00
item: layoutFrom,
2015-04-12 18:21:02 +08:00
attribute: layoutFromAttribute,
relatedBy: layoutRelation,
toItem: layoutTo,
attribute: layoutToAttribute,
2016-01-26 18:49:04 +08:00
multiplier: self.multiplier.constraintMultiplierTargetValue,
constant: layoutConstant
)
2015-04-12 18:21:02 +08:00
// set priority
2016-01-26 18:49:04 +08:00
layoutConstraint.priority = self.priority.constraintPriorityTargetValue
2015-04-12 18:21:02 +08:00
// set constraint
2016-01-26 18:49:04 +08:00
layoutConstraint.constraint = self
2015-04-12 18:21:02 +08:00
2016-01-26 18:49:04 +08:00
// append
2015-04-12 18:21:02 +08:00
newLayoutConstraints.append(layoutConstraint)
}
2016-01-26 18:49:04 +08:00
// updating logic
2015-04-12 18:21:02 +08:00
if updateExisting {
2016-01-26 18:49:04 +08:00
2015-04-12 18:21:02 +08:00
// get existing constraints for this view
2016-06-15 09:49:49 +08:00
let existingLayoutConstraints = layoutFrom.snp.installedLayoutConstraints.reversed()
2015-04-12 18:21:02 +08:00
// 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
}
2016-01-26 18:49:04 +08:00
// otherwise add this layout constraint to new keep list
2015-04-12 18:21:02 +08:00
else {
newLayoutConstraintsToKeep.append(layoutConstraint)
}
}
// set constraints to only new ones
newLayoutConstraints = newLayoutConstraintsToKeep
}
// add constraints
2016-01-26 18:49:04 +08:00
#if SNAPKIT_DEPLOYMENT_LEGACY && (os(iOS) || os(tvOS))
if #available(iOS 8.0, *) {
NSLayoutConstraint.activate(newLayoutConstraints)
2016-01-26 18:49:04 +08:00
} else {
installOnView?.addConstraints(newLayoutConstraints)
}
#else
2016-06-15 09:49:49 +08:00
NSLayoutConstraint.activate(newLayoutConstraints)
#endif
2015-04-12 18:21:02 +08:00
// set install info
2016-08-02 15:56:16 +08:00
self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjects())
2015-04-12 18:21:02 +08:00
// store which layout constraints are installed for this constraint
for layoutConstraint in newLayoutConstraints {
2016-06-15 09:49:49 +08:00
self.installInfo!.layoutConstraints.add(layoutConstraint)
2015-04-12 18:21:02 +08:00
}
// store the layout constraints against the layout from view
2016-01-26 18:49:04 +08:00
layoutFrom.snp.appendInstalledLayoutConstraints(newLayoutConstraints)
2015-04-12 18:21:02 +08:00
return newLayoutConstraints
}
2016-01-26 18:49:04 +08:00
internal func uninstallIfNeeded() {
defer {
self.installInfo = nil
}
2016-08-02 15:56:16 +08:00
guard let installedLayoutConstraints = self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint],
installedLayoutConstraints.count > 0 else {
2016-01-26 18:49:04 +08:00
return
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
#if SNAPKIT_DEPLOYMENT_LEGACY && !os(OSX)
if #available(iOS 8.0, *) {
NSLayoutConstraint.deactivate(installedLayoutConstraints)
} else if let installedOnView = installInfo?.view {
2016-01-26 18:49:04 +08:00
installedOnView.removeConstraints(installedLayoutConstraints)
}
#else
2016-06-15 09:49:49 +08:00
NSLayoutConstraint.deactivate(installedLayoutConstraints)
2016-01-26 18:49:04 +08:00
#endif
// remove the constraints from the from item view
self.from.view?.snp.removeInstalledLayoutConstraints(installedLayoutConstraints)
2015-04-12 18:21:02 +08:00
}
2015-04-11 19:39:12 +08:00
2016-01-26 18:49:04 +08:00
internal func activateIfNeeded() {
guard self.installInfo != nil else {
_ = self.installIfNeeded()
2016-01-26 18:49:04 +08:00
return
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
#if SNAPKIT_DEPLOYMENT_LEGACY
guard #available(iOS 8.0, OSX 10.10, *) else {
_ = self.installIfNeeded()
2016-01-26 18:49:04 +08:00
return
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
#endif
2016-08-02 15:56:16 +08:00
guard let layoutConstraints = self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint],
layoutConstraints.count > 0 else {
return
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
2016-06-15 09:49:49 +08:00
NSLayoutConstraint.activate(layoutConstraints)
2016-01-26 18:49:04 +08:00
}
internal func deactivateIfNeeded() {
#if SNAPKIT_DEPLOYMENT_LEGACY
guard #available(iOS 8.0, OSX 10.10, *) else {
return
}
#endif
2016-08-02 15:56:16 +08:00
guard let layoutConstraints = self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint],
layoutConstraints.count > 0 else {
return
2015-04-12 18:21:02 +08:00
}
2016-06-15 09:49:49 +08:00
NSLayoutConstraint.deactivate(layoutConstraints)
2016-01-26 18:49:04 +08:00
}
}
private final class ConstraintInstallInfo {
private weak var view: ConstraintView? = nil
2016-08-02 15:56:16 +08:00
private let layoutConstraints: NSHashTable<AnyObject>
2016-01-26 18:49:04 +08:00
2016-08-02 15:56:16 +08:00
private init(view: ConstraintView?, layoutConstraints: NSHashTable<AnyObject>) {
2016-01-26 18:49:04 +08:00
self.view = view
self.layoutConstraints = layoutConstraints
2015-04-12 18:21:02 +08:00
}
2016-01-26 18:49:04 +08:00
2015-04-12 18:21:02 +08:00
}
2016-06-15 09:49:49 +08:00
private func closestCommonSuperviewFromView(_ fromView: ConstraintView?, toView: ConstraintView?) -> ConstraintView? {
2016-01-26 18:49:04 +08:00
var views = Set<ConstraintView>()
2015-04-12 18:21:02 +08:00
var fromView = fromView
var toView = toView
2015-06-17 19:09:54 +08:00
repeat {
2015-04-12 18:21:02 +08:00
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
}
2016-01-26 18:49:04 +08:00
private func ==(lhs: Constraint, rhs: Constraint) -> Bool {
return (lhs.from == rhs.from &&
lhs.to == rhs.to &&
lhs.relation == rhs.relation &&
2016-06-15 09:49:49 +08:00
lhs.multiplier.constraintMultiplierTargetValue == rhs.multiplier.constraintMultiplierTargetValue &&
lhs.priority.constraintPriorityTargetValue == rhs.priority.constraintPriorityTargetValue)
2015-06-17 19:09:54 +08:00
}