AnimatableCollectionViewCell.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Created by Tom Baranes on 11/04/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. @IBDesignable
  7. open class AnimatableCollectionViewCell: UICollectionViewCell, CornerDesignable, FillDesignable, BorderDesignable, RotationDesignable,
  8. ShadowDesignable, TableViewCellDesignable, GradientDesignable,
  9. BackgroundImageDesignable, Animatable {
  10. // MARK: - CornerDesignable
  11. @IBInspectable open var cornerRadius: CGFloat = CGFloat.nan {
  12. didSet {
  13. configureCornerRadius()
  14. }
  15. }
  16. open var cornerSides: CornerSides = .allSides {
  17. didSet {
  18. configureCornerRadius()
  19. }
  20. }
  21. @IBInspectable var _cornerSides: String? {
  22. didSet {
  23. cornerSides = CornerSides(rawValue: _cornerSides)
  24. }
  25. }
  26. // MARK: - ShadowDesignable
  27. @IBInspectable open var shadowColor: UIColor? {
  28. didSet {
  29. configureShadowColor()
  30. }
  31. }
  32. @IBInspectable open var shadowRadius: CGFloat = CGFloat.nan {
  33. didSet {
  34. configureShadowRadius()
  35. }
  36. }
  37. @IBInspectable open var shadowOpacity: CGFloat = CGFloat.nan {
  38. didSet {
  39. configureShadowOpacity()
  40. }
  41. }
  42. @IBInspectable open var shadowOffset: CGPoint = CGPoint(x: CGFloat.nan, y: CGFloat.nan) {
  43. didSet {
  44. configureShadowOffset()
  45. }
  46. }
  47. // MARK: - FillDesignable
  48. @IBInspectable open var fillColor: UIColor? {
  49. didSet {
  50. configureFillColor()
  51. }
  52. }
  53. open var predefinedColor: ColorType? {
  54. didSet {
  55. configureFillColor()
  56. }
  57. }
  58. @IBInspectable var _predefinedColor: String? {
  59. didSet {
  60. predefinedColor = ColorType(string: _predefinedColor)
  61. }
  62. }
  63. @IBInspectable open var opacity: CGFloat = CGFloat.nan {
  64. didSet {
  65. configureOpacity()
  66. }
  67. }
  68. // MARK: - BorderDesignable
  69. open var borderType: BorderType = .solid {
  70. didSet {
  71. configureBorder()
  72. }
  73. }
  74. @IBInspectable var _borderType: String? {
  75. didSet {
  76. borderType = BorderType(string: _borderType)
  77. }
  78. }
  79. @IBInspectable open var borderColor: UIColor? {
  80. didSet {
  81. configureBorder()
  82. }
  83. }
  84. @IBInspectable open var borderWidth: CGFloat = CGFloat.nan {
  85. didSet {
  86. configureBorder()
  87. }
  88. }
  89. open var borderSides: BorderSides = .AllSides {
  90. didSet {
  91. configureBorder()
  92. }
  93. }
  94. @IBInspectable var _borderSides: String? {
  95. didSet {
  96. borderSides = BorderSides(rawValue: _borderSides)
  97. }
  98. }
  99. // MARK: - RotationDesignable
  100. @IBInspectable open var rotate: CGFloat = CGFloat.nan {
  101. didSet {
  102. configureRotate()
  103. }
  104. }
  105. // MARK: - TableViewCellDesignable
  106. @IBInspectable open var removeSeparatorMargins: Bool = false
  107. // MARK: - GradientDesignable
  108. open var gradientMode: GradientMode = .linear
  109. @IBInspectable var _gradientMode: String? {
  110. didSet {
  111. gradientMode = GradientMode(string: _gradientMode) ?? .linear
  112. }
  113. }
  114. @IBInspectable open var startColor: UIColor?
  115. @IBInspectable open var endColor: UIColor?
  116. open var predefinedGradient: GradientType?
  117. @IBInspectable var _predefinedGradient: String? {
  118. didSet {
  119. predefinedGradient = GradientType(string: _predefinedGradient)
  120. }
  121. }
  122. open var startPoint: GradientStartPoint = .top
  123. @IBInspectable var _startPoint: String? {
  124. didSet {
  125. startPoint = GradientStartPoint(string: _startPoint, default: .top)
  126. }
  127. }
  128. // MARK: - BackgroundImageDesignable
  129. @IBInspectable open var backgroundImage: UIImage? {
  130. didSet {
  131. configureBackgroundImage()
  132. }
  133. }
  134. // MARK: - Animatable
  135. open var animationType: AnimationType = .none
  136. @IBInspectable var _animationType: String? {
  137. didSet {
  138. animationType = AnimationType(string: _animationType)
  139. }
  140. }
  141. @IBInspectable open var autoRun: Bool = true
  142. @IBInspectable open var duration: Double = Double.nan
  143. @IBInspectable open var delay: Double = Double.nan
  144. @IBInspectable open var damping: CGFloat = CGFloat.nan
  145. @IBInspectable open var velocity: CGFloat = CGFloat.nan
  146. @IBInspectable open var force: CGFloat = CGFloat.nan
  147. @IBInspectable var _timingFunction: String = "" {
  148. didSet {
  149. timingFunction = TimingFunctionType(string: _timingFunction)
  150. }
  151. }
  152. open var timingFunction: TimingFunctionType = .none
  153. // MARK: - Lifecycle
  154. open override func prepareForInterfaceBuilder() {
  155. super.prepareForInterfaceBuilder()
  156. configureInspectableProperties()
  157. }
  158. open override func awakeFromNib() {
  159. super.awakeFromNib()
  160. configureInspectableProperties()
  161. }
  162. open override func layoutSubviews() {
  163. super.layoutSubviews()
  164. configureAfterLayoutSubviews()
  165. autoRunAnimation()
  166. }
  167. // MARK: - Private
  168. fileprivate func configureInspectableProperties() {
  169. configureAnimatableProperties()
  170. configureOpacity()
  171. }
  172. fileprivate func configureAfterLayoutSubviews() {
  173. configureCornerRadius()
  174. configureBorder()
  175. configureGradient()
  176. }
  177. }