-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECTableViewController.m
127 lines (100 loc) · 3.92 KB
/
ECTableViewController.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// PPTableViewController.m
// Test
//
// Created by Evgeny Cherpak on 4/7/13.
// Copyright (c) 2013 Evgeny Cherpak. All rights reserved.
//
#import "ECTableViewController.h"
#import "ECBalloonView.h"
@interface ECTableViewController ()
@property (nonatomic, strong) UILongPressGestureRecognizer* longPressGestureRecognizer;
@property (nonatomic, strong) UITapGestureRecognizer* tapGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer* swipeGestureRecognizer;
@property (nonatomic, strong) ECBalloonView* tooltipView;
@property (nonatomic, strong) NSIndexPath* tooltipIndexPath;
@end
@implementation ECTableViewController
- (UITableViewCell*)cellByLocation:(CGPoint)point
{
for (UITableViewCell* cell in self.tableView.visibleCells) {
if (CGRectContainsPoint(cell.frame, point)) {
return cell;
}
}
return nil;
}
- (void)longPressGestureRecognizer:(UILongPressGestureRecognizer*)sender
{
CGPoint location = [sender locationInView:self.view];
UITableViewCell* cell = [self cellByLocation:location];
NSString *text = cell.textLabel.text;
// NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//
// if ( [self.tooltipIndexPath compare:indexPath] == NSOrderedSame && [self.tooltipView.text isEqualToString:text] ) {
// // we have nothing to do here...
// } else {
if ( self.tooltipView ) {
[self.tooltipView removeFromSuperview];
}
if ( cell ) {
CGSize tooltipSize = [ECBalloonView sizeInView:self.tableView withText:text];
ECArrowDirection arrowDirection;
if ( cell.frame.origin.y - tooltipSize.height > self.tableView.contentOffset.y ) {
location.y = cell.frame.origin.y - tooltipSize.height;
arrowDirection = ECArrowDirectionDown;
} else {
location.y = cell.frame.origin.y + cell.frame.size.height;
arrowDirection = ECArrowDirectionUp;
}
self.tooltipView = [[ECBalloonView alloc] initWithPoint:location andText:text andArrowDirection:arrowDirection];
[self.view addSubview:self.tooltipView];
[self.tableView setScrollEnabled:NO];
// }
}
}
- (void)tapGestureRecognizer:(UITapGestureRecognizer*)sender
{
if ( self.tooltipView ) {
[self resetTooltip];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
self.tapGestureRecognizer.numberOfTapsRequired = 1;
self.tapGestureRecognizer.delegate = self;
self.tapGestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:self.tapGestureRecognizer];
self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizer:)];
self.longPressGestureRecognizer.delegate = self;
[self.longPressGestureRecognizer requireGestureRecognizerToFail:self.tapGestureRecognizer];
[self.view addGestureRecognizer:self.longPressGestureRecognizer];
}
- (void)resetTooltip
{
[self.tooltipView removeFromSuperview];
self.tooltipView = nil;
[self.tableView setScrollEnabled:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self resetTooltip];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ( self.tooltipView ) {
if ( gestureRecognizer == self.tapGestureRecognizer || gestureRecognizer == self.longPressGestureRecognizer )
return YES;
else
return NO;
}
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
@end