SwipeAction.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // SwipeAction.swift
  3. //
  4. // Created by Jeremy Koch
  5. // Copyright © 2017 Jeremy Koch. All rights reserved.
  6. //
  7. import UIKit
  8. /// Constants that help define the appearance of action buttons.
  9. public enum SwipeActionStyle: Int {
  10. /// Apply a style that reflects standard non-destructive actions.
  11. case `default`
  12. /// Apply a style that reflects destructive actions.
  13. case destructive
  14. }
  15. /**
  16. The `SwipeAction` object defines a single action to present when the user swipes horizontally in a table/collection item.
  17. This class lets you define one or more custom actions to display for a given item in your table/collection. Each instance of this class represents a single action to perform and includes the text, formatting information, and behavior for the corresponding button.
  18. */
  19. public class SwipeAction: NSObject {
  20. /// An optional unique action identifier.
  21. public var identifier: String?
  22. /// The title of the action button.
  23. ///
  24. /// - note: You must specify a title or an image.
  25. public var title: String?
  26. /// The style applied to the action button.
  27. public var style: SwipeActionStyle
  28. /// The object that is notified as transitioning occurs.
  29. public var transitionDelegate: SwipeActionTransitioning?
  30. /// The font to use for the title of the action button.
  31. ///
  32. /// - note: If you do not specify a font, a 15pt system font is used.
  33. public var font: UIFont?
  34. /// The text color of the action button.
  35. ///
  36. /// - note: If you do not specify a color, white is used.
  37. public var textColor: UIColor?
  38. /// The highlighted text color of the action button.
  39. ///
  40. /// - note: If you do not specify a color, `textColor` is used.
  41. public var highlightedTextColor: UIColor?
  42. /// The image used for the action button.
  43. ///
  44. /// - note: You must specify a title or an image.
  45. public var image: UIImage?
  46. /// The highlighted image used for the action button.
  47. ///
  48. /// - note: If you do not specify a highlight image, the default `image` is used for the highlighted state.
  49. public var highlightedImage: UIImage?
  50. /// The closure to execute when the user taps the button associated with this action.
  51. public var handler: ((SwipeAction, IndexPath) -> Void)?
  52. /// The background color of the action button.
  53. ///
  54. /// - note: Use this property to specify the background color for your button. If you do not specify a value for this property, the framework assigns a default color based on the value in the style property.
  55. public var backgroundColor: UIColor?
  56. /// The highlighted background color of the action button.
  57. ///
  58. /// - note: Use this property to specify the highlighted background color for your button.
  59. public var highlightedBackgroundColor: UIColor?
  60. /// The visual effect to apply to the action button.
  61. ///
  62. /// - note: Assigning a visual effect object to this property adds that effect to the background of the action button.
  63. public var backgroundEffect: UIVisualEffect?
  64. /// A Boolean value that determines whether the actions menu is automatically hidden upon selection.
  65. ///
  66. /// - note: When set to `true`, the actions menu is automatically hidden when the action is selected. The default value is `false`.
  67. public var hidesWhenSelected = false
  68. /**
  69. Constructs a new `SwipeAction` instance.
  70. - parameter style: The style of the action button.
  71. - parameter title: The title of the action button.
  72. - parameter handler: The closure to execute when the user taps the button associated with this action.
  73. */
  74. public init(style: SwipeActionStyle, title: String?, handler: ((SwipeAction, IndexPath) -> Void)?) {
  75. self.title = title
  76. self.style = style
  77. self.handler = handler
  78. }
  79. /**
  80. Calling this method performs the configured expansion completion animation including deletion, if necessary. Calling this method more than once has no effect.
  81. You should only call this method from the implementation of your action `handler` method.
  82. - parameter style: The desired style for completing the expansion action.
  83. */
  84. public func fulfill(with style: ExpansionFulfillmentStyle) {
  85. completionHandler?(style)
  86. }
  87. // MARK: - Internal
  88. internal var completionHandler: ((ExpansionFulfillmentStyle) -> Void)?
  89. }
  90. /// Describes how expansion should be resolved once the action has been fulfilled.
  91. public enum ExpansionFulfillmentStyle {
  92. /// Implies the item will be deleted upon action fulfillment.
  93. case delete
  94. /// Implies the item will be reset and the actions view hidden upon action fulfillment.
  95. case reset
  96. }
  97. // MARK: - Internal
  98. internal extension SwipeAction {
  99. var hasBackgroundColor: Bool {
  100. return backgroundColor != .clear && backgroundEffect == nil
  101. }
  102. }