AnimatableView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // Created by Jake Lin on 11/18/15.
  3. // Copyright © 2015 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. @IBDesignable
  7. open class AnimatableView: UIView, CornerDesignable, FillDesignable, BorderDesignable,
  8. RotationDesignable, ShadowDesignable, BlurDesignable,
  9. TintDesignable, GradientDesignable, MaskDesignable,
  10. Animatable {
  11. // MARK: - CornerDesignable
  12. @IBInspectable open var cornerRadius: CGFloat = CGFloat.nan {
  13. didSet {
  14. configureCornerRadius()
  15. }
  16. }
  17. open var cornerSides: CornerSides = .allSides {
  18. didSet {
  19. configureCornerRadius()
  20. }
  21. }
  22. @IBInspectable var _cornerSides: String? {
  23. didSet {
  24. cornerSides = CornerSides(rawValue: _cornerSides)
  25. }
  26. }
  27. // MARK: - FillDesignable
  28. @IBInspectable open var fillColor: UIColor? {
  29. didSet {
  30. configureFillColor()
  31. }
  32. }
  33. open var predefinedColor: ColorType? {
  34. didSet {
  35. configureFillColor()
  36. }
  37. }
  38. @IBInspectable var _predefinedColor: String? {
  39. didSet {
  40. predefinedColor = ColorType(string: _predefinedColor)
  41. }
  42. }
  43. @IBInspectable open var opacity: CGFloat = CGFloat.nan {
  44. didSet {
  45. configureOpacity()
  46. }
  47. }
  48. // MARK: - BorderDesignable
  49. open var borderType: BorderType = .solid {
  50. didSet {
  51. configureBorder()
  52. }
  53. }
  54. @IBInspectable var _borderType: String? {
  55. didSet {
  56. borderType = BorderType(string: _borderType)
  57. }
  58. }
  59. @IBInspectable open var borderColor: UIColor? {
  60. didSet {
  61. configureBorder()
  62. }
  63. }
  64. @IBInspectable open var borderWidth: CGFloat = CGFloat.nan {
  65. didSet {
  66. configureBorder()
  67. }
  68. }
  69. open var borderSides: BorderSides = .AllSides {
  70. didSet {
  71. configureBorder()
  72. }
  73. }
  74. @IBInspectable var _borderSides: String? {
  75. didSet {
  76. borderSides = BorderSides(rawValue: _borderSides)
  77. }
  78. }
  79. // MARK: - RotationDesignable
  80. @IBInspectable open var rotate: CGFloat = CGFloat.nan {
  81. didSet {
  82. configureRotate()
  83. }
  84. }
  85. // MARK: - ShadowDesignable
  86. @IBInspectable open var shadowColor: UIColor? {
  87. didSet {
  88. configureShadowColor()
  89. }
  90. }
  91. @IBInspectable open var shadowRadius: CGFloat = CGFloat.nan {
  92. didSet {
  93. configureShadowRadius()
  94. }
  95. }
  96. @IBInspectable open var shadowOpacity: CGFloat = CGFloat.nan {
  97. didSet {
  98. configureShadowOpacity()
  99. }
  100. }
  101. @IBInspectable open var shadowOffset: CGPoint = CGPoint(x: CGFloat.nan, y: CGFloat.nan) {
  102. didSet {
  103. configureShadowOffset()
  104. }
  105. }
  106. // MARK: - BlurDesignable
  107. open var blurEffectStyle: UIBlurEffect.Style? {
  108. didSet {
  109. configureBlurEffectStyle()
  110. }
  111. }
  112. @IBInspectable var _blurEffectStyle: String? {
  113. didSet {
  114. blurEffectStyle = UIBlurEffect.Style(string: _blurEffectStyle)
  115. }
  116. }
  117. open var vibrancyEffectStyle: UIBlurEffect.Style? {
  118. didSet {
  119. configureBlurEffectStyle()
  120. }
  121. }
  122. @IBInspectable var _vibrancyEffectStyle: String? {
  123. didSet {
  124. vibrancyEffectStyle = UIBlurEffect.Style(string: _vibrancyEffectStyle)
  125. }
  126. }
  127. @IBInspectable open var blurOpacity: CGFloat = CGFloat.nan {
  128. didSet {
  129. configureBlurEffectStyle()
  130. }
  131. }
  132. // MARK: - TintDesignable
  133. @IBInspectable open var tintOpacity: CGFloat = CGFloat.nan
  134. @IBInspectable open var shadeOpacity: CGFloat = CGFloat.nan
  135. @IBInspectable open var toneColor: UIColor?
  136. @IBInspectable open var toneOpacity: CGFloat = CGFloat.nan
  137. // MARK: - GradientDesignable
  138. open var gradientMode: GradientMode = .linear
  139. @IBInspectable var _gradientMode: String? {
  140. didSet {
  141. gradientMode = GradientMode(string: _gradientMode) ?? .linear
  142. }
  143. }
  144. @IBInspectable open var startColor: UIColor?
  145. @IBInspectable open var endColor: UIColor?
  146. open var predefinedGradient: GradientType?
  147. @IBInspectable var _predefinedGradient: String? {
  148. didSet {
  149. predefinedGradient = GradientType(string: _predefinedGradient)
  150. }
  151. }
  152. open var startPoint: GradientStartPoint = .top
  153. @IBInspectable var _startPoint: String? {
  154. didSet {
  155. startPoint = GradientStartPoint(string: _startPoint, default: .top)
  156. }
  157. }
  158. // MARK: - MaskDesignable
  159. open var maskType: MaskType = .none {
  160. didSet {
  161. configureMask(previousMaskType: oldValue)
  162. configureBorder()
  163. configureMaskShadow()
  164. }
  165. }
  166. /// The mask type used in Interface Builder. **Should not** use this property in code.
  167. @IBInspectable var _maskType: String? {
  168. didSet {
  169. maskType = MaskType(string: _maskType)
  170. }
  171. }
  172. // MARK: - Animatable
  173. open var animationType: AnimationType = .none
  174. @IBInspectable var _animationType: String? {
  175. didSet {
  176. animationType = AnimationType(string: _animationType)
  177. }
  178. }
  179. @IBInspectable open var autoRun: Bool = true
  180. @IBInspectable open var duration: Double = Double.nan
  181. @IBInspectable open var delay: Double = Double.nan
  182. @IBInspectable open var damping: CGFloat = CGFloat.nan
  183. @IBInspectable open var velocity: CGFloat = CGFloat.nan
  184. @IBInspectable open var force: CGFloat = CGFloat.nan
  185. @IBInspectable var _timingFunction: String = "" {
  186. didSet {
  187. timingFunction = TimingFunctionType(string: _timingFunction)
  188. }
  189. }
  190. open var timingFunction: TimingFunctionType = .none
  191. // MARK: - Lifecycle
  192. open override func prepareForInterfaceBuilder() {
  193. super.prepareForInterfaceBuilder()
  194. configureInspectableProperties()
  195. }
  196. open override func awakeFromNib() {
  197. super.awakeFromNib()
  198. configureInspectableProperties()
  199. }
  200. open override func layoutSubviews() {
  201. super.layoutSubviews()
  202. configureAfterLayoutSubviews()
  203. autoRunAnimation()
  204. }
  205. // MARK: - Private
  206. fileprivate func configureInspectableProperties() {
  207. configureAnimatableProperties()
  208. configureTintedColor()
  209. }
  210. fileprivate func configureAfterLayoutSubviews() {
  211. configureMask(previousMaskType: maskType)
  212. configureCornerRadius()
  213. configureBorder()
  214. configureMaskShadow()
  215. configureGradient()
  216. }
  217. }