MGSwipeTableCell.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * MGSwipeTableCell is licensed under MIT license. See LICENSE.md file for more information.
  3. * Copyright (c) 2014 Imanol Fernandez @MortimerGoro
  4. */
  5. #import <UIKit/UIKit.h>
  6. /** Transition types */
  7. typedef NS_ENUM(NSInteger, MGSwipeTransition) {
  8. MGSwipeTransitionBorder = 0,
  9. MGSwipeTransitionStatic,
  10. MGSwipeTransitionDrag,
  11. MGSwipeTransitionClipCenter,
  12. MGSwipeTransitionRotate3D
  13. };
  14. #define MGSwipeTransition3D MGSwipeTransitionRotate3D
  15. /** Swipe directions */
  16. typedef NS_ENUM(NSInteger, MGSwipeDirection) {
  17. MGSwipeDirectionLeftToRight = 0,
  18. MGSwipeDirectionRightToLeft
  19. };
  20. /** Swipe state */
  21. typedef NS_ENUM(NSInteger, MGSwipeState) {
  22. MGSwipeStateNone = 0,
  23. MGSwipeStateSwippingLeftToRight,
  24. MGSwipeStateSwippingRightToLeft,
  25. MGSwipeStateExpandingLeftToRight,
  26. MGSwipeStateExpandingRightToLeft,
  27. };
  28. /** Swipe state */
  29. typedef NS_ENUM(NSInteger, MGSwipeExpansionLayout) {
  30. MGSwipeExpansionLayoutBorder = 0,
  31. MGSwipeExpansionLayoutCenter
  32. };
  33. /**
  34. * Swipe settings
  35. **/
  36. @interface MGSwipeSettings: NSObject
  37. /** Transition used while swipping buttons */
  38. @property (nonatomic, assign) MGSwipeTransition transition;
  39. /** Size proportional threshold to hide/keep the buttons when the user ends swipping. Default value 0.5 */
  40. @property (nonatomic, assign) CGFloat threshold;
  41. /** Optional offset to change the swipe buttons position. Relative to the cell border position. Default value: 0
  42. ** For example it can be used to avoid cropped buttons when sectionIndexTitlesForTableView is used in the UITableView
  43. **/
  44. @property (nonatomic, assign) CGFloat offset;
  45. /** Property to read or change swipe animation durations. Default value 0.3 */
  46. @property (nonatomic, assign) CGFloat animationDuration;
  47. @end
  48. /**
  49. * Expansion settings to make expandable buttons
  50. * Swipe button are not expandable by default
  51. **/
  52. @interface MGSwipeExpansionSettings: NSObject
  53. /** index of the expandable button (in the left or right buttons arrays) */
  54. @property (nonatomic, assign) NSInteger buttonIndex;
  55. /** if true the button fills the cell on trigger, else it bounces back to its initial position */
  56. @property (nonatomic, assign) BOOL fillOnTrigger;
  57. /** Size proportional threshold to trigger the expansion button. Default value 1.5 */
  58. @property (nonatomic, assign) CGFloat threshold;
  59. /** Optional expansion color. Expanded button's background color is used by default **/
  60. @property (nonatomic, strong) UIColor * expansionColor;
  61. /** Defines the layout of the expanded button **/
  62. @property (nonatomic, assign) MGSwipeExpansionLayout expansionLayout;
  63. /** Property to read or change expansion animation durations. Default value 0.2
  64. * The target animation is the change of a button from normal state to expanded state
  65. */
  66. @property (nonatomic, assign) CGFloat animationDuration;
  67. @end
  68. /** helper forward declaration */
  69. @class MGSwipeTableCell;
  70. /**
  71. * Optional delegate to configure swipe buttons or to receive triggered actions.
  72. * Buttons can be configured inline when the cell is created instead of using this delegate,
  73. * but using the delegate improves memory usage because buttons are only created in demand
  74. */
  75. @protocol MGSwipeTableCellDelegate <NSObject>
  76. @optional
  77. /**
  78. * Delegate method to enable/disable swipe gestures
  79. * @return YES if swipe is allowed
  80. **/
  81. -(BOOL) swipeTableCell:(MGSwipeTableCell*) cell canSwipe:(MGSwipeDirection) direction;
  82. /**
  83. * Delegate method invoked when the current swipe state changes
  84. @param state the current Swipe State
  85. @param gestureIsActive YES if the user swipe gesture is active. No if the uses has already ended the gesture
  86. **/
  87. -(void) swipeTableCell:(MGSwipeTableCell*) cell didChangeSwipeState:(MGSwipeState) state gestureIsActive:(BOOL) gestureIsActive;
  88. /**
  89. * Called when the user clicks a swipe button or when a expandable button is automatically triggered
  90. * @return YES to autohide the current swipe buttons
  91. **/
  92. -(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion;
  93. /**
  94. * Delegate method to setup the swipe buttons and swipe/expansion settings
  95. * Buttons can be any kind of UIView but it's recommended to use the convenience MGSwipeButton class
  96. * Setting up buttons with this delegate instead of using cell properties improves memory usage because buttons are only created in demand
  97. * @param swipeTableCell the UITableVieCel to configure. You can get the indexPath using [tableView indexPathForCell:cell]
  98. * @param direction The swipe direction (left to right or right to left)
  99. * @param swipeSettings instance to configure the swipe transition and setting (optional)
  100. * @param expansionSettings instance to configure button expansions (optional)
  101. * @return Buttons array
  102. **/
  103. -(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
  104. swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;
  105. @end
  106. /**
  107. * Swipe Cell class
  108. * To implement swipe cells you have to override from this class
  109. * You can create the cells programmatically, using xibs or storyboards
  110. */
  111. @interface MGSwipeTableCell : UITableViewCell
  112. /** optional delegate (not retained) */
  113. @property (nonatomic, weak) id<MGSwipeTableCellDelegate> delegate;
  114. /** optional to use contentView alternative. Use this property instead of contentView to support animated views while swipping */
  115. @property (nonatomic, strong, readonly) UIView * swipeContentView;
  116. /**
  117. * Left and right swipe buttons and its settings.
  118. * Buttons can be any kind of UIView but it's recommended to use the convenience MGSwipeButton class
  119. */
  120. @property (nonatomic, copy) NSArray * leftButtons;
  121. @property (nonatomic, copy) NSArray * rightButtons;
  122. @property (nonatomic, strong) MGSwipeSettings * leftSwipeSettings;
  123. @property (nonatomic, strong) MGSwipeSettings * rightSwipeSettings;
  124. /** Optional settings to allow expandable buttons */
  125. @property (nonatomic, strong) MGSwipeExpansionSettings * leftExpansion;
  126. @property (nonatomic, strong) MGSwipeExpansionSettings * rightExpansion;
  127. /** Readonly property to fetch the current swipe state */
  128. @property (nonatomic, readonly) MGSwipeState swipeState;
  129. /** Readonly property to check if the user swipe gesture is currently active */
  130. @property (nonatomic, readonly) BOOL isSwipeGestureActive;
  131. // default is NO. Controls whether multiple cells can be swipped simultaneously
  132. @property (nonatomic) BOOL allowsMultipleSwipe;
  133. // default is NO. Controls whether buttons with different width are allowed. Buttons are resized to have the same size by default.
  134. @property (nonatomic) BOOL allowsButtonsWithDifferentWidth;
  135. //default is YES. Controls wheter swipe gesture is allowed when the touch starts into the swiped buttons
  136. @property (nonatomic) BOOL allowsSwipeWhenTappingButtons;
  137. /** Optional background color for swipe overlay. If not set, its inferred automatically from the cell contentView */
  138. @property (nonatomic, strong) UIColor * swipeBackgroundColor;
  139. /** Property to read or change the current swipe offset programmatically */
  140. @property (nonatomic, assign) CGFloat swipeOffset;
  141. /** Utility methods to show or hide swipe buttons programmatically */
  142. -(void) hideSwipeAnimated: (BOOL) animated;
  143. -(void) hideSwipeAnimated: (BOOL) animated completion:(void(^)()) completion;
  144. -(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated;
  145. -(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated completion:(void(^)()) completion;
  146. -(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(void(^)()) completion;
  147. -(void) expandSwipe: (MGSwipeDirection) direction animated: (BOOL) animated;
  148. /** Refresh method to be used when you want to update the cell contents while the user is swipping */
  149. -(void) refreshContentView;
  150. /** Refresh method to be used when you want to dinamically change the left or right buttons (add or remove)
  151. * If you only want to change the title or the backgroundColor of a button you can change it's properties (get the button instance from leftButtons or rightButtons arrays)
  152. * @param usingDelegate if YES new buttons will be fetched using the MGSwipeTableCellDelegate. Otherwise new buttons will be fetched from leftButtons/rightButtons properties.
  153. */
  154. -(void) refreshButtons: (BOOL) usingDelegate;
  155. @end