AnimatableButton.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 AnimatableButton: UIButton, CornerDesignable, FillDesignable, BorderDesignable, ShadowDesignable, MaskDesignable, Animatable {
  8. // MARK: - CornerDesignable
  9. @IBInspectable open var cornerRadius: CGFloat = CGFloat.nan {
  10. didSet {
  11. configureCornerRadius()
  12. }
  13. }
  14. open var cornerSides: CornerSides = .allSides {
  15. didSet {
  16. configureCornerRadius()
  17. }
  18. }
  19. @IBInspectable var _cornerSides: String? {
  20. didSet {
  21. cornerSides = CornerSides(rawValue: _cornerSides)
  22. }
  23. }
  24. // MARK: - FillDesignable
  25. @IBInspectable open var fillColor: UIColor? {
  26. didSet {
  27. configureFillColor()
  28. }
  29. }
  30. open var predefinedColor: ColorType? {
  31. didSet {
  32. configureFillColor()
  33. }
  34. }
  35. @IBInspectable var _predefinedColor: String? {
  36. didSet {
  37. predefinedColor = ColorType(string: _predefinedColor)
  38. }
  39. }
  40. @IBInspectable open var opacity: CGFloat = CGFloat.nan {
  41. didSet {
  42. configureOpacity()
  43. }
  44. }
  45. // MARK: - BorderDesignable
  46. open var borderType: BorderType = .solid {
  47. didSet {
  48. configureBorder()
  49. }
  50. }
  51. @IBInspectable var _borderType: String? {
  52. didSet {
  53. borderType = BorderType(string: _borderType)
  54. }
  55. }
  56. @IBInspectable open var borderColor: UIColor? {
  57. didSet {
  58. configureBorder()
  59. }
  60. }
  61. @IBInspectable open var borderWidth: CGFloat = CGFloat.nan {
  62. didSet {
  63. configureBorder()
  64. }
  65. }
  66. open var borderSides: BorderSides = .AllSides {
  67. didSet {
  68. configureBorder()
  69. }
  70. }
  71. @IBInspectable var _borderSides: String? {
  72. didSet {
  73. borderSides = BorderSides(rawValue: _borderSides)
  74. }
  75. }
  76. // MARK: - ShadowDesignable
  77. @IBInspectable open var shadowColor: UIColor? {
  78. didSet {
  79. configureShadowColor()
  80. }
  81. }
  82. @IBInspectable open var shadowRadius: CGFloat = CGFloat.nan {
  83. didSet {
  84. configureShadowRadius()
  85. }
  86. }
  87. @IBInspectable open var shadowOpacity: CGFloat = CGFloat.nan {
  88. didSet {
  89. configureShadowOpacity()
  90. }
  91. }
  92. @IBInspectable open var shadowOffset: CGPoint = CGPoint(x: CGFloat.nan, y: CGFloat.nan) {
  93. didSet {
  94. configureShadowOffset()
  95. }
  96. }
  97. // MARK: - MaskDesignable
  98. open var maskType: MaskType = .none {
  99. didSet {
  100. configureMask(previousMaskType: oldValue)
  101. configureBorder()
  102. configureMaskShadow()
  103. }
  104. }
  105. /// The mask type used in Interface Builder. **Should not** use this property in code.
  106. @IBInspectable var _maskType: String? {
  107. didSet {
  108. maskType = MaskType(string: _maskType)
  109. }
  110. }
  111. // MARK: - Animatable
  112. open var animationType: AnimationType = .none
  113. @IBInspectable var _animationType: String? {
  114. didSet {
  115. animationType = AnimationType(string: _animationType)
  116. }
  117. }
  118. @IBInspectable open var autoRun: Bool = true
  119. @IBInspectable open var duration: Double = Double.nan
  120. @IBInspectable open var delay: Double = Double.nan
  121. @IBInspectable open var damping: CGFloat = CGFloat.nan
  122. @IBInspectable open var velocity: CGFloat = CGFloat.nan
  123. @IBInspectable open var force: CGFloat = CGFloat.nan
  124. @IBInspectable var _timingFunction: String = "" {
  125. didSet {
  126. timingFunction = TimingFunctionType(string: _timingFunction)
  127. }
  128. }
  129. open var timingFunction: TimingFunctionType = .none
  130. // MARK: - Lifecycle
  131. open override func prepareForInterfaceBuilder() {
  132. super.prepareForInterfaceBuilder()
  133. configureInspectableProperties()
  134. }
  135. open override func awakeFromNib() {
  136. super.awakeFromNib()
  137. configureInspectableProperties()
  138. }
  139. open override func layoutSubviews() {
  140. super.layoutSubviews()
  141. configureAfterLayoutSubviews()
  142. autoRunAnimation()
  143. }
  144. // MARK: - Private
  145. fileprivate func configureInspectableProperties() {
  146. configureAnimatableProperties()
  147. }
  148. fileprivate func configureAfterLayoutSubviews() {
  149. configureMask(previousMaskType: maskType)
  150. configureCornerRadius()
  151. configureBorder()
  152. configureMaskShadow()
  153. }
  154. }