forked from b051/RSSwipeMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSSwipeMenuGestureRecognizer.m
72 lines (63 loc) · 1.82 KB
/
RSSwipeMenuGestureRecognizer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// RSSwipeMenuGestureRecognizer.m
//
// Created by Rex Sheng on 8/6/12.
// Copyright (c) 2012 lognllc.com. All rights reserved.
//
#import "RSSwipeMenuGestureRecognizer.h"
@implementation RSSwipeMenuGestureRecognizer
- (id) initWithTarget:(id)target action:(SEL)action
{
if ((self = [super initWithTarget:target action:action])) {
self.maximumNumberOfTouches = 1;
}
return self;
}
- (void)reset
{
[super reset];
self.indexPath = nil;
_cell = nil;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed || _indexPath) return;
UITableView *tableView = (UITableView *)self.view;
[tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:NO];
CGPoint curr = [[touches anyObject] locationInView:tableView];
CGPoint prev = [[touches anyObject] previousLocationInView:tableView];
CGFloat horizontalWin = ABS(prev.x - curr.x) - ABS(curr.y - prev.y);
if (horizontalWin > 0) {
_indexPath = [tableView indexPathForRowAtPoint:curr];
_cell = nil;
for (UITableViewCell *cell in [tableView visibleCells]) {
if (CGRectContainsPoint(cell.frame, curr)) {
_cell = cell;
break;
}
}
if (_cell == nil) {
self.state = UIGestureRecognizerStateFailed;
return;
}
_cell.selected = NO;
} else {
self.state = UIGestureRecognizerStateFailed;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
self.state = UIGestureRecognizerStateEnded;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
self.state = UIGestureRecognizerStateCancelled;
}
@end