MGSwipeTableCell.m 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 "MGSwipeTableCell.h"
  6. #pragma mark Input Overlay Helper Class
  7. /** Used to capture table input while swipe buttons are visible*/
  8. @interface MGSwipeTableInputOverlay : UIView
  9. @property (nonatomic, weak) MGSwipeTableCell * currentCell;
  10. @end
  11. @implementation MGSwipeTableInputOverlay
  12. -(id) initWithFrame:(CGRect)frame
  13. {
  14. if (self = [super initWithFrame:frame]) {
  15. self.backgroundColor = [UIColor clearColor];
  16. self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  17. }
  18. return self;
  19. }
  20. -(UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event
  21. {
  22. if (_currentCell && CGRectContainsPoint(_currentCell.bounds, [self convertPoint:point toView:_currentCell])) {
  23. return nil;
  24. }
  25. [_currentCell hideSwipeAnimated:YES];
  26. return nil; //return nil to allow swipping a new cell while the current one is hidding
  27. }
  28. @end
  29. #pragma mark Button Container View and transitions
  30. @interface MGSwipeButtonsView : UIView
  31. @property (nonatomic, weak) MGSwipeTableCell * cell;
  32. @end
  33. @implementation MGSwipeButtonsView
  34. {
  35. NSArray * _buttons;
  36. UIView * _container;
  37. BOOL _fromLeft;
  38. UIView * _expandedButton;
  39. UIView * _expandedButtonAnimated;
  40. UIView * _expansionBackground;
  41. UIView * _expansionBackgroundAnimated;
  42. UIColor * _backgroundCopy;
  43. CGRect _expandedButtonBoundsCopy;
  44. MGSwipeExpansionLayout _expansionLayout;
  45. CGFloat _expansionOffset;
  46. BOOL _autoHideExpansion;
  47. }
  48. #pragma mark Layout
  49. -(instancetype) initWithButtons:(NSArray*) buttonsArray direction:(MGSwipeDirection) direction differentWidth:(BOOL) differentWidth
  50. {
  51. CGFloat containerWidth = 0;
  52. CGSize maxSize = CGSizeZero;
  53. for (UIView * button in buttonsArray) {
  54. containerWidth += button.bounds.size.width;
  55. maxSize.width = MAX(maxSize.width, button.bounds.size.width);
  56. maxSize.height = MAX(maxSize.height, button.bounds.size.height);
  57. }
  58. if (!differentWidth) {
  59. containerWidth = maxSize.width * buttonsArray.count;
  60. }
  61. if (self = [super initWithFrame:CGRectMake(0, 0, containerWidth, maxSize.height)]) {
  62. _fromLeft = direction == MGSwipeDirectionLeftToRight;
  63. _container = [[UIView alloc] initWithFrame:self.bounds];
  64. _container.clipsToBounds = YES;
  65. _container.backgroundColor = [UIColor clearColor];
  66. [self addSubview:_container];
  67. _buttons = _fromLeft ? buttonsArray: [[buttonsArray reverseObjectEnumerator] allObjects];
  68. for (UIView * button in _buttons) {
  69. if ([button isKindOfClass:[UIButton class]]) {
  70. [(UIButton *)button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  71. }
  72. if (!differentWidth) {
  73. button.frame = CGRectMake(0, 0, maxSize.width, maxSize.height);
  74. }
  75. button.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  76. [_container insertSubview:button atIndex: _fromLeft ? 0: _container.subviews.count];
  77. }
  78. [self resetButtons];
  79. }
  80. return self;
  81. }
  82. -(void) dealloc
  83. {
  84. for (UIView * button in _buttons) {
  85. if ([button isKindOfClass:[UIButton class]]) {
  86. [(UIButton *)button removeTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  87. }
  88. }
  89. }
  90. -(void) resetButtons
  91. {
  92. CGFloat offsetX = 0;
  93. for (UIView * button in _buttons) {
  94. button.frame = CGRectMake(offsetX, 0, button.bounds.size.width, self.bounds.size.height);
  95. button.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  96. offsetX += button.bounds.size.width;
  97. }
  98. }
  99. -(void) layoutExpansion: (CGFloat) offset
  100. {
  101. _expansionOffset = offset;
  102. _container.frame = CGRectMake(_fromLeft ? 0: self.bounds.size.width - offset, 0, offset, self.bounds.size.height);
  103. if (_expansionBackgroundAnimated && _expandedButtonAnimated) {
  104. _expansionBackgroundAnimated.frame = [self expansionBackgroundRect:_expandedButtonAnimated];
  105. }
  106. }
  107. -(void) layoutSubviews
  108. {
  109. [super layoutSubviews];
  110. if (_expandedButton) {
  111. [self layoutExpansion:_expansionOffset];
  112. }
  113. else {
  114. _container.frame = self.bounds;
  115. }
  116. }
  117. -(CGRect) expansionBackgroundRect: (UIView *) button
  118. {
  119. CGFloat extra = 100.0f; //extra size to avoid expansion background size issue on iOS 7.0
  120. if (_fromLeft) {
  121. return CGRectMake(-extra, 0, button.frame.origin.x + extra, _container.bounds.size.height);
  122. }
  123. else {
  124. return CGRectMake(button.frame.origin.x + button.bounds.size.width, 0,
  125. _container.bounds.size.width - (button.frame.origin.x + button.bounds.size.width ) + extra
  126. ,_container.bounds.size.height);
  127. }
  128. }
  129. -(void) expandToOffset:(CGFloat) offset settings:(MGSwipeExpansionSettings*) settings
  130. {
  131. if (settings.buttonIndex < 0 || settings.buttonIndex >= _buttons.count) {
  132. return;
  133. }
  134. if (!_expandedButton) {
  135. _expandedButton = [_buttons objectAtIndex: _fromLeft ? settings.buttonIndex : _buttons.count - settings.buttonIndex - 1];
  136. CGRect previusRect = _container.frame;
  137. [self layoutExpansion:offset];
  138. [self resetButtons];
  139. if (!_fromLeft) { //Fix expansion animation for right buttons
  140. for (UIView * button in _buttons) {
  141. CGRect frame = button.frame;
  142. frame.origin.x += _container.bounds.size.width - previusRect.size.width;
  143. button.frame = frame;
  144. }
  145. }
  146. _expansionBackground = [[UIView alloc] initWithFrame:[self expansionBackgroundRect:_expandedButton]];
  147. _expansionBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  148. if (settings.expansionColor) {
  149. _backgroundCopy = _expandedButton.backgroundColor;
  150. _expandedButton.backgroundColor = settings.expansionColor;
  151. }
  152. _expansionBackground.backgroundColor = _expandedButton.backgroundColor;
  153. if (UIColor.clearColor == _expandedButton.backgroundColor) {
  154. // Provides access to more complex content for display on the background
  155. _expansionBackground.layer.contents = _expandedButton.layer.contents;
  156. }
  157. [_container addSubview:_expansionBackground];
  158. _expansionLayout = settings.expansionLayout;
  159. CGFloat duration = _fromLeft ? _cell.leftExpansion.animationDuration : _cell.rightExpansion.animationDuration;
  160. [UIView animateWithDuration: duration animations:^{
  161. _expandedButton.hidden = NO;
  162. if (_expansionLayout == MGSwipeExpansionLayoutCenter) {
  163. _expandedButtonBoundsCopy = _expandedButton.bounds;
  164. _expandedButton.layer.mask = nil;
  165. _expandedButton.layer.transform = CATransform3DIdentity;
  166. _expandedButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  167. [_expandedButton.superview bringSubviewToFront:_expandedButton];
  168. _expandedButton.frame = _container.bounds;
  169. }
  170. else if (_fromLeft) {
  171. _expandedButton.frame = CGRectMake(_container.bounds.size.width - _expandedButton.bounds.size.width, 0, _expandedButton.bounds.size.width, _expandedButton.bounds.size.height);
  172. _expandedButton.autoresizingMask|= UIViewAutoresizingFlexibleLeftMargin;
  173. }
  174. else {
  175. _expandedButton.frame = CGRectMake(0, 0, _expandedButton.bounds.size.width, _expandedButton.bounds.size.height);
  176. _expandedButton.autoresizingMask|= UIViewAutoresizingFlexibleRightMargin;
  177. }
  178. _expansionBackground.frame = [self expansionBackgroundRect:_expandedButton];
  179. } completion:^(BOOL finished) {
  180. }];
  181. return;
  182. }
  183. [self layoutExpansion:offset];
  184. }
  185. -(void) endExpansioAnimated:(BOOL) animated
  186. {
  187. if (_expandedButton) {
  188. _expandedButtonAnimated = _expandedButton;
  189. if (_expansionBackgroundAnimated && _expansionBackgroundAnimated != _expansionBackground) {
  190. [_expansionBackgroundAnimated removeFromSuperview];
  191. }
  192. _expansionBackgroundAnimated = _expansionBackground;
  193. _expansionBackground = nil;
  194. _expandedButton = nil;
  195. if (_backgroundCopy) {
  196. _expansionBackgroundAnimated.backgroundColor = _backgroundCopy;
  197. _expandedButtonAnimated.backgroundColor = _backgroundCopy;
  198. _backgroundCopy = nil;
  199. }
  200. CGFloat duration = _fromLeft ? _cell.leftExpansion.animationDuration : _cell.rightExpansion.animationDuration;
  201. [UIView animateWithDuration: animated ? duration : 0.0 animations:^{
  202. _container.frame = self.bounds;
  203. if (_expansionLayout == MGSwipeExpansionLayoutCenter) {
  204. _expandedButtonAnimated.frame = _expandedButtonBoundsCopy;
  205. }
  206. [self resetButtons];
  207. _expansionBackgroundAnimated.frame = [self expansionBackgroundRect:_expandedButtonAnimated];
  208. } completion:^(BOOL finished) {
  209. [_expansionBackgroundAnimated removeFromSuperview];
  210. }];
  211. }
  212. else if (_expansionBackground) {
  213. [_expansionBackground removeFromSuperview];
  214. _expansionBackground = nil;
  215. }
  216. }
  217. -(UIView*) getExpandedButton
  218. {
  219. return _expandedButton;
  220. }
  221. #pragma mark Trigger Actions
  222. -(BOOL) handleClick: (id) sender fromExpansion:(BOOL) fromExpansion
  223. {
  224. bool autoHide = false;
  225. #pragma clang diagnostic push
  226. #pragma clang diagnostic ignored "-Wundeclared-selector"
  227. if ([sender respondsToSelector:@selector(callMGSwipeConvenienceCallback:)]) {
  228. //call convenience block callback if exits (usage of MGSwipeButton class is not compulsory)
  229. autoHide = [sender performSelector:@selector(callMGSwipeConvenienceCallback:) withObject:_cell];
  230. }
  231. #pragma clang diagnostic pop
  232. if (_cell.delegate && [_cell.delegate respondsToSelector:@selector(swipeTableCell:tappedButtonAtIndex:direction:fromExpansion:)]) {
  233. NSInteger index = [_buttons indexOfObject:sender];
  234. if (!_fromLeft) {
  235. index = _buttons.count - index - 1; //right buttons are reversed
  236. }
  237. autoHide|= [_cell.delegate swipeTableCell:_cell tappedButtonAtIndex:index direction:_fromLeft ? MGSwipeDirectionLeftToRight : MGSwipeDirectionRightToLeft fromExpansion:fromExpansion];
  238. }
  239. if (fromExpansion && autoHide) {
  240. _expandedButton = nil;
  241. _cell.swipeOffset = 0;
  242. }
  243. else if (autoHide) {
  244. [_cell hideSwipeAnimated:YES];
  245. }
  246. return autoHide;
  247. }
  248. //button listener
  249. -(void) buttonClicked: (id) sender
  250. {
  251. [self handleClick:sender fromExpansion:NO];
  252. }
  253. #pragma mark Transitions
  254. -(void) transitionStatic:(CGFloat) t
  255. {
  256. const CGFloat dx = self.bounds.size.width * (1.0 - t);
  257. CGFloat offsetX = 0;
  258. for (UIView *button in _buttons) {
  259. CGRect frame = button.frame;
  260. frame.origin.x = offsetX + (_fromLeft ? dx : -dx);
  261. button.frame = frame;
  262. offsetX += frame.size.width;
  263. }
  264. }
  265. -(void) transitionDrag:(CGFloat) t
  266. {
  267. //No Op, nothing to do ;)
  268. }
  269. -(void) transitionClip:(CGFloat) t
  270. {
  271. CGFloat selfWidth = self.bounds.size.width;
  272. CGFloat offsetX = 0;
  273. for (UIView *button in _buttons) {
  274. CGRect frame = button.frame;
  275. CGFloat dx = roundf(frame.size.width * 0.5 * (1.0 - t)) ;
  276. frame.origin.x = _fromLeft ? (selfWidth - frame.size.width - offsetX) * (1.0 - t) + offsetX + dx : offsetX * t - dx;
  277. button.frame = frame;
  278. CAShapeLayer *maskLayer = [CAShapeLayer new];
  279. CGRect maskRect = CGRectMake(dx - 0.5, 0, frame.size.width - 2 * dx + 1.5, frame.size.height);
  280. CGPathRef path = CGPathCreateWithRect(maskRect, NULL);
  281. maskLayer.path = path;
  282. CGPathRelease(path);
  283. button.layer.mask = maskLayer;
  284. offsetX += frame.size.width;
  285. }
  286. }
  287. -(void) transtitionFloatBorder:(CGFloat) t
  288. {
  289. CGFloat selfWidth = self.bounds.size.width;
  290. CGFloat offsetX = 0;
  291. for (UIView *button in _buttons) {
  292. CGRect frame = button.frame;
  293. frame.origin.x = _fromLeft ? (selfWidth - frame.size.width - offsetX) * (1.0 - t) + offsetX : offsetX * t;
  294. button.frame = frame;
  295. offsetX += frame.size.width;
  296. }
  297. }
  298. -(void) transition3D:(CGFloat) t
  299. {
  300. const CGFloat invert = _fromLeft ? 1.0 : -1.0;
  301. const CGFloat angle = M_PI_2 * (1.0 - t) * invert;
  302. CATransform3D transform = CATransform3DIdentity;
  303. transform.m34 = -1.0/400.0f; //perspective 1/z
  304. const CGFloat dx = -_container.bounds.size.width * 0.5 * invert;
  305. const CGFloat offset = dx * 2 * (1.0 - t);
  306. transform = CATransform3DTranslate(transform, dx - offset, 0, 0);
  307. transform = CATransform3DRotate(transform, angle, 0.0, 1.0, 0.0);
  308. transform = CATransform3DTranslate(transform, -dx, 0, 0);
  309. _container.layer.transform = transform;
  310. }
  311. -(void) transition:(MGSwipeTransition) mode percent:(CGFloat) t
  312. {
  313. switch (mode) {
  314. case MGSwipeTransitionStatic: [self transitionStatic:t]; break;
  315. case MGSwipeTransitionDrag: [self transitionDrag:t]; break;
  316. case MGSwipeTransitionClipCenter: [self transitionClip:t]; break;
  317. case MGSwipeTransitionBorder: [self transtitionFloatBorder:t]; break;
  318. case MGSwipeTransition3D: [self transition3D:t]; break;
  319. }
  320. if (_expandedButtonAnimated && _expansionBackgroundAnimated) {
  321. _expansionBackgroundAnimated.frame = [self expansionBackgroundRect:_expandedButtonAnimated];
  322. }
  323. }
  324. @end
  325. #pragma mark Settings Classes
  326. @implementation MGSwipeSettings
  327. -(instancetype) init
  328. {
  329. if (self = [super init]) {
  330. self.transition = MGSwipeTransitionBorder;
  331. self.threshold = 0.5;
  332. self.offset = 0;
  333. self.animationDuration = 0.3;
  334. }
  335. return self;
  336. }
  337. @end
  338. @implementation MGSwipeExpansionSettings
  339. -(instancetype) init
  340. {
  341. if (self = [super init]) {
  342. self.buttonIndex = -1;
  343. self.threshold = 1.3;
  344. self.animationDuration = 0.2;
  345. }
  346. return self;
  347. }
  348. @end
  349. typedef struct MGSwipeAnimationData {
  350. CGFloat from;
  351. CGFloat to;
  352. CFTimeInterval duration;
  353. CFTimeInterval start;
  354. } MGSwipeAnimationData;
  355. #pragma mark MGSwipeTableCell Implementation
  356. @implementation MGSwipeTableCell
  357. {
  358. UITapGestureRecognizer * _tapRecognizer;
  359. UIPanGestureRecognizer * _panRecognizer;
  360. CGPoint _panStartPoint;
  361. CGFloat _panStartOffset;
  362. CGFloat _targetOffset;
  363. UIView * _swipeOverlay;
  364. UIImageView * _swipeView;
  365. UIView * _swipeContentView;
  366. MGSwipeButtonsView * _leftView;
  367. MGSwipeButtonsView * _rightView;
  368. bool _allowSwipeRightToLeft;
  369. bool _allowSwipeLeftToRight;
  370. __weak MGSwipeButtonsView * _activeExpansion;
  371. MGSwipeTableInputOverlay * _tableInputOverlay;
  372. bool _overlayEnabled;
  373. __weak UITableView * _cachedParentTable;
  374. UITableViewCellSelectionStyle _previusSelectionStyle;
  375. NSMutableSet * _previusHiddenViews;
  376. BOOL _triggerStateChanges;
  377. MGSwipeAnimationData _animationData;
  378. void (^_animationCompletion)();
  379. CADisplayLink * _displayLink;
  380. }
  381. #pragma mark View creation & layout
  382. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  383. {
  384. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  385. if (self) {
  386. [self initViews:YES];
  387. }
  388. return self;
  389. }
  390. - (id)initWithCoder:(NSCoder*)aDecoder
  391. {
  392. if(self = [super initWithCoder:aDecoder]) {
  393. [self initViews:YES];
  394. }
  395. return self;
  396. }
  397. -(void) awakeFromNib
  398. {
  399. [super awakeFromNib];
  400. if (!_panRecognizer) {
  401. [self initViews:YES];
  402. }
  403. }
  404. -(void) dealloc
  405. {
  406. [self hideSwipeOverlayIfNeeded];
  407. }
  408. -(void) initViews: (BOOL) cleanButtons
  409. {
  410. if (cleanButtons) {
  411. _leftButtons = [NSArray array];
  412. _rightButtons = [NSArray array];
  413. _leftSwipeSettings = [[MGSwipeSettings alloc] init];
  414. _rightSwipeSettings = [[MGSwipeSettings alloc] init];
  415. _leftExpansion = [[MGSwipeExpansionSettings alloc] init];
  416. _rightExpansion = [[MGSwipeExpansionSettings alloc] init];
  417. }
  418. _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
  419. [self addGestureRecognizer:_panRecognizer];
  420. _panRecognizer.delegate = self;
  421. _activeExpansion = nil;
  422. _previusHiddenViews = [NSMutableSet set];
  423. _swipeState = MGSwipeStateNone;
  424. _triggerStateChanges = YES;
  425. _allowsSwipeWhenTappingButtons = YES;
  426. }
  427. -(void) cleanViews
  428. {
  429. [self hideSwipeAnimated:NO];
  430. if (_displayLink) {
  431. [_displayLink invalidate];
  432. _displayLink = nil;
  433. }
  434. if (_swipeOverlay) {
  435. [_swipeOverlay removeFromSuperview];
  436. _swipeOverlay = nil;
  437. }
  438. _leftView = _rightView = nil;
  439. if (_panRecognizer) {
  440. _panRecognizer.delegate = nil;
  441. [self removeGestureRecognizer:_panRecognizer];
  442. _panRecognizer = nil;
  443. }
  444. }
  445. -(UIView *) swipeContentView
  446. {
  447. if (!_swipeContentView) {
  448. _swipeContentView = [[UIView alloc] initWithFrame:self.contentView.bounds];
  449. _swipeContentView.backgroundColor = [UIColor clearColor];
  450. _swipeContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  451. _swipeContentView.layer.zPosition = 9;
  452. [self.contentView addSubview:_swipeContentView];
  453. }
  454. return _swipeContentView;
  455. }
  456. -(void) layoutSubviews
  457. {
  458. [super layoutSubviews];
  459. if (_swipeContentView) {
  460. _swipeContentView.frame = self.contentView.bounds;
  461. }
  462. if (_swipeOverlay) {
  463. _swipeOverlay.frame = CGRectMake(0, 0, self.bounds.size.width, self.contentView.bounds.size.height);
  464. }
  465. }
  466. -(void) fetchButtonsIfNeeded
  467. {
  468. if (_leftButtons.count == 0 && _delegate && [_delegate respondsToSelector:@selector(swipeTableCell:swipeButtonsForDirection:swipeSettings:expansionSettings:)]) {
  469. _leftButtons = [_delegate swipeTableCell:self swipeButtonsForDirection:MGSwipeDirectionLeftToRight swipeSettings:_leftSwipeSettings expansionSettings:_leftExpansion];
  470. }
  471. if (_rightButtons.count == 0 && _delegate && [_delegate respondsToSelector:@selector(swipeTableCell:swipeButtonsForDirection:swipeSettings:expansionSettings:)]) {
  472. _rightButtons = [_delegate swipeTableCell:self swipeButtonsForDirection:MGSwipeDirectionRightToLeft swipeSettings:_rightSwipeSettings expansionSettings:_rightExpansion];
  473. }
  474. }
  475. -(void) createSwipeViewIfNeeded
  476. {
  477. if (!_swipeOverlay) {
  478. _swipeOverlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
  479. _swipeOverlay.hidden = YES;
  480. _swipeOverlay.backgroundColor = [self backgroundColorForSwipe];
  481. _swipeOverlay.layer.zPosition = 10; //force render on top of the contentView;
  482. _swipeView = [[UIImageView alloc] initWithFrame:_swipeOverlay.bounds];
  483. _swipeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  484. _swipeView.contentMode = UIViewContentModeCenter;
  485. _swipeView.clipsToBounds = YES;
  486. [_swipeOverlay addSubview:_swipeView];
  487. [self.contentView addSubview:_swipeOverlay];
  488. }
  489. [self fetchButtonsIfNeeded];
  490. if (!_leftView && _leftButtons.count > 0) {
  491. _leftView = [[MGSwipeButtonsView alloc] initWithButtons:_leftButtons direction:MGSwipeDirectionLeftToRight differentWidth:_allowsButtonsWithDifferentWidth];
  492. _leftView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
  493. _leftView.cell = self;
  494. _leftView.frame = CGRectMake(-_leftView.bounds.size.width, 0, _leftView.bounds.size.width, _swipeOverlay.bounds.size.height);
  495. [_swipeOverlay addSubview:_leftView];
  496. }
  497. if (!_rightView && _rightButtons.count > 0) {
  498. _rightView = [[MGSwipeButtonsView alloc] initWithButtons:_rightButtons direction:MGSwipeDirectionRightToLeft differentWidth:_allowsButtonsWithDifferentWidth];
  499. _rightView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
  500. _rightView.cell = self;
  501. _rightView.frame = CGRectMake(_swipeOverlay.bounds.size.width, 0, _rightView.bounds.size.width, _swipeOverlay.bounds.size.height);
  502. [_swipeOverlay addSubview:_rightView];
  503. }
  504. }
  505. - (void) showSwipeOverlayIfNeeded
  506. {
  507. if (_overlayEnabled) {
  508. return;
  509. }
  510. _overlayEnabled = YES;
  511. self.selected = NO;
  512. if (_swipeContentView)
  513. [_swipeContentView removeFromSuperview];
  514. _swipeView.image = [self imageFromView:self];
  515. _swipeOverlay.hidden = NO;
  516. if (_swipeContentView)
  517. [_swipeView addSubview:_swipeContentView];
  518. if (!_allowsMultipleSwipe) {
  519. //input overlay on the whole table
  520. UITableView * table = [self parentTable];
  521. _tableInputOverlay = [[MGSwipeTableInputOverlay alloc] initWithFrame:table.bounds];
  522. _tableInputOverlay.currentCell = self;
  523. [table addSubview:_tableInputOverlay];
  524. }
  525. _previusSelectionStyle = self.selectionStyle;
  526. self.selectionStyle = UITableViewCellSelectionStyleNone;
  527. [self setAccesoryViewsHidden:YES];
  528. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
  529. _tapRecognizer.cancelsTouchesInView = YES;
  530. _tapRecognizer.delegate = self;
  531. [self addGestureRecognizer:_tapRecognizer];
  532. }
  533. -(void) hideSwipeOverlayIfNeeded
  534. {
  535. if (!_overlayEnabled) {
  536. return;
  537. }
  538. _overlayEnabled = NO;
  539. _swipeOverlay.hidden = YES;
  540. _swipeView.image = nil;
  541. if (_swipeContentView) {
  542. [_swipeContentView removeFromSuperview];
  543. [self.contentView addSubview:_swipeContentView];
  544. }
  545. if (_tableInputOverlay) {
  546. [_tableInputOverlay removeFromSuperview];
  547. _tableInputOverlay = nil;
  548. }
  549. self.selectionStyle = _previusSelectionStyle;
  550. NSArray * selectedRows = self.parentTable.indexPathsForSelectedRows;
  551. if ([selectedRows containsObject:[self.parentTable indexPathForCell:self]]) {
  552. self.selected = YES;
  553. }
  554. [self setAccesoryViewsHidden:NO];
  555. if (_tapRecognizer) {
  556. [self removeGestureRecognizer:_tapRecognizer];
  557. _tapRecognizer = nil;
  558. }
  559. }
  560. -(void) refreshContentView
  561. {
  562. CGFloat currentOffset = _swipeOffset;
  563. BOOL prevValue = _triggerStateChanges;
  564. _triggerStateChanges = NO;
  565. self.swipeOffset = 0;
  566. self.swipeOffset = currentOffset;
  567. _triggerStateChanges = prevValue;
  568. }
  569. -(void) refreshButtons: (BOOL) usingDelegate
  570. {
  571. if (usingDelegate) {
  572. self.leftButtons = @[];
  573. self.rightButtons = @[];
  574. }
  575. if (_leftView) {
  576. [_leftView removeFromSuperview];
  577. _leftView = nil;
  578. }
  579. if (_rightView) {
  580. [_rightView removeFromSuperview];
  581. _rightView = nil;
  582. }
  583. [self createSwipeViewIfNeeded];
  584. [self refreshContentView];
  585. }
  586. #pragma mark Handle Table Events
  587. -(void) willMoveToSuperview:(UIView *)newSuperview;
  588. {
  589. if (newSuperview == nil) { //remove the table overlay when a cell is removed from the table
  590. [self hideSwipeOverlayIfNeeded];
  591. }
  592. }
  593. -(void) prepareForReuse
  594. {
  595. [super prepareForReuse];
  596. [self cleanViews];
  597. if (_swipeState != MGSwipeStateNone) {
  598. _triggerStateChanges = YES;
  599. [self updateState:MGSwipeStateNone];
  600. }
  601. BOOL cleanButtons = _delegate && [_delegate respondsToSelector:@selector(swipeTableCell:swipeButtonsForDirection:swipeSettings:expansionSettings:)];
  602. [self initViews:cleanButtons];
  603. }
  604. -(void) setEditing:(BOOL)editing animated:(BOOL)animated
  605. {
  606. [super setEditing:editing animated:animated];
  607. if (editing) { //disable swipe buttons when the user sets table editing mode
  608. self.swipeOffset = 0;
  609. }
  610. }
  611. -(void) setEditing:(BOOL)editing
  612. {
  613. [super setEditing:YES];
  614. if (editing) { //disable swipe buttons when the user sets table editing mode
  615. self.swipeOffset = 0;
  616. }
  617. }
  618. -(UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event
  619. {
  620. if (_swipeOverlay && !_swipeOverlay.hidden) {
  621. //override hitTest to give swipe buttons a higher priority (diclosure buttons can steal input)
  622. UIView * targets[] = {_leftView, _rightView};
  623. for (int i = 0; i< 2; ++i) {
  624. UIView * target = targets[i];
  625. if (!target) continue;
  626. CGPoint p = [self convertPoint:point toView:target];
  627. if (CGRectContainsPoint(target.bounds, p)) {
  628. return [target hitTest:p withEvent:event];
  629. }
  630. }
  631. }
  632. return [super hitTest:point withEvent:event];
  633. }
  634. #pragma mark Some utility methods
  635. - (UIImage *)imageFromView:(UIView *)view {
  636. UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [[UIScreen mainScreen] scale]);
  637. [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  638. UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
  639. UIGraphicsEndImageContext();
  640. return image;
  641. }
  642. -(void) setAccesoryViewsHidden: (BOOL) hidden
  643. {
  644. if (self.accessoryView) {
  645. self.accessoryView.hidden = hidden;
  646. }
  647. for (UIView * view in self.contentView.superview.subviews) {
  648. if (view != self.contentView && ([view isKindOfClass:[UIButton class]] || [NSStringFromClass(view.class) rangeOfString:@"Disclosure"].location != NSNotFound)) {
  649. view.hidden = hidden;
  650. }
  651. }
  652. for (UIView * view in self.contentView.subviews) {
  653. if (view == _swipeOverlay || view == _swipeContentView) continue;
  654. if (hidden && !view.hidden) {
  655. view.hidden = YES;
  656. [_previusHiddenViews addObject:view];
  657. }
  658. else if (!hidden && [_previusHiddenViews containsObject:view]) {
  659. view.hidden = NO;
  660. }
  661. }
  662. if (!hidden) {
  663. [_previusHiddenViews removeAllObjects];
  664. }
  665. }
  666. -(UIColor *) backgroundColorForSwipe
  667. {
  668. if (_swipeBackgroundColor) {
  669. return _swipeBackgroundColor; //user defined color
  670. }
  671. else if (self.contentView.backgroundColor && ![self.contentView.backgroundColor isEqual:[UIColor clearColor]]) {
  672. return self.contentView.backgroundColor;
  673. }
  674. else if (self.backgroundColor) {
  675. return self.backgroundColor;
  676. }
  677. return [UIColor clearColor];
  678. }
  679. -(UITableView *) parentTable
  680. {
  681. if (_cachedParentTable) {
  682. return _cachedParentTable;
  683. }
  684. UIView * view = self.superview;
  685. while(view != nil) {
  686. if([view isKindOfClass:[UITableView class]]) {
  687. _cachedParentTable = (UITableView*) view;
  688. }
  689. view = view.superview;
  690. }
  691. return _cachedParentTable;
  692. }
  693. -(void) updateState: (MGSwipeState) newState;
  694. {
  695. if (!_triggerStateChanges || _swipeState == newState) {
  696. return;
  697. }
  698. _swipeState = newState;
  699. if (_delegate && [_delegate respondsToSelector:@selector(swipeTableCell:didChangeSwipeState:gestureIsActive:)]) {
  700. [_delegate swipeTableCell:self didChangeSwipeState:_swipeState gestureIsActive: self.isSwipeGestureActive] ;
  701. }
  702. }
  703. #pragma mark Swipe Animation
  704. - (void)setSwipeOffset:(CGFloat) newOffset;
  705. {
  706. _swipeOffset = newOffset;
  707. CGFloat sign1 = newOffset > 0 ? 1.0 : -1.0;
  708. CGFloat offset = fabs(newOffset);
  709. MGSwipeButtonsView * activeButtons = sign1 < 0 ? _rightView : _leftView;
  710. if (!activeButtons || offset == 0) {
  711. if (_leftView)
  712. [_leftView endExpansioAnimated:NO];
  713. if (_rightView)
  714. [_rightView endExpansioAnimated:NO];
  715. [self hideSwipeOverlayIfNeeded];
  716. _targetOffset = 0;
  717. [self updateState:MGSwipeStateNone];
  718. return;
  719. }
  720. else {
  721. [self showSwipeOverlayIfNeeded];
  722. CGFloat swipeThreshold = sign1 < 0 ? _rightSwipeSettings.threshold : _leftSwipeSettings.threshold;
  723. _targetOffset = offset > activeButtons.bounds.size.width * swipeThreshold ? activeButtons.bounds.size.width * sign1 : 0;
  724. }
  725. _swipeView.transform = CGAffineTransformMakeTranslation(newOffset, 0);
  726. //animate existing buttons
  727. MGSwipeButtonsView* but[2] = {_leftView, _rightView};
  728. MGSwipeSettings* settings[2] = {_leftSwipeSettings, _rightSwipeSettings};
  729. MGSwipeExpansionSettings * expansions[2] = {_leftExpansion, _rightExpansion};
  730. for (int i = 0; i< 2; ++i) {
  731. MGSwipeButtonsView * view = but[i];
  732. if (!view) continue;
  733. //buttons view position
  734. CGFloat translation = MIN(offset, view.bounds.size.width) * sign1 + settings[i].offset * sign1;
  735. view.transform = CGAffineTransformMakeTranslation(translation, 0);
  736. if (view != activeButtons) continue; //only transition if active (perf. improvement)
  737. bool expand = expansions[i].buttonIndex >= 0 && offset > view.bounds.size.width * expansions[i].threshold;
  738. if (expand) {
  739. [view expandToOffset:offset settings:expansions[i]];
  740. _targetOffset = expansions[i].fillOnTrigger ? self.bounds.size.width * sign1 : 0;
  741. _activeExpansion = view;
  742. [self updateState:i ? MGSwipeStateExpandingRightToLeft : MGSwipeStateExpandingLeftToRight];
  743. }
  744. else {
  745. [view endExpansioAnimated:YES];
  746. _activeExpansion = nil;
  747. CGFloat t = MIN(1.0f, offset/view.bounds.size.width);
  748. [view transition:settings[i].transition percent:t];
  749. [self updateState:i ? MGSwipeStateSwippingRightToLeft : MGSwipeStateSwippingLeftToRight];
  750. }
  751. }
  752. }
  753. -(void) updateSwipe: (CGFloat) offset
  754. {
  755. bool allowed = offset > 0 ? _allowSwipeLeftToRight : _allowSwipeRightToLeft;
  756. UIView * buttons = offset > 0 ? _leftView : _rightView;
  757. if (!buttons || ! allowed) {
  758. offset = 0;
  759. }
  760. self.swipeOffset = offset;
  761. }
  762. -(void) hideSwipeAnimated: (BOOL) animated completion:(void(^)()) completion
  763. {
  764. [self setSwipeOffset:0 animated:animated completion:completion];
  765. }
  766. -(void) hideSwipeAnimated: (BOOL) animated
  767. {
  768. [self setSwipeOffset:0 animated:animated completion:nil];
  769. }
  770. -(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated
  771. {
  772. [self showSwipe:direction animated:animated completion:nil];
  773. }
  774. -(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated completion:(void(^)()) completion
  775. {
  776. [self createSwipeViewIfNeeded];
  777. _allowSwipeLeftToRight = _leftButtons.count > 0;
  778. _allowSwipeRightToLeft = _rightButtons.count > 0;
  779. UIView * buttonsView = direction == MGSwipeDirectionLeftToRight ? _leftView : _rightView;
  780. if (buttonsView) {
  781. CGFloat s = direction == MGSwipeDirectionLeftToRight ? 1.0 : -1.0;
  782. [self setSwipeOffset:buttonsView.bounds.size.width * s animated:animated completion:completion];
  783. }
  784. }
  785. -(void) expandSwipe: (MGSwipeDirection) direction animated: (BOOL) animated
  786. {
  787. CGFloat s = direction == MGSwipeDirectionLeftToRight ? 1.0 : -1.0;
  788. MGSwipeExpansionSettings* expSetting = direction == MGSwipeDirectionLeftToRight ? _leftExpansion : _rightExpansion;
  789. // only perform animation if there's no pending expansion animation and requested direction has fillOnTrigger enabled
  790. if(!_activeExpansion && expSetting.fillOnTrigger) {
  791. [self createSwipeViewIfNeeded];
  792. _allowSwipeLeftToRight = _leftButtons.count > 0;
  793. _allowSwipeRightToLeft = _rightButtons.count > 0;
  794. UIView * buttonsView = direction == MGSwipeDirectionLeftToRight ? _leftView : _rightView;
  795. if (buttonsView) {
  796. __weak MGSwipeButtonsView * expansionView = direction == MGSwipeDirectionLeftToRight ? _leftView : _rightView;
  797. __weak MGSwipeTableCell * weakself = self;
  798. [self setSwipeOffset:buttonsView.bounds.size.width * s * expSetting.threshold * 2 animated:animated completion:^{
  799. [expansionView endExpansioAnimated:YES];
  800. [weakself setSwipeOffset:0 animated:NO completion:nil];
  801. }];
  802. }
  803. }
  804. }
  805. -(void) animationTick: (CADisplayLink *) timer
  806. {
  807. if (!_animationData.start) {
  808. _animationData.start = timer.timestamp;
  809. }
  810. CFTimeInterval elapsed = timer.timestamp - _animationData.start;
  811. CGFloat t = MIN(elapsed/_animationData.duration, 1.0f);
  812. bool completed = t>=1.0f;
  813. if (completed) {
  814. _triggerStateChanges = YES;
  815. }
  816. //CubicEaseOut interpolation
  817. t--;
  818. self.swipeOffset = (t * t * t + 1.0) * (_animationData.to - _animationData.from) + _animationData.from;
  819. //call animation completion and invalidate timer
  820. if (completed){
  821. [timer invalidate];
  822. _displayLink = nil;
  823. if (_animationCompletion) {
  824. _animationCompletion();
  825. }
  826. }
  827. }
  828. -(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(void(^)()) completion
  829. {
  830. _animationCompletion = completion;
  831. if (_displayLink) {
  832. [_displayLink invalidate];
  833. _displayLink = nil;
  834. }
  835. if (!animated) {
  836. self.swipeOffset = offset;
  837. return;
  838. }
  839. _triggerStateChanges = NO;
  840. _animationData.from = _swipeOffset;
  841. _animationData.to = offset;
  842. _animationData.duration = _swipeOffset > 0 ? _leftSwipeSettings.animationDuration : _rightSwipeSettings.animationDuration;
  843. _animationData.start = 0;
  844. _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(animationTick:)];
  845. [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
  846. }
  847. #pragma mark Gestures
  848. -(void) cancelPanGesture
  849. {
  850. if (_panRecognizer.state != UIGestureRecognizerStateEnded) {
  851. _panRecognizer.enabled = NO;
  852. _panRecognizer.enabled = YES;
  853. [self hideSwipeAnimated:YES];
  854. }
  855. }
  856. -(void) tapHandler: (UITapGestureRecognizer *) recognizer
  857. {
  858. [self hideSwipeAnimated:YES];
  859. }
  860. -(void) panHandler: (UIPanGestureRecognizer *)gesture
  861. {
  862. CGPoint current = [gesture translationInView:self];
  863. if (gesture.state == UIGestureRecognizerStateBegan) {
  864. self.highlighted = NO;
  865. [self createSwipeViewIfNeeded];
  866. _panStartPoint = current;
  867. _panStartOffset = _swipeOffset;
  868. if (!_allowsMultipleSwipe) {
  869. NSArray * cells = [self parentTable].visibleCells;
  870. for (MGSwipeTableCell * cell in cells) {
  871. if ([cell isKindOfClass:[MGSwipeTableCell class]] && cell != self) {
  872. [cell cancelPanGesture];
  873. }
  874. }
  875. }
  876. }
  877. else if (gesture.state == UIGestureRecognizerStateChanged) {
  878. CGFloat offset = _panStartOffset + current.x - _panStartPoint.x;
  879. [self updateSwipe:offset];
  880. }
  881. else {
  882. MGSwipeButtonsView * expansion = _activeExpansion;
  883. if (expansion) {
  884. UIView * expandedButton = [expansion getExpandedButton];
  885. [self setSwipeOffset:_targetOffset animated:YES completion:^{
  886. BOOL autoHide = [expansion handleClick:expandedButton fromExpansion:YES];
  887. if (autoHide) {
  888. [expansion endExpansioAnimated:NO];
  889. }
  890. }];
  891. }
  892. else {
  893. CGFloat velocity = [_panRecognizer velocityInView:self].x;
  894. CGFloat inertiaThreshold = 100.0; //points per second
  895. if (velocity > inertiaThreshold) {
  896. _targetOffset = _swipeOffset < 0 ? 0 : (_leftView ? _leftView.bounds.size.width : _targetOffset);
  897. }
  898. else if (velocity < -inertiaThreshold) {
  899. _targetOffset = _swipeOffset > 0 ? 0 : (_rightView ? -_rightView.bounds.size.width : _targetOffset);
  900. }
  901. [self setSwipeOffset:_targetOffset animated:YES completion:nil];
  902. }
  903. }
  904. }
  905. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  906. if (gestureRecognizer == _panRecognizer) {
  907. if (self.isEditing) {
  908. return NO; //do not swipe while editing table
  909. }
  910. CGPoint translation = [_panRecognizer translationInView:self];
  911. if (fabs(translation.y) > fabs(translation.x)) {
  912. return NO; // user is scrolling vertically
  913. }
  914. if (_swipeView) {
  915. CGPoint point = [_tapRecognizer locationInView:_swipeView];
  916. if (!CGRectContainsPoint(_swipeView.bounds, point)) {
  917. return _allowsSwipeWhenTappingButtons; //user clicked outside the cell or in the buttons area
  918. }
  919. }
  920. if (_swipeOffset != 0.0) {
  921. return YES; //already swipped, don't need to check buttons or canSwipe delegate
  922. }
  923. //make a decision according to existing buttons or using the optional delegate
  924. if (_delegate && [_delegate respondsToSelector:@selector(swipeTableCell:canSwipe:)]) {
  925. _allowSwipeLeftToRight = [_delegate swipeTableCell:self canSwipe:MGSwipeDirectionLeftToRight];
  926. _allowSwipeRightToLeft = [_delegate swipeTableCell:self canSwipe:MGSwipeDirectionRightToLeft];
  927. }
  928. else {
  929. [self fetchButtonsIfNeeded];
  930. _allowSwipeLeftToRight = _leftButtons.count > 0;
  931. _allowSwipeRightToLeft = _rightButtons.count > 0;
  932. }
  933. return (_allowSwipeLeftToRight && translation.x > 0) || (_allowSwipeRightToLeft && translation.x < 0);
  934. }
  935. else if (gestureRecognizer == _tapRecognizer) {
  936. CGPoint point = [_tapRecognizer locationInView:_swipeView];
  937. return CGRectContainsPoint(_swipeView.bounds, point);
  938. }
  939. return YES;
  940. }
  941. -(BOOL) isSwipeGestureActive
  942. {
  943. return _panRecognizer.state == UIGestureRecognizerStateBegan || _panRecognizer.state == UIGestureRecognizerStateChanged;
  944. }
  945. @end