Constraint.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 addConstraint(ConstraintAttributes.Left) }
  33. public var top: Constraint { return addConstraint(ConstraintAttributes.Top) }
  34. public var right: Constraint { return addConstraint(ConstraintAttributes.Right) }
  35. public var bottom: Constraint { return addConstraint(ConstraintAttributes.Bottom) }
  36. public var leading: Constraint { return addConstraint(ConstraintAttributes.Leading) }
  37. public var trailing: Constraint { return addConstraint(ConstraintAttributes.Trailing) }
  38. public var width: Constraint { return addConstraint(ConstraintAttributes.Width) }
  39. public var height: Constraint { return addConstraint(ConstraintAttributes.Height) }
  40. public var centerX: Constraint { return addConstraint(ConstraintAttributes.CenterX) }
  41. public var centerY: Constraint { return addConstraint(ConstraintAttributes.CenterY) }
  42. public var baseline: Constraint { return addConstraint(ConstraintAttributes.Baseline) }
  43. public var and: Constraint { return self }
  44. public var with: Constraint { return self }
  45. // MARK: initializer
  46. internal init(fromItem: ConstraintItem) {
  47. self.fromItem = fromItem
  48. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  49. }
  50. // MARK: equalTo
  51. public func equalTo(other: ConstraintItem) -> Constraint {
  52. return constrainTo(other, relation: .Equal)
  53. }
  54. public func equalTo(other: View) -> Constraint {
  55. return constrainTo(other, relation: .Equal)
  56. }
  57. #if os(iOS)
  58. public func equalTo(other: UILayoutSupport) -> Constraint {
  59. return constrainTo(other, relation: .Equal)
  60. }
  61. #endif
  62. public func equalTo(other: Float) -> Constraint {
  63. return constrainTo(other, relation: .Equal)
  64. }
  65. public func equalTo(other: Double) -> Constraint {
  66. return constrainTo(Float(other), relation: .Equal)
  67. }
  68. public func equalTo(other: CGFloat) -> Constraint {
  69. return constrainTo(Float(other), relation: .Equal)
  70. }
  71. public func equalTo(other: Int) -> Constraint {
  72. return constrainTo(Float(other), relation: .Equal)
  73. }
  74. public func equalTo(other: UInt) -> Constraint {
  75. return constrainTo(Float(other), relation: .Equal)
  76. }
  77. public func equalTo(other: CGSize) -> Constraint {
  78. return constrainTo(other, relation: .Equal)
  79. }
  80. public func equalTo(other: CGPoint) -> Constraint {
  81. return constrainTo(other, relation: .Equal)
  82. }
  83. public func equalTo(other: EdgeInsets) -> Constraint {
  84. return constrainTo(other, relation: .Equal)
  85. }
  86. // MARK: lessThanOrEqualTo
  87. public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
  88. return constrainTo(other, relation: .LessThanOrEqualTo)
  89. }
  90. public func lessThanOrEqualTo(other: View) -> Constraint {
  91. return constrainTo(other, relation: .LessThanOrEqualTo)
  92. }
  93. #if os(iOS)
  94. public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  95. return constrainTo(other, relation: .LessThanOrEqualTo)
  96. }
  97. #endif
  98. public func lessThanOrEqualTo(other: Float) -> Constraint {
  99. return constrainTo(other, relation: .LessThanOrEqualTo)
  100. }
  101. public func lessThanOrEqualTo(other: Double) -> Constraint {
  102. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  103. }
  104. public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
  105. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  106. }
  107. public func lessThanOrEqualTo(other: Int) -> Constraint {
  108. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  109. }
  110. public func lessThanOrEqualTo(other: UInt) -> Constraint {
  111. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  112. }
  113. public func lessThanOrEqualTo(other: CGSize) -> Constraint {
  114. return constrainTo(other, relation: .LessThanOrEqualTo)
  115. }
  116. public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
  117. return constrainTo(other, relation: .LessThanOrEqualTo)
  118. }
  119. public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
  120. return constrainTo(other, relation: .LessThanOrEqualTo)
  121. }
  122. // MARK: greaterThanOrEqualTo
  123. public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
  124. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  125. }
  126. public func greaterThanOrEqualTo(other: View) -> Constraint {
  127. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  128. }
  129. #if os(iOS)
  130. public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  131. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  132. }
  133. #endif
  134. public func greaterThanOrEqualTo(other: Float) -> Constraint {
  135. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  136. }
  137. public func greaterThanOrEqualTo(other: Double) -> Constraint {
  138. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  139. }
  140. public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
  141. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  142. }
  143. public func greaterThanOrEqualTo(other: Int) -> Constraint {
  144. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  145. }
  146. public func greaterThanOrEqualTo(other: UInt) -> Constraint {
  147. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  148. }
  149. public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
  150. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  151. }
  152. public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
  153. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  154. }
  155. public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
  156. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  157. }
  158. // MARK: multiplier
  159. public func multipliedBy(amount: Float) -> Constraint {
  160. self.multiplier = amount
  161. return self
  162. }
  163. public func multipliedBy(amount: Double) -> Constraint {
  164. return self.multipliedBy(Float(amount))
  165. }
  166. public func multipliedBy(amount: CGFloat) -> Constraint {
  167. return self.multipliedBy(Float(amount))
  168. }
  169. public func multipliedBy(amount: Int) -> Constraint {
  170. return self.multipliedBy(Float(amount))
  171. }
  172. public func multipliedBy(amount: UInt) -> Constraint {
  173. return self.multipliedBy(Float(amount))
  174. }
  175. public func dividedBy(amount: Float) -> Constraint {
  176. self.multiplier = 1.0 / amount;
  177. return self
  178. }
  179. public func dividedBy(amount: Double) -> Constraint {
  180. return self.dividedBy(Float(amount))
  181. }
  182. public func dividedBy(amount: CGFloat) -> Constraint {
  183. return self.dividedBy(Float(amount))
  184. }
  185. public func dividedBy(amount: Int) -> Constraint {
  186. return self.dividedBy(Float(amount))
  187. }
  188. public func dividedBy(amount: UInt) -> Constraint {
  189. return self.dividedBy(Float(amount))
  190. }
  191. // MARK: priority
  192. public func priority(priority: Float) -> Constraint {
  193. self.priority = priority
  194. return self
  195. }
  196. public func priority(priority: Double) -> Constraint {
  197. return self.priority(Float(priority))
  198. }
  199. public func priority(priority: CGFloat) -> Constraint {
  200. return self.priority(Float(priority))
  201. }
  202. public func priority(priority: UInt) -> Constraint {
  203. return self.priority(Float(priority))
  204. }
  205. public func priority(priority: Int) -> Constraint {
  206. return self.priority(Float(priority))
  207. }
  208. public func priorityRequired() -> Constraint {
  209. return self.priority(1000.0)
  210. }
  211. public func priorityHigh() -> Constraint {
  212. return self.priority(750.0)
  213. }
  214. public func priorityLow() -> Constraint {
  215. return self.priority(250.0)
  216. }
  217. // MARK: offset
  218. public func offset(amount: Float) -> Constraint {
  219. self.offset = amount
  220. return self
  221. }
  222. public func offset(amount: Double) -> Constraint {
  223. return self.offset(Float(amount))
  224. }
  225. public func offset(amount: CGFloat) -> Constraint {
  226. return self.offset(Float(amount))
  227. }
  228. public func offset(amount: Int) -> Constraint {
  229. return self.offset(Float(amount))
  230. }
  231. public func offset(amount: UInt) -> Constraint {
  232. return self.offset(Float(amount))
  233. }
  234. public func offset(amount: CGPoint) -> Constraint {
  235. self.offset = amount
  236. return self
  237. }
  238. public func offset(amount: CGSize) -> Constraint {
  239. self.offset = amount
  240. return self
  241. }
  242. public func offset(amount: EdgeInsets) -> Constraint {
  243. self.offset = amount
  244. return self
  245. }
  246. // MARK: insets
  247. public func insets(amount: EdgeInsets) -> Constraint {
  248. self.offset = amount
  249. return self
  250. }
  251. // MARK: install / uninstall
  252. public func install() -> Array<LayoutConstraint> {
  253. return self.installOnView(updateExisting: false)
  254. }
  255. public func uninstall() {
  256. self.uninstallFromView()
  257. }
  258. // MARK: internal
  259. internal func installOnView(updateExisting: Bool = false) -> Array<LayoutConstraint> {
  260. if self.installedOnView != nil {
  261. NSException(name: "Cannot Install Constraint", reason: "Already installed", userInfo: nil).raise()
  262. return []
  263. }
  264. var installOnView: View? = nil
  265. if self.toItem.view != nil {
  266. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  267. if installOnView == nil {
  268. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  269. return []
  270. }
  271. } else {
  272. installOnView = self.fromItem.view?.superview
  273. if installOnView == nil {
  274. if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
  275. installOnView = self.fromItem.view
  276. }
  277. if installedOnView == nil {
  278. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  279. return []
  280. }
  281. }
  282. }
  283. var layoutConstraints: Array<LayoutConstraint> = []
  284. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  285. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  286. // get layout from
  287. let layoutFrom: View? = self.fromItem.view
  288. // get layout relation
  289. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  290. for layoutFromAttribute in layoutFromAttributes {
  291. // get layout to attribute
  292. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  293. // get layout constant
  294. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  295. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  296. // get layout to
  297. var layoutTo: View? = self.toItem.view
  298. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  299. layoutTo = installOnView
  300. }
  301. // create layout constraint
  302. let layoutConstraint = LayoutConstraint(
  303. item: layoutFrom!,
  304. attribute: layoutFromAttribute,
  305. relatedBy: layoutRelation,
  306. toItem: layoutTo,
  307. attribute: layoutToAttribute,
  308. multiplier: CGFloat(self.multiplier),
  309. constant: layoutConstant)
  310. // set priority
  311. layoutConstraint.priority = self.priority
  312. // set constraint
  313. layoutConstraint.constraint = self
  314. layoutConstraints.append(layoutConstraint)
  315. }
  316. // special logic for updating
  317. if updateExisting {
  318. // get existing constraints for this view
  319. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  320. // array that will contain only new layout constraints
  321. var newLayoutConstraints = Array<LayoutConstraint>()
  322. // begin looping
  323. for layoutConstraint in layoutConstraints {
  324. // layout constraint that should be updated
  325. var updateLayoutConstraint: LayoutConstraint? = nil
  326. // loop through existing and check for match
  327. for existingLayoutConstraint in existingLayoutConstraints {
  328. if existingLayoutConstraint == layoutConstraint {
  329. updateLayoutConstraint = existingLayoutConstraint
  330. break
  331. }
  332. }
  333. // if we have existing one lets just update the constant
  334. if updateLayoutConstraint != nil {
  335. updateLayoutConstraint!.constant = layoutConstraint.constant
  336. }
  337. // otherwise add this layout constraint to new list
  338. else {
  339. newLayoutConstraints.append(layoutConstraint)
  340. }
  341. }
  342. // set constraints to only new ones
  343. layoutConstraints = newLayoutConstraints
  344. }
  345. // add constraints
  346. installOnView!.addConstraints(layoutConstraints)
  347. self.installedOnView = installOnView
  348. self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable()
  349. for layoutConstraint in layoutConstraints {
  350. self.installedLayoutConstraints!.addObject(layoutConstraint)
  351. }
  352. return layoutConstraints
  353. }
  354. internal func uninstallFromView() {
  355. if let view = self.installedOnView {
  356. // remove all installed layout constraints
  357. var layoutConstraintsToRemove = Array<LayoutConstraint>()
  358. if let installedLayoutConstraints = self.installedLayoutConstraints?.allObjects as? Array<LayoutConstraint> {
  359. layoutConstraintsToRemove += installedLayoutConstraints
  360. }
  361. if layoutConstraintsToRemove.count > 0 {
  362. view.removeConstraints(layoutConstraintsToRemove)
  363. }
  364. // clean up the snp_installedLayoutConstraints
  365. var layoutConstraints = view.snp_installedLayoutConstraints
  366. var layoutConstraintsToKeep = Array<LayoutConstraint>()
  367. for layoutConstraint in layoutConstraints {
  368. if !contains(layoutConstraintsToRemove, layoutConstraint) {
  369. layoutConstraintsToKeep.append(layoutConstraint)
  370. }
  371. }
  372. view.snp_installedLayoutConstraints = layoutConstraintsToKeep
  373. }
  374. self.installedOnView = nil
  375. self.installedLayoutConstraints = nil
  376. }
  377. // MARK: private
  378. private let fromItem: ConstraintItem
  379. private var toItem: ConstraintItem
  380. private var relation: ConstraintRelation?
  381. private var constant: Any?
  382. private var multiplier: Float = 1.0
  383. private var priority: Float = 1000.0
  384. private var offset: Any?
  385. private var installedLayoutConstraints: NSHashTable?
  386. private weak var installedOnView: View?
  387. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  388. if self.relation == nil {
  389. self.fromItem.attributes += attributes
  390. }
  391. return self
  392. }
  393. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  394. if other.attributes != ConstraintAttributes.None {
  395. var toLayoutAttributes = other.attributes.layoutAttributes
  396. if toLayoutAttributes.count > 1 {
  397. var fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  398. if toLayoutAttributes != fromLayoutAttributes {
  399. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  400. return self
  401. }
  402. other.attributes = ConstraintAttributes.None
  403. }
  404. }
  405. self.toItem = other
  406. self.relation = relation
  407. return self
  408. }
  409. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  410. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  411. }
  412. #if os(iOS)
  413. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
  414. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  415. }
  416. #endif
  417. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  418. self.constant = other
  419. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  420. }
  421. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  422. self.constant = other
  423. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  424. }
  425. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  426. self.constant = other
  427. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  428. }
  429. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  430. self.constant = other
  431. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  432. }
  433. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  434. self.constant = other
  435. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  436. }
  437. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  438. var closestCommonSuperview: View?
  439. var secondViewSuperview: View? = toView
  440. while closestCommonSuperview == nil && secondViewSuperview != nil {
  441. var firstViewSuperview = fromView
  442. while closestCommonSuperview == nil && firstViewSuperview != nil {
  443. if secondViewSuperview == firstViewSuperview {
  444. closestCommonSuperview = secondViewSuperview
  445. }
  446. firstViewSuperview = firstViewSuperview?.superview
  447. }
  448. secondViewSuperview = secondViewSuperview?.superview
  449. }
  450. return closestCommonSuperview
  451. }
  452. }
  453. private extension NSLayoutAttribute {
  454. private func snp_offsetForValue(value: Any?) -> CGFloat {
  455. // Float
  456. if let float = value as? Float {
  457. return CGFloat(float)
  458. }
  459. // Double
  460. else if let double = value as? Double {
  461. return CGFloat(double)
  462. }
  463. // UInt
  464. else if let int = value as? Int {
  465. return CGFloat(int)
  466. }
  467. // Int
  468. else if let uint = value as? UInt {
  469. return CGFloat(uint)
  470. }
  471. // CGFloat
  472. else if let float = value as? CGFloat {
  473. return float
  474. }
  475. // CGSize
  476. else if let size = value as? CGSize {
  477. if self == .Width {
  478. return size.width
  479. } else if self == .Height {
  480. return size.height
  481. }
  482. }
  483. // CGPoint
  484. else if let point = value as? CGPoint {
  485. if self == .Left || self == .CenterX {
  486. return point.x
  487. } else if self == .Top || self == .CenterY {
  488. return point.y
  489. } else if self == .Right {
  490. return -point.x
  491. } else if self == .Bottom {
  492. return -point.y
  493. }
  494. }
  495. // EdgeInsets
  496. else if let insets = value as? EdgeInsets {
  497. if self == .Left {
  498. return insets.left
  499. } else if self == .Top {
  500. return insets.top
  501. } else if self == .Right {
  502. return -insets.right
  503. } else if self == .Bottom {
  504. return -insets.bottom
  505. }
  506. }
  507. return CGFloat(0)
  508. }
  509. private func snp_constantForValue(value: Any?) -> CGFloat {
  510. // Float
  511. if let float = value as? Float {
  512. return CGFloat(float)
  513. }
  514. // Double
  515. else if let double = value as? Double {
  516. return CGFloat(double)
  517. }
  518. // UInt
  519. else if let int = value as? Int {
  520. return CGFloat(int)
  521. }
  522. // Int
  523. else if let uint = value as? UInt {
  524. return CGFloat(uint)
  525. }
  526. // CGFloat
  527. else if let float = value as? CGFloat {
  528. return float
  529. }
  530. // CGSize
  531. else if let size = value as? CGSize {
  532. if self == .Width {
  533. return size.width
  534. } else if self == .Height {
  535. return size.height
  536. }
  537. }
  538. // CGPoint
  539. else if let point = value as? CGPoint {
  540. if self == .Left || self == .CenterX {
  541. return point.x
  542. } else if self == .Top || self == .CenterY {
  543. return point.y
  544. } else if self == .Right {
  545. return point.x
  546. } else if self == .Bottom {
  547. return point.y
  548. }
  549. }
  550. // EdgeInsets
  551. else if let insets = value as? EdgeInsets {
  552. if self == .Left {
  553. return insets.left
  554. } else if self == .Top {
  555. return insets.top
  556. } else if self == .Right {
  557. return -insets.right
  558. } else if self == .Bottom {
  559. return -insets.bottom
  560. }
  561. }
  562. return CGFloat(0);
  563. }
  564. }
  565. internal func ==(left: Constraint, right: Constraint) -> Bool {
  566. return (left.fromItem == right.fromItem &&
  567. left.toItem == right.toItem &&
  568. left.relation == right.relation &&
  569. left.multiplier == right.multiplier &&
  570. left.priority == right.priority)
  571. }