SwipeExpanding.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // SwipeExpanding.swift
  3. //
  4. // Created by Jeremy Koch
  5. // Copyright © 2017 Jeremy Koch. All rights reserved.
  6. //
  7. import UIKit
  8. /**
  9. Adopt the `SwipeExpanding` protocol in objects that implement custom appearance of actions during expansion.
  10. */
  11. public protocol SwipeExpanding {
  12. /**
  13. Asks your object for the animation timing parameters.
  14. - parameter buttons: The expansion action button, which includes expanding action plus the remaining actions in the view.
  15. - parameter expanding: The new expansion state.
  16. - parameter otherActionButtons: The other action buttons in the view, not including the action button being expanded.
  17. */
  18. func animationTimingParameters(buttons: [UIButton], expanding: Bool) -> SwipeExpansionAnimationTimingParameters
  19. /**
  20. Tells your object when the expansion state is changing.
  21. - parameter button: The expansion action button.
  22. - parameter expanding: The new expansion state.
  23. - parameter otherActionButtons: The other action buttons in the view, not including the action button being expanded.
  24. */
  25. func actionButton(_ button: UIButton, didChange expanding: Bool, otherActionButtons: [UIButton])
  26. }
  27. /**
  28. Specifies timing information for the overall expansion animation.
  29. */
  30. public struct SwipeExpansionAnimationTimingParameters {
  31. /// Returns a `SwipeExpansionAnimationTimingParameters` instance with default animation parameters.
  32. public static var `default`: SwipeExpansionAnimationTimingParameters { return SwipeExpansionAnimationTimingParameters() }
  33. /// The duration of the expansion animation.
  34. public var duration: Double
  35. /// The delay before starting the expansion animation.
  36. public var delay: Double
  37. /**
  38. Contructs a new `SwipeExpansionAnimationTimingParameters` instance.
  39. - parameter duration: The duration of the animation.
  40. - parameter delay: The delay before starting the expansion animation.
  41. - returns: The new `SwipeExpansionAnimationTimingParameters` instance.
  42. */
  43. public init(duration: Double = 0.6, delay: Double = 0) {
  44. self.duration = duration
  45. self.delay = delay
  46. }
  47. }
  48. /**
  49. A scale and alpha expansion object drives the custom appearance of the effected actions during expansion.
  50. */
  51. public struct ScaleAndAlphaExpansion: SwipeExpanding {
  52. /// Returns a `ScaleAndAlphaExpansion` instance with default expansion options.
  53. public static var `default`: ScaleAndAlphaExpansion { return ScaleAndAlphaExpansion() }
  54. /// The duration of the animation.
  55. public let duration: Double
  56. /// The scale factor used during animation.
  57. public let scale: CGFloat
  58. /// The inter-button delay between animations.
  59. public let interButtonDelay: Double
  60. /**
  61. Contructs a new `ScaleAndAlphaExpansion` instance.
  62. - parameter duration: The duration of the animation.
  63. - parameter scale: The scale factor used during animation.
  64. - parameter interButtonDelay: The inter-button delay between animations.
  65. - returns: The new `ScaleAndAlphaExpansion` instance.
  66. */
  67. public init(duration: Double = 0.15, scale: CGFloat = 0.8, interButtonDelay: Double = 0.1) {
  68. self.duration = duration
  69. self.scale = scale
  70. self.interButtonDelay = interButtonDelay
  71. }
  72. /// :nodoc:
  73. public func animationTimingParameters(buttons: [UIButton], expanding: Bool) -> SwipeExpansionAnimationTimingParameters {
  74. var timingParameters = SwipeExpansionAnimationTimingParameters.default
  75. timingParameters.delay = expanding ? interButtonDelay : 0
  76. return timingParameters
  77. }
  78. /// :nodoc:
  79. public func actionButton(_ button: UIButton, didChange expanding: Bool, otherActionButtons: [UIButton]) {
  80. let buttons = expanding ? otherActionButtons : otherActionButtons.reversed()
  81. buttons.enumerated().forEach { index, button in
  82. UIView.animate(withDuration: duration, delay: interButtonDelay * Double(expanding ? index : index + 1), options: [], animations: {
  83. button.transform = expanding ? .init(scaleX: self.scale, y: self.scale) : .identity
  84. button.alpha = expanding ? 0.0 : 1.0
  85. }, completion: nil)
  86. }
  87. }
  88. }