MGSwipeTableCell.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * MGSwipeTableCell is licensed under MIT license. See LICENSE.md file for more information.
  3. * Copyright (c) 2016 Imanol Fernandez @MortimerGoro
  4. */
  5. #import <UIKit/UIKit.h>
  6. #import "MGSwipeButton.h"
  7. /** Transition types */
  8. typedef NS_ENUM(NSInteger, MGSwipeTransition) {
  9. MGSwipeTransitionBorder = 0,
  10. MGSwipeTransitionStatic,
  11. MGSwipeTransitionDrag,
  12. MGSwipeTransitionClipCenter,
  13. MGSwipeTransitionRotate3D
  14. };
  15. /** Compatibility with older versions */
  16. #define MGSwipeTransition3D MGSwipeTransitionRotate3D
  17. #define MGSwipeStateSwippingLeftToRight MGSwipeStateSwipingLeftToRight
  18. #define MGSwipeStateSwippingRightToLeft MGSwipeStateSwipingRightToLeft
  19. /** Swipe directions */
  20. typedef NS_ENUM(NSInteger, MGSwipeDirection) {
  21. MGSwipeDirectionLeftToRight = 0,
  22. MGSwipeDirectionRightToLeft
  23. };
  24. /** Swipe state */
  25. typedef NS_ENUM(NSInteger, MGSwipeState) {
  26. MGSwipeStateNone = 0,
  27. MGSwipeStateSwipingLeftToRight,
  28. MGSwipeStateSwipingRightToLeft,
  29. MGSwipeStateExpandingLeftToRight,
  30. MGSwipeStateExpandingRightToLeft,
  31. };
  32. /** Swipe state */
  33. typedef NS_ENUM(NSInteger, MGSwipeExpansionLayout) {
  34. MGSwipeExpansionLayoutBorder = 0,
  35. MGSwipeExpansionLayoutCenter,
  36. MGSwipeExpansionLayoutNone
  37. };
  38. /** Swipe Easing Function */
  39. typedef NS_ENUM(NSInteger, MGSwipeEasingFunction) {
  40. MGSwipeEasingFunctionLinear = 0,
  41. MGSwipeEasingFunctionQuadIn,
  42. MGSwipeEasingFunctionQuadOut,
  43. MGSwipeEasingFunctionQuadInOut,
  44. MGSwipeEasingFunctionCubicIn,
  45. MGSwipeEasingFunctionCubicOut,
  46. MGSwipeEasingFunctionCubicInOut,
  47. MGSwipeEasingFunctionBounceIn,
  48. MGSwipeEasingFunctionBounceOut,
  49. MGSwipeEasingFunctionBounceInOut
  50. };
  51. /**
  52. * Swipe animation settings
  53. **/
  54. @interface MGSwipeAnimation : NSObject
  55. /** Animation duration in seconds. Default value 0.3 */
  56. @property (nonatomic, assign) CGFloat duration;
  57. /** Animation easing function. Default value EaseOutBounce */
  58. @property (nonatomic, assign) MGSwipeEasingFunction easingFunction;
  59. /** Override this method to implement custom easing functions */
  60. -(CGFloat) value:(CGFloat) elapsed duration:(CGFloat) duration from:(CGFloat) from to:(CGFloat) to;
  61. @end
  62. /**
  63. * Swipe settings
  64. **/
  65. @interface MGSwipeSettings: NSObject
  66. /** Transition used while swiping buttons */
  67. @property (nonatomic, assign) MGSwipeTransition transition;
  68. /** Size proportional threshold to hide/keep the buttons when the user ends swiping. Default value 0.5 */
  69. @property (nonatomic, assign) CGFloat threshold;
  70. /** Optional offset to change the swipe buttons position. Relative to the cell border position. Default value: 0
  71. ** For example it can be used to avoid cropped buttons when sectionIndexTitlesForTableView is used in the UITableView
  72. **/
  73. @property (nonatomic, assign) CGFloat offset;
  74. /** Top margin of the buttons relative to the contentView */
  75. @property (nonatomic, assign) CGFloat topMargin;
  76. /** Bottom margin of the buttons relative to the contentView */
  77. @property (nonatomic, assign) CGFloat bottomMargin;
  78. /** Distance between the buttons. Default value : 0 */
  79. @property (nonatomic, assign) CGFloat buttonsDistance;
  80. /** Animation settings when the swipe buttons are shown */
  81. @property (nonatomic, strong, nonnull) MGSwipeAnimation * showAnimation;
  82. /** Animation settings when the swipe buttons are hided */
  83. @property (nonatomic, strong, nonnull) MGSwipeAnimation * hideAnimation;
  84. /** Animation settings when the cell is stretched from the swipe buttons */
  85. @property (nonatomic, strong, nonnull) MGSwipeAnimation * stretchAnimation;
  86. /** Property to read or change swipe animation durations. Default value 0.3 */
  87. @property (nonatomic, assign) CGFloat animationDuration DEPRECATED_ATTRIBUTE;
  88. /** If true the buttons are kept swiped when the threshold is reached and the user ends the gesture
  89. * If false, the buttons are always hidden when the user ends the swipe gesture
  90. */
  91. @property (nonatomic, assign) BOOL keepButtonsSwiped;
  92. /** If true the table cell is not swiped, just the buttons **/
  93. @property (nonatomic, assign) BOOL onlySwipeButtons;
  94. /** If NO the swipe bounces will be disabled, the swipe motion will stop right after the button */
  95. @property (nonatomic, assign) BOOL enableSwipeBounces;
  96. /** Coefficient applied to cell movement in bounce zone. Set to value between 0.0 and 1.0
  97. to make the cell 'resist' swiping after buttons are revealed. Default is 1.0 */
  98. @property (nonatomic, assign) CGFloat swipeBounceRate;
  99. @end
  100. /**
  101. * Expansion settings to make expandable buttons
  102. * Swipe button are not expandable by default
  103. **/
  104. @interface MGSwipeExpansionSettings: NSObject
  105. /** index of the expandable button (in the left or right buttons arrays) */
  106. @property (nonatomic, assign) NSInteger buttonIndex;
  107. /** if true the button fills the cell on trigger, else it bounces back to its initial position */
  108. @property (nonatomic, assign) BOOL fillOnTrigger;
  109. /** Size proportional threshold to trigger the expansion button. Default value 1.5 */
  110. @property (nonatomic, assign) CGFloat threshold;
  111. /** Optional expansion color. Expanded button's background color is used by default **/
  112. @property (nonatomic, strong, nullable) UIColor * expansionColor;
  113. /** Defines the layout of the expanded button **/
  114. @property (nonatomic, assign) MGSwipeExpansionLayout expansionLayout;
  115. /** Animation settings when the expansion is triggered **/
  116. @property (nonatomic, strong, nonnull) MGSwipeAnimation * triggerAnimation;
  117. /** Property to read or change expansion animation durations. Default value 0.2
  118. * The target animation is the change of a button from normal state to expanded state
  119. */
  120. @property (nonatomic, assign) CGFloat animationDuration;
  121. @end
  122. /** helper forward declaration */
  123. @class MGSwipeTableCell;
  124. /**
  125. * Optional delegate to configure swipe buttons or to receive triggered actions.
  126. * Buttons can be configured inline when the cell is created instead of using this delegate,
  127. * but using the delegate improves memory usage because buttons are only created in demand
  128. */
  129. @protocol MGSwipeTableCellDelegate <NSObject>
  130. @optional
  131. /**
  132. * Delegate method to enable/disable swipe gestures
  133. * @return YES if swipe is allowed
  134. **/
  135. -(BOOL) swipeTableCell:(nonnull MGSwipeTableCell*) cell canSwipe:(MGSwipeDirection) direction fromPoint:(CGPoint) point;
  136. -(BOOL) swipeTableCell:(nonnull MGSwipeTableCell*) cell canSwipe:(MGSwipeDirection) direction DEPRECATED_ATTRIBUTE; //backwards compatibility
  137. /**
  138. * Delegate method invoked when the current swipe state changes
  139. @param state the current Swipe State
  140. @param gestureIsActive YES if the user swipe gesture is active. No if the uses has already ended the gesture
  141. **/
  142. -(void) swipeTableCell:(nonnull MGSwipeTableCell*) cell didChangeSwipeState:(MGSwipeState) state gestureIsActive:(BOOL) gestureIsActive;
  143. /**
  144. * Called when the user clicks a swipe button or when a expandable button is automatically triggered
  145. * @return YES to autohide the current swipe buttons
  146. **/
  147. -(BOOL) swipeTableCell:(nonnull MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion;
  148. /**
  149. * Delegate method to setup the swipe buttons and swipe/expansion settings
  150. * Buttons can be any kind of UIView but it's recommended to use the convenience MGSwipeButton class
  151. * Setting up buttons with this delegate instead of using cell properties improves memory usage because buttons are only created in demand
  152. * @param cell the UITableViewCell to configure. You can get the indexPath using [tableView indexPathForCell:cell]
  153. * @param direction The swipe direction (left to right or right to left)
  154. * @param swipeSettings instance to configure the swipe transition and setting (optional)
  155. * @param expansionSettings instance to configure button expansions (optional)
  156. * @return Buttons array
  157. **/
  158. -(nullable NSArray<UIView*>*) swipeTableCell:(nonnull MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
  159. swipeSettings:(nonnull MGSwipeSettings*) swipeSettings expansionSettings:(nonnull MGSwipeExpansionSettings*) expansionSettings;
  160. /**
  161. * Called when the user taps on a swiped cell
  162. * @return YES to autohide the current swipe buttons
  163. **/
  164. -(BOOL) swipeTableCell:(nonnull MGSwipeTableCell *)cell shouldHideSwipeOnTap:(CGPoint) point;
  165. /**
  166. * Called when the cell will begin swiping
  167. * Useful to make cell changes that only are shown after the cell is swiped open
  168. **/
  169. -(void) swipeTableCellWillBeginSwiping:(nonnull MGSwipeTableCell *) cell;
  170. /**
  171. * Called when the cell will end swiping
  172. **/
  173. -(void) swipeTableCellWillEndSwiping:(nonnull MGSwipeTableCell *) cell;
  174. @end
  175. /**
  176. * Swipe Cell class
  177. * To implement swipe cells you have to override from this class
  178. * You can create the cells programmatically, using xibs or storyboards
  179. */
  180. @interface MGSwipeTableCell : UITableViewCell
  181. /** optional delegate (not retained) */
  182. @property (nonatomic, weak, nullable) id<MGSwipeTableCellDelegate> delegate;
  183. /** optional to use contentView alternative. Use this property instead of contentView to support animated views while swiping */
  184. @property (nonatomic, strong, readonly, nonnull) UIView * swipeContentView;
  185. /**
  186. * Left and right swipe buttons and its settings.
  187. * Buttons can be any kind of UIView but it's recommended to use the convenience MGSwipeButton class
  188. */
  189. @property (nonatomic, copy, nonnull) NSArray<UIView*> * leftButtons;
  190. @property (nonatomic, copy, nonnull) NSArray<UIView*> * rightButtons;
  191. @property (nonatomic, strong, nonnull) MGSwipeSettings * leftSwipeSettings;
  192. @property (nonatomic, strong, nonnull) MGSwipeSettings * rightSwipeSettings;
  193. /** Optional settings to allow expandable buttons */
  194. @property (nonatomic, strong, nonnull) MGSwipeExpansionSettings * leftExpansion;
  195. @property (nonatomic, strong, nonnull) MGSwipeExpansionSettings * rightExpansion;
  196. /** Readonly property to fetch the current swipe state */
  197. @property (nonatomic, readonly) MGSwipeState swipeState;
  198. /** Readonly property to check if the user swipe gesture is currently active */
  199. @property (nonatomic, readonly) BOOL isSwipeGestureActive;
  200. // default is NO. Controls whether multiple cells can be swiped simultaneously
  201. @property (nonatomic) BOOL allowsMultipleSwipe;
  202. // default is NO. Controls whether buttons with different width are allowed. Buttons are resized to have the same size by default.
  203. @property (nonatomic) BOOL allowsButtonsWithDifferentWidth;
  204. //default is YES. Controls whether swipe gesture is allowed when the touch starts into the swiped buttons
  205. @property (nonatomic) BOOL allowsSwipeWhenTappingButtons;
  206. //default is YES. Controls whether swipe gesture is allowed in opposite directions. NO value disables swiping in opposite direction once started in one direction
  207. @property (nonatomic) BOOL allowsOppositeSwipe;
  208. // default is NO. Controls whether the cell selection/highlight status is preserved when expansion occurs
  209. @property (nonatomic) BOOL preservesSelectionStatus;
  210. /* default is NO. Controls whether dismissing a swiped cell when tapping outside of the cell generates a real touch event on the other cell.
  211. Default behaviour is the same as the Mail app on iOS. Enable it if you want to allow to start a new swipe while a cell is already in swiped in a single step. */
  212. @property (nonatomic) BOOL touchOnDismissSwipe;
  213. /** Optional background color for swipe overlay. If not set, its inferred automatically from the cell contentView */
  214. @property (nonatomic, strong, nullable) UIColor * swipeBackgroundColor;
  215. /** Property to read or change the current swipe offset programmatically */
  216. @property (nonatomic, assign) CGFloat swipeOffset;
  217. /** Utility methods to show or hide swipe buttons programmatically */
  218. -(void) hideSwipeAnimated: (BOOL) animated;
  219. -(void) hideSwipeAnimated: (BOOL) animated completion:(nullable void(^)(BOOL finished)) completion;
  220. -(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated;
  221. -(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated completion:(nullable void(^)(BOOL finished)) completion;
  222. -(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(nullable void(^)(BOOL finished)) completion;
  223. -(void) setSwipeOffset:(CGFloat)offset animation: (nullable MGSwipeAnimation *) animation completion:(nullable void(^)(BOOL finished)) completion;
  224. -(void) expandSwipe: (MGSwipeDirection) direction animated: (BOOL) animated;
  225. /** Refresh method to be used when you want to update the cell contents while the user is swiping */
  226. -(void) refreshContentView;
  227. /** Refresh method to be used when you want to dynamically change the left or right buttons (add or remove)
  228. * 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)
  229. * @param usingDelegate if YES new buttons will be fetched using the MGSwipeTableCellDelegate. Otherwise new buttons will be fetched from leftButtons/rightButtons properties.
  230. */
  231. -(void) refreshButtons: (BOOL) usingDelegate;
  232. @end