Netease-Music-Demo by sss
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConstraintMakerRelatable.swift 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
  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) || os(tvOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. public class ConstraintMakerRelatable {
  29. internal let description: ConstraintDescription
  30. internal init(_ description: ConstraintDescription) {
  31. self.description = description
  32. }
  33. internal func relatedTo(_ other: ConstraintRelatableTarget, relation: ConstraintRelation, file: String, line: UInt) -> ConstraintMakerEditable {
  34. let related: ConstraintItem
  35. let constant: ConstraintConstantTarget
  36. if let other = other as? ConstraintItem {
  37. guard other.attributes == ConstraintAttributes.none ||
  38. other.attributes.layoutAttributes.count <= 1 ||
  39. other.attributes.layoutAttributes == self.description.attributes.layoutAttributes ||
  40. other.attributes == .edges && self.description.attributes == .margins ||
  41. other.attributes == .margins && self.description.attributes == .edges ||
  42. other.attributes == .directionalEdges && self.description.attributes == .directionalMargins ||
  43. other.attributes == .directionalMargins && self.description.attributes == .directionalEdges else {
  44. fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))");
  45. }
  46. related = other
  47. constant = 0.0
  48. } else if let other = other as? ConstraintView {
  49. related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
  50. constant = 0.0
  51. } else if let other = other as? ConstraintConstantTarget {
  52. related = ConstraintItem(target: nil, attributes: ConstraintAttributes.none)
  53. constant = other
  54. } else if #available(iOS 9.0, OSX 10.11, *), let other = other as? ConstraintLayoutGuide {
  55. related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
  56. constant = 0.0
  57. } else {
  58. fatalError("Invalid constraint. (\(file), \(line))")
  59. }
  60. let editable = ConstraintMakerEditable(self.description)
  61. editable.description.sourceLocation = (file, line)
  62. editable.description.relation = relation
  63. editable.description.related = related
  64. editable.description.constant = constant
  65. return editable
  66. }
  67. @discardableResult
  68. public func equalTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  69. return self.relatedTo(other, relation: .equal, file: file, line: line)
  70. }
  71. @discardableResult
  72. public func equalToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  73. guard let other = self.description.item.superview else {
  74. fatalError("Expected superview but found nil when attempting make constraint `equalToSuperview`.")
  75. }
  76. return self.relatedTo(other, relation: .equal, file: file, line: line)
  77. }
  78. @discardableResult
  79. public func lessThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  80. return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
  81. }
  82. @discardableResult
  83. public func lessThanOrEqualToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  84. guard let other = self.description.item.superview else {
  85. fatalError("Expected superview but found nil when attempting make constraint `lessThanOrEqualToSuperview`.")
  86. }
  87. return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
  88. }
  89. @discardableResult
  90. public func greaterThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
  91. return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
  92. }
  93. @discardableResult
  94. public func greaterThanOrEqualToSuperview(_ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
  95. guard let other = self.description.item.superview else {
  96. fatalError("Expected superview but found nil when attempting make constraint `greaterThanOrEqualToSuperview`.")
  97. }
  98. return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
  99. }
  100. }