Constraint.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. //
  2. // Snap
  3. //
  4. // Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. /**
  29. * Constraint is a single item that defines all the properties for a single ConstraintMaker chain
  30. */
  31. final public class Constraint {
  32. public var left: Constraint { return self.addConstraint(ConstraintAttributes.Left) }
  33. public var top: Constraint { return self.addConstraint(ConstraintAttributes.Top) }
  34. public var right: Constraint { return self.addConstraint(ConstraintAttributes.Right) }
  35. public var bottom: Constraint { return self.addConstraint(ConstraintAttributes.Bottom) }
  36. public var leading: Constraint { return self.addConstraint(ConstraintAttributes.Leading) }
  37. public var trailing: Constraint { return self.addConstraint(ConstraintAttributes.Trailing) }
  38. public var width: Constraint { return self.addConstraint(ConstraintAttributes.Width) }
  39. public var height: Constraint { return self.addConstraint(ConstraintAttributes.Height) }
  40. public var centerX: Constraint { return self.addConstraint(ConstraintAttributes.CenterX) }
  41. public var centerY: Constraint { return self.addConstraint(ConstraintAttributes.CenterY) }
  42. public var baseline: Constraint { return self.addConstraint(ConstraintAttributes.Baseline) }
  43. public var and: Constraint {
  44. if self.relation != nil {
  45. fatalError("And is semantic only and can only be used before a relation is set.")
  46. }
  47. return self
  48. }
  49. public var with: Constraint { return self }
  50. // MARK: initializer
  51. internal init(fromItem: ConstraintItem) {
  52. self.fromItem = fromItem
  53. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  54. }
  55. // MARK: equalTo
  56. public func equalTo(other: ConstraintItem) -> Constraint {
  57. return self.constrainTo(other, relation: .Equal)
  58. }
  59. public func equalTo(other: View) -> Constraint {
  60. return self.constrainTo(other, relation: .Equal)
  61. }
  62. #if os(iOS)
  63. public func equalTo(other: UILayoutSupport) -> Constraint {
  64. return self.constrainTo(other, relation: .Equal)
  65. }
  66. #endif
  67. public func equalTo(other: Float) -> Constraint {
  68. return self.constrainTo(other, relation: .Equal)
  69. }
  70. public func equalTo(other: Double) -> Constraint {
  71. return self.constrainTo(Float(other), relation: .Equal)
  72. }
  73. public func equalTo(other: CGFloat) -> Constraint {
  74. return self.constrainTo(Float(other), relation: .Equal)
  75. }
  76. public func equalTo(other: Int) -> Constraint {
  77. return self.constrainTo(Float(other), relation: .Equal)
  78. }
  79. public func equalTo(other: UInt) -> Constraint {
  80. return self.constrainTo(Float(other), relation: .Equal)
  81. }
  82. public func equalTo(other: CGSize) -> Constraint {
  83. return self.constrainTo(other, relation: .Equal)
  84. }
  85. public func equalTo(other: CGPoint) -> Constraint {
  86. return self.constrainTo(other, relation: .Equal)
  87. }
  88. public func equalTo(other: EdgeInsets) -> Constraint {
  89. return self.constrainTo(other, relation: .Equal)
  90. }
  91. // MARK: lessThanOrEqualTo
  92. public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
  93. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  94. }
  95. public func lessThanOrEqualTo(other: View) -> Constraint {
  96. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  97. }
  98. #if os(iOS)
  99. public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  100. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  101. }
  102. #endif
  103. public func lessThanOrEqualTo(other: Float) -> Constraint {
  104. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  105. }
  106. public func lessThanOrEqualTo(other: Double) -> Constraint {
  107. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  108. }
  109. public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
  110. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  111. }
  112. public func lessThanOrEqualTo(other: Int) -> Constraint {
  113. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  114. }
  115. public func lessThanOrEqualTo(other: UInt) -> Constraint {
  116. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  117. }
  118. public func lessThanOrEqualTo(other: CGSize) -> Constraint {
  119. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  120. }
  121. public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
  122. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  123. }
  124. public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
  125. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  126. }
  127. // MARK: greaterThanOrEqualTo
  128. public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
  129. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  130. }
  131. public func greaterThanOrEqualTo(other: View) -> Constraint {
  132. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  133. }
  134. #if os(iOS)
  135. public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  136. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  137. }
  138. #endif
  139. public func greaterThanOrEqualTo(other: Float) -> Constraint {
  140. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  141. }
  142. public func greaterThanOrEqualTo(other: Double) -> Constraint {
  143. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  144. }
  145. public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
  146. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  147. }
  148. public func greaterThanOrEqualTo(other: Int) -> Constraint {
  149. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  150. }
  151. public func greaterThanOrEqualTo(other: UInt) -> Constraint {
  152. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  153. }
  154. public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
  155. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  156. }
  157. public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
  158. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  159. }
  160. public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
  161. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  162. }
  163. // MARK: multiplier
  164. public func multipliedBy(amount: Float) -> Constraint {
  165. self.multiplier = amount
  166. return self
  167. }
  168. public func multipliedBy(amount: Double) -> Constraint {
  169. return self.multipliedBy(Float(amount))
  170. }
  171. public func multipliedBy(amount: CGFloat) -> Constraint {
  172. return self.multipliedBy(Float(amount))
  173. }
  174. public func multipliedBy(amount: Int) -> Constraint {
  175. return self.multipliedBy(Float(amount))
  176. }
  177. public func multipliedBy(amount: UInt) -> Constraint {
  178. return self.multipliedBy(Float(amount))
  179. }
  180. public func dividedBy(amount: Float) -> Constraint {
  181. self.multiplier = 1.0 / amount;
  182. return self
  183. }
  184. public func dividedBy(amount: Double) -> Constraint {
  185. return self.dividedBy(Float(amount))
  186. }
  187. public func dividedBy(amount: CGFloat) -> Constraint {
  188. return self.dividedBy(Float(amount))
  189. }
  190. public func dividedBy(amount: Int) -> Constraint {
  191. return self.dividedBy(Float(amount))
  192. }
  193. public func dividedBy(amount: UInt) -> Constraint {
  194. return self.dividedBy(Float(amount))
  195. }
  196. // MARK: priority
  197. public func priority(priority: Float) -> Constraint {
  198. self.priority = priority
  199. return self
  200. }
  201. public func priority(priority: Double) -> Constraint {
  202. return self.priority(Float(priority))
  203. }
  204. public func priority(priority: CGFloat) -> Constraint {
  205. return self.priority(Float(priority))
  206. }
  207. public func priority(priority: UInt) -> Constraint {
  208. return self.priority(Float(priority))
  209. }
  210. public func priority(priority: Int) -> Constraint {
  211. return self.priority(Float(priority))
  212. }
  213. public func priorityRequired() -> Constraint {
  214. return self.priority(1000.0)
  215. }
  216. public func priorityHigh() -> Constraint {
  217. return self.priority(750.0)
  218. }
  219. public func priorityMedium() -> Constraint {
  220. #if os(iOS)
  221. return self.priority(500.0)
  222. #else
  223. return self.priority(501.0)
  224. #endif
  225. }
  226. public func priorityLow() -> Constraint {
  227. return self.priority(250.0)
  228. }
  229. // MARK: offset
  230. public func offset(amount: Float) -> Constraint {
  231. self.offset = amount
  232. return self
  233. }
  234. public func offset(amount: Double) -> Constraint {
  235. return self.offset(Float(amount))
  236. }
  237. public func offset(amount: CGFloat) -> Constraint {
  238. return self.offset(Float(amount))
  239. }
  240. public func offset(amount: Int) -> Constraint {
  241. return self.offset(Float(amount))
  242. }
  243. public func offset(amount: UInt) -> Constraint {
  244. return self.offset(Float(amount))
  245. }
  246. public func offset(amount: CGPoint) -> Constraint {
  247. self.offset = amount
  248. return self
  249. }
  250. public func offset(amount: CGSize) -> Constraint {
  251. self.offset = amount
  252. return self
  253. }
  254. public func offset(amount: EdgeInsets) -> Constraint {
  255. self.offset = amount
  256. return self
  257. }
  258. // MARK: insets
  259. public func insets(amount: EdgeInsets) -> Constraint {
  260. self.offset = amount
  261. return self
  262. }
  263. // MARK: install / uninstall
  264. public func install() -> [LayoutConstraint] {
  265. return self.installOnView(updateExisting: false)
  266. }
  267. public func uninstall() {
  268. self.uninstallFromView()
  269. }
  270. public func activate() {
  271. if NSLayoutConstraint.respondsToSelector("activateConstraints:") && self.installInfo != nil {
  272. let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
  273. if layoutConstraints.count > 0 {
  274. NSLayoutConstraint.activateConstraints(layoutConstraints)
  275. }
  276. } else {
  277. self.install()
  278. }
  279. }
  280. public func deactivate() {
  281. if NSLayoutConstraint.respondsToSelector("deactivateConstraints:") && self.installInfo != nil {
  282. let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
  283. if layoutConstraints.count > 0 {
  284. NSLayoutConstraint.deactivateConstraints(layoutConstraints)
  285. }
  286. } else {
  287. self.uninstall()
  288. }
  289. }
  290. // MARK: internal
  291. internal func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
  292. var installOnView: View? = nil
  293. if self.toItem.view != nil {
  294. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  295. if installOnView == nil {
  296. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  297. return []
  298. }
  299. } else {
  300. installOnView = self.fromItem.view?.superview
  301. if installOnView == nil {
  302. if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
  303. installOnView = self.fromItem.view
  304. }
  305. if installOnView == nil {
  306. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  307. return []
  308. }
  309. }
  310. }
  311. if let installedOnView = self.installInfo?.view {
  312. if installedOnView != installOnView {
  313. NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
  314. return []
  315. }
  316. return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
  317. }
  318. var newLayoutConstraints = [LayoutConstraint]()
  319. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  320. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  321. // get layout from
  322. let layoutFrom: View? = self.fromItem.view
  323. // get layout relation
  324. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  325. for layoutFromAttribute in layoutFromAttributes {
  326. // get layout to attribute
  327. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  328. // get layout constant
  329. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  330. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  331. // get layout to
  332. var layoutTo: View? = self.toItem.view
  333. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  334. layoutTo = installOnView
  335. }
  336. // create layout constraint
  337. let layoutConstraint = LayoutConstraint(
  338. item: layoutFrom!,
  339. attribute: layoutFromAttribute,
  340. relatedBy: layoutRelation,
  341. toItem: layoutTo,
  342. attribute: layoutToAttribute,
  343. multiplier: CGFloat(self.multiplier),
  344. constant: layoutConstant)
  345. // set priority
  346. layoutConstraint.priority = self.priority
  347. // set constraint
  348. layoutConstraint.snp_constraint = self
  349. newLayoutConstraints.append(layoutConstraint)
  350. }
  351. // special logic for updating
  352. if updateExisting {
  353. // get existing constraints for this view
  354. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  355. // array that will contain only new layout constraints to keep
  356. var newLayoutConstraintsToKeep = [LayoutConstraint]()
  357. // begin looping
  358. for layoutConstraint in newLayoutConstraints {
  359. // layout constraint that should be updated
  360. var updateLayoutConstraint: LayoutConstraint? = nil
  361. // loop through existing and check for match
  362. for existingLayoutConstraint in existingLayoutConstraints {
  363. if existingLayoutConstraint == layoutConstraint {
  364. updateLayoutConstraint = existingLayoutConstraint
  365. break
  366. }
  367. }
  368. // if we have existing one lets just update the constant
  369. if updateLayoutConstraint != nil {
  370. updateLayoutConstraint!.constant = layoutConstraint.constant
  371. }
  372. // otherwise add this layout constraint to new keep list
  373. else {
  374. newLayoutConstraintsToKeep.append(layoutConstraint)
  375. }
  376. }
  377. // set constraints to only new ones
  378. newLayoutConstraints = newLayoutConstraintsToKeep
  379. }
  380. // add constraints
  381. installOnView!.addConstraints(newLayoutConstraints)
  382. // set install info
  383. self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
  384. // store which layout constraints are installed for this constraint
  385. for layoutConstraint in newLayoutConstraints {
  386. self.installInfo!.layoutConstraints.addObject(layoutConstraint)
  387. }
  388. // store the layout constraints against the layout from view
  389. layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
  390. // return the new constraints
  391. return newLayoutConstraints
  392. }
  393. internal func uninstallFromView() {
  394. if let installInfo = self.installInfo,
  395. let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
  396. if installedLayoutConstraints.count > 0 {
  397. if let installedOnView = installInfo.view {
  398. // remove the constraints from the UIView's storage
  399. installedOnView.removeConstraints(installedLayoutConstraints)
  400. }
  401. // remove the constraints from the from item view
  402. if let fromView = self.fromItem.view {
  403. fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter {
  404. return !contains(installedLayoutConstraints, $0)
  405. }
  406. }
  407. }
  408. }
  409. self.installInfo = nil
  410. }
  411. // MARK: private
  412. private let fromItem: ConstraintItem
  413. private var toItem: ConstraintItem
  414. private var relation: ConstraintRelation?
  415. private var constant: Any = Float(0.0)
  416. private var multiplier: Float = 1.0
  417. private var priority: Float = 1000.0
  418. private var offset: Any = Float(0.0)
  419. private var installInfo: ConstraintInstallInfo?
  420. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  421. if self.relation == nil {
  422. self.fromItem.attributes += attributes
  423. }
  424. return self
  425. }
  426. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  427. if other.attributes != ConstraintAttributes.None {
  428. let toLayoutAttributes = other.attributes.layoutAttributes
  429. if toLayoutAttributes.count > 1 {
  430. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  431. if toLayoutAttributes != fromLayoutAttributes {
  432. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  433. return self
  434. }
  435. other.attributes = ConstraintAttributes.None
  436. }
  437. }
  438. self.toItem = other
  439. self.relation = relation
  440. return self
  441. }
  442. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  443. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  444. }
  445. #if os(iOS)
  446. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
  447. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  448. }
  449. #endif
  450. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  451. self.constant = other
  452. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  453. }
  454. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  455. self.constant = other
  456. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  457. }
  458. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  459. self.constant = other
  460. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  461. }
  462. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  463. self.constant = other
  464. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  465. }
  466. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  467. self.constant = other
  468. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  469. }
  470. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  471. var views = Set<View>()
  472. var fromView = fromView
  473. var toView = toView
  474. do {
  475. if let view = toView {
  476. if views.contains(view) {
  477. return view
  478. }
  479. views.insert(view)
  480. toView = view.superview
  481. }
  482. if let view = fromView {
  483. if views.contains(view) {
  484. return view
  485. }
  486. views.insert(view)
  487. fromView = view.superview
  488. }
  489. } while (fromView != nil || toView != nil)
  490. return nil
  491. }
  492. }
  493. private extension NSLayoutAttribute {
  494. private func snp_offsetForValue(value: Any?) -> CGFloat {
  495. // Float
  496. if let float = value as? Float {
  497. return CGFloat(float)
  498. }
  499. // Double
  500. else if let double = value as? Double {
  501. return CGFloat(double)
  502. }
  503. // UInt
  504. else if let int = value as? Int {
  505. return CGFloat(int)
  506. }
  507. // Int
  508. else if let uint = value as? UInt {
  509. return CGFloat(uint)
  510. }
  511. // CGFloat
  512. else if let float = value as? CGFloat {
  513. return float
  514. }
  515. // CGSize
  516. else if let size = value as? CGSize {
  517. if self == .Width {
  518. return size.width
  519. } else if self == .Height {
  520. return size.height
  521. }
  522. }
  523. // CGPoint
  524. else if let point = value as? CGPoint {
  525. if self == .Left || self == .CenterX {
  526. return point.x
  527. } else if self == .Top || self == .CenterY {
  528. return point.y
  529. } else if self == .Right {
  530. return -point.x
  531. } else if self == .Bottom {
  532. return -point.y
  533. }
  534. }
  535. // EdgeInsets
  536. else if let insets = value as? EdgeInsets {
  537. if self == .Left {
  538. return insets.left
  539. } else if self == .Top {
  540. return insets.top
  541. } else if self == .Right {
  542. return -insets.right
  543. } else if self == .Bottom {
  544. return -insets.bottom
  545. }
  546. }
  547. return CGFloat(0)
  548. }
  549. private func snp_constantForValue(value: Any?) -> CGFloat {
  550. // Float
  551. if let float = value as? Float {
  552. return CGFloat(float)
  553. }
  554. // Double
  555. else if let double = value as? Double {
  556. return CGFloat(double)
  557. }
  558. // UInt
  559. else if let int = value as? Int {
  560. return CGFloat(int)
  561. }
  562. // Int
  563. else if let uint = value as? UInt {
  564. return CGFloat(uint)
  565. }
  566. // CGFloat
  567. else if let float = value as? CGFloat {
  568. return float
  569. }
  570. // CGSize
  571. else if let size = value as? CGSize {
  572. if self == .Width {
  573. return size.width
  574. } else if self == .Height {
  575. return size.height
  576. }
  577. }
  578. // CGPoint
  579. else if let point = value as? CGPoint {
  580. if self == .Left || self == .CenterX {
  581. return point.x
  582. } else if self == .Top || self == .CenterY {
  583. return point.y
  584. } else if self == .Right {
  585. return point.x
  586. } else if self == .Bottom {
  587. return point.y
  588. }
  589. }
  590. // EdgeInsets
  591. else if let insets = value as? EdgeInsets {
  592. if self == .Left {
  593. return insets.left
  594. } else if self == .Top {
  595. return insets.top
  596. } else if self == .Right {
  597. return -insets.right
  598. } else if self == .Bottom {
  599. return -insets.bottom
  600. }
  601. }
  602. return CGFloat(0);
  603. }
  604. }
  605. private struct ConstraintInstallInfo {
  606. weak var view: UIView? = nil
  607. let layoutConstraints: NSHashTable
  608. }
  609. internal func ==(left: Constraint, right: Constraint) -> Bool {
  610. return (left.fromItem == right.fromItem &&
  611. left.toItem == right.toItem &&
  612. left.relation == right.relation &&
  613. left.multiplier == right.multiplier &&
  614. left.priority == right.priority)
  615. }