Constraint.swift 25 KB

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