IB.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // IB.swift
  3. // IBAnimatableApp
  4. //
  5. // Created by Tom Baranes on 17/01/2018.
  6. // Copyright © 2018 IBAnimatable. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. public final class IB: NSObject, CornerDesignable, FillDesignable, BorderDesignable,
  11. RotationDesignable, ShadowDesignable, BlurDesignable,
  12. GradientDesignable, MaskDesignable {
  13. private let view: UIView
  14. // MARK: Life cycle
  15. fileprivate init(view: UIView) {
  16. self.view = view
  17. super.init()
  18. }
  19. public func reload() {
  20. configureMask(in: view, previousMaskType: maskType)
  21. configureCornerRadius(in: view)
  22. configureBorder(in: view)
  23. configureMaskShadow(in: view)
  24. }
  25. // MARK: - CornerDesignable
  26. public var cornerRadius: CGFloat = CGFloat.nan {
  27. didSet {
  28. configureCornerRadius(in: view)
  29. }
  30. }
  31. public var cornerSides: CornerSides = .allSides {
  32. didSet {
  33. configureCornerRadius(in: view)
  34. }
  35. }
  36. // MARK: - FillDesignable
  37. public var fillColor: UIColor? {
  38. didSet {
  39. configureFillColor(in: view)
  40. }
  41. }
  42. public var predefinedColor: ColorType? {
  43. didSet {
  44. configureFillColor(in: view)
  45. }
  46. }
  47. public var opacity: CGFloat = CGFloat.nan {
  48. didSet {
  49. configureOpacity(in: view)
  50. }
  51. }
  52. // MARK: - BorderDesignable
  53. public var borderType: BorderType = .solid {
  54. didSet {
  55. configureBorder(in: view)
  56. }
  57. }
  58. public var borderColor: UIColor? {
  59. didSet {
  60. configureBorder(in: view)
  61. }
  62. }
  63. public var borderWidth: CGFloat = CGFloat.nan {
  64. didSet {
  65. configureBorder(in: view)
  66. }
  67. }
  68. public var borderSides: BorderSides = .AllSides {
  69. didSet {
  70. configureBorder(in: view)
  71. }
  72. }
  73. // MARK: - RotationDesignable
  74. public var rotate: CGFloat = CGFloat.nan {
  75. didSet {
  76. configureRotate(in: view)
  77. }
  78. }
  79. // MARK: - ShadowDesignable
  80. public var shadowColor: UIColor? {
  81. didSet {
  82. configureShadowColor(in: view)
  83. }
  84. }
  85. public var shadowRadius: CGFloat = CGFloat.nan {
  86. didSet {
  87. configureShadowRadius(in: view)
  88. }
  89. }
  90. public var shadowOpacity: CGFloat = CGFloat.nan {
  91. didSet {
  92. configureShadowOpacity(in: view)
  93. }
  94. }
  95. public var shadowOffset: CGPoint = CGPoint(x: CGFloat.nan, y: CGFloat.nan) {
  96. didSet {
  97. configureShadowOffset(in: view)
  98. }
  99. }
  100. // MARK: - BlurDesignable
  101. public var blurEffectStyle: UIBlurEffect.Style? {
  102. didSet {
  103. configureBlurEffectStyle(in: view)
  104. }
  105. }
  106. public var vibrancyEffectStyle: UIBlurEffect.Style? {
  107. didSet {
  108. configureBlurEffectStyle(in: view)
  109. }
  110. }
  111. public var blurOpacity: CGFloat = CGFloat.nan {
  112. didSet {
  113. configureBlurEffectStyle(in: view)
  114. }
  115. }
  116. // MARK: - GradientDesignable
  117. public var gradientMode: GradientMode = .linear {
  118. didSet {
  119. configureGradient(in: view)
  120. }
  121. }
  122. public var startColor: UIColor? {
  123. didSet {
  124. configureGradient(in: view)
  125. }
  126. }
  127. public var endColor: UIColor? {
  128. didSet {
  129. configureGradient(in: view)
  130. }
  131. }
  132. public var predefinedGradient: GradientType? {
  133. didSet {
  134. configureGradient(in: view)
  135. }
  136. }
  137. public var startPoint: GradientStartPoint = .top {
  138. didSet {
  139. configureGradient(in: view)
  140. }
  141. }
  142. // MARK: - MaskDesignable
  143. public var maskType: MaskType = .none {
  144. didSet {
  145. configureMask(in: view, previousMaskType: oldValue)
  146. configureBorder(in: view)
  147. configureMaskShadow(in: view)
  148. }
  149. }
  150. }
  151. // MARK: -
  152. private struct AssociatedKeys {
  153. static var ib = "IBAnimatable"
  154. }
  155. extension UIView {
  156. public var ib: IB {
  157. get {
  158. var ibValue = objc_getAssociatedObject(self, &AssociatedKeys.ib) as? IB
  159. if ibValue == nil {
  160. ibValue = IB(view: self)
  161. self.ib = ibValue!
  162. }
  163. return ibValue!
  164. }
  165. set {
  166. objc_setAssociatedObject(self, &AssociatedKeys.ib, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  167. }
  168. }
  169. }