Constraint.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. 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 priorityLow() -> Constraint {
  220. return self.priority(250.0)
  221. }
  222. // MARK: offset
  223. public func offset(amount: Float) -> Constraint {
  224. self.offset = amount
  225. return self
  226. }
  227. public func offset(amount: Double) -> Constraint {
  228. return self.offset(Float(amount))
  229. }
  230. public func offset(amount: CGFloat) -> Constraint {
  231. return self.offset(Float(amount))
  232. }
  233. public func offset(amount: Int) -> Constraint {
  234. return self.offset(Float(amount))
  235. }
  236. public func offset(amount: UInt) -> Constraint {
  237. return self.offset(Float(amount))
  238. }
  239. public func offset(amount: CGPoint) -> Constraint {
  240. self.offset = amount
  241. return self
  242. }
  243. public func offset(amount: CGSize) -> Constraint {
  244. self.offset = amount
  245. return self
  246. }
  247. public func offset(amount: EdgeInsets) -> Constraint {
  248. self.offset = amount
  249. return self
  250. }
  251. // MARK: insets
  252. public func insets(amount: EdgeInsets) -> Constraint {
  253. self.offset = amount
  254. return self
  255. }
  256. // MARK: install / uninstall
  257. public func install() -> Array<LayoutConstraint> {
  258. return self.installOnView(updateExisting: false)
  259. }
  260. public func uninstall() {
  261. self.uninstallFromView()
  262. }
  263. // MARK: internal
  264. internal func installOnView(updateExisting: Bool = false) -> Array<LayoutConstraint> {
  265. var installOnView: View? = nil
  266. if self.toItem.view != nil {
  267. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  268. if installOnView == nil {
  269. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  270. return []
  271. }
  272. } else {
  273. installOnView = self.fromItem.view?.superview
  274. if installOnView == nil {
  275. if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
  276. installOnView = self.fromItem.view
  277. }
  278. if installOnView == nil {
  279. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  280. return []
  281. }
  282. }
  283. }
  284. if self.installedOnView != nil {
  285. if self.installedOnView != installOnView {
  286. NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
  287. return []
  288. }
  289. return (self.installedLayoutConstraints?.allObjects as? Array<LayoutConstraint>)!
  290. }
  291. var newLayoutConstraints = Array<LayoutConstraint>()
  292. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  293. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  294. // get layout from
  295. let layoutFrom: View? = self.fromItem.view
  296. // get layout relation
  297. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  298. for layoutFromAttribute in layoutFromAttributes {
  299. // get layout to attribute
  300. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  301. // get layout constant
  302. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  303. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  304. // get layout to
  305. var layoutTo: View? = self.toItem.view
  306. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  307. layoutTo = installOnView
  308. }
  309. // create layout constraint
  310. let layoutConstraint = LayoutConstraint(
  311. item: layoutFrom!,
  312. attribute: layoutFromAttribute,
  313. relatedBy: layoutRelation,
  314. toItem: layoutTo,
  315. attribute: layoutToAttribute,
  316. multiplier: CGFloat(self.multiplier),
  317. constant: layoutConstant)
  318. // set priority
  319. layoutConstraint.priority = self.priority
  320. // set constraint
  321. layoutConstraint.constraint = self
  322. newLayoutConstraints.append(layoutConstraint)
  323. }
  324. // special logic for updating
  325. if updateExisting {
  326. // get existing constraints for this view
  327. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  328. // array that will contain only new layout constraints to keep
  329. var newLayoutConstraintsToKeep = Array<LayoutConstraint>()
  330. // begin looping
  331. for layoutConstraint in newLayoutConstraints {
  332. // layout constraint that should be updated
  333. var updateLayoutConstraint: LayoutConstraint? = nil
  334. // loop through existing and check for match
  335. for existingLayoutConstraint in existingLayoutConstraints {
  336. if existingLayoutConstraint == layoutConstraint {
  337. updateLayoutConstraint = existingLayoutConstraint
  338. break
  339. }
  340. }
  341. // if we have existing one lets just update the constant
  342. if updateLayoutConstraint != nil {
  343. updateLayoutConstraint!.constant = layoutConstraint.constant
  344. }
  345. // otherwise add this layout constraint to new keep list
  346. else {
  347. newLayoutConstraintsToKeep.append(layoutConstraint)
  348. }
  349. }
  350. // set constraints to only new ones
  351. newLayoutConstraints = newLayoutConstraintsToKeep
  352. }
  353. // add constraints
  354. installOnView!.addConstraints(newLayoutConstraints)
  355. // store which view this constraint was installed on
  356. self.installedOnView = installOnView
  357. // store which layout constraints are installed for this constraint
  358. self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable()
  359. for layoutConstraint in newLayoutConstraints {
  360. self.installedLayoutConstraints!.addObject(layoutConstraint)
  361. }
  362. // store the layout constraints against the installed on view
  363. var layoutConstraints = Array<LayoutConstraint>(layoutFrom!.snp_installedLayoutConstraints)
  364. layoutConstraints += newLayoutConstraints
  365. layoutFrom!.snp_installedLayoutConstraints = layoutConstraints
  366. // return the new constraints
  367. return newLayoutConstraints
  368. }
  369. internal func uninstallFromView() {
  370. if let view = self.installedOnView {
  371. // remove all installed layout constraints
  372. var layoutConstraintsToRemove = Array<LayoutConstraint>()
  373. if let allObjects = self.installedLayoutConstraints?.allObjects {
  374. if let installedLayoutConstraints = allObjects as? Array<LayoutConstraint> {
  375. layoutConstraintsToRemove += installedLayoutConstraints
  376. }
  377. }
  378. if layoutConstraintsToRemove.count > 0 {
  379. view.removeConstraints(layoutConstraintsToRemove)
  380. }
  381. // clean up the snp_installedLayoutConstraints
  382. var layoutConstraints = view.snp_installedLayoutConstraints
  383. var layoutConstraintsToKeep = Array<LayoutConstraint>()
  384. for layoutConstraint in layoutConstraints {
  385. if !contains(layoutConstraintsToRemove, layoutConstraint) {
  386. layoutConstraintsToKeep.append(layoutConstraint)
  387. }
  388. }
  389. view.snp_installedLayoutConstraints = layoutConstraintsToKeep
  390. }
  391. self.installedOnView = nil
  392. self.installedLayoutConstraints = nil
  393. }
  394. // MARK: private
  395. private let fromItem: ConstraintItem
  396. private var toItem: ConstraintItem
  397. private var relation: ConstraintRelation?
  398. private var constant: Any?
  399. private var multiplier: Float = 1.0
  400. private var priority: Float = 1000.0
  401. private var offset: Any?
  402. private var installedLayoutConstraints: NSHashTable?
  403. private weak var installedOnView: View?
  404. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  405. if self.relation == nil {
  406. self.fromItem.attributes += attributes
  407. }
  408. return self
  409. }
  410. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  411. if other.attributes != ConstraintAttributes.None {
  412. var toLayoutAttributes = other.attributes.layoutAttributes
  413. if toLayoutAttributes.count > 1 {
  414. var fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  415. if toLayoutAttributes != fromLayoutAttributes {
  416. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  417. return self
  418. }
  419. other.attributes = ConstraintAttributes.None
  420. }
  421. }
  422. self.toItem = other
  423. self.relation = relation
  424. return self
  425. }
  426. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  427. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  428. }
  429. #if os(iOS)
  430. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
  431. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  432. }
  433. #endif
  434. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  435. self.constant = other
  436. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  437. }
  438. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  439. self.constant = other
  440. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  441. }
  442. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  443. self.constant = other
  444. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  445. }
  446. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  447. self.constant = other
  448. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  449. }
  450. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  451. self.constant = other
  452. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  453. }
  454. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  455. var views = NSMutableSet()
  456. var fromView = fromView
  457. var toView = toView
  458. do {
  459. if let view = toView {
  460. if views.containsObject(view) {
  461. return view
  462. }
  463. views.addObject(view)
  464. toView = view.superview
  465. }
  466. if let view = fromView {
  467. if views.containsObject(view) {
  468. return view
  469. }
  470. views.addObject(view)
  471. fromView = view.superview
  472. }
  473. } while (fromView != nil || toView != nil)
  474. return nil
  475. }
  476. }
  477. private extension NSLayoutAttribute {
  478. private func snp_offsetForValue(value: Any?) -> CGFloat {
  479. // Float
  480. if let float = value as? Float {
  481. return CGFloat(float)
  482. }
  483. // Double
  484. else if let double = value as? Double {
  485. return CGFloat(double)
  486. }
  487. // UInt
  488. else if let int = value as? Int {
  489. return CGFloat(int)
  490. }
  491. // Int
  492. else if let uint = value as? UInt {
  493. return CGFloat(uint)
  494. }
  495. // CGFloat
  496. else if let float = value as? CGFloat {
  497. return float
  498. }
  499. // CGSize
  500. else if let size = value as? CGSize {
  501. if self == .Width {
  502. return size.width
  503. } else if self == .Height {
  504. return size.height
  505. }
  506. }
  507. // CGPoint
  508. else if let point = value as? CGPoint {
  509. if self == .Left || self == .CenterX {
  510. return point.x
  511. } else if self == .Top || self == .CenterY {
  512. return point.y
  513. } else if self == .Right {
  514. return -point.x
  515. } else if self == .Bottom {
  516. return -point.y
  517. }
  518. }
  519. // EdgeInsets
  520. else if let insets = value as? EdgeInsets {
  521. if self == .Left {
  522. return insets.left
  523. } else if self == .Top {
  524. return insets.top
  525. } else if self == .Right {
  526. return -insets.right
  527. } else if self == .Bottom {
  528. return -insets.bottom
  529. }
  530. }
  531. return CGFloat(0)
  532. }
  533. private func snp_constantForValue(value: Any?) -> CGFloat {
  534. // Float
  535. if let float = value as? Float {
  536. return CGFloat(float)
  537. }
  538. // Double
  539. else if let double = value as? Double {
  540. return CGFloat(double)
  541. }
  542. // UInt
  543. else if let int = value as? Int {
  544. return CGFloat(int)
  545. }
  546. // Int
  547. else if let uint = value as? UInt {
  548. return CGFloat(uint)
  549. }
  550. // CGFloat
  551. else if let float = value as? CGFloat {
  552. return float
  553. }
  554. // CGSize
  555. else if let size = value as? CGSize {
  556. if self == .Width {
  557. return size.width
  558. } else if self == .Height {
  559. return size.height
  560. }
  561. }
  562. // CGPoint
  563. else if let point = value as? CGPoint {
  564. if self == .Left || self == .CenterX {
  565. return point.x
  566. } else if self == .Top || self == .CenterY {
  567. return point.y
  568. } else if self == .Right {
  569. return point.x
  570. } else if self == .Bottom {
  571. return point.y
  572. }
  573. }
  574. // EdgeInsets
  575. else if let insets = value as? EdgeInsets {
  576. if self == .Left {
  577. return insets.left
  578. } else if self == .Top {
  579. return insets.top
  580. } else if self == .Right {
  581. return -insets.right
  582. } else if self == .Bottom {
  583. return -insets.bottom
  584. }
  585. }
  586. return CGFloat(0);
  587. }
  588. }
  589. internal func ==(left: Constraint, right: Constraint) -> Bool {
  590. return (left.fromItem == right.fromItem &&
  591. left.toItem == right.toItem &&
  592. left.relation == right.relation &&
  593. left.multiplier == right.multiplier &&
  594. left.priority == right.priority)
  595. }