AnimatableImageView.swift 5.9 KB

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