-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBFloatingHeaderTableViewController.m
76 lines (52 loc) · 1.86 KB
/
BBFloatingHeaderTableViewController.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
//
// FloatingHeaderViewController.m
// FloatingTableViewControllerExample
//
// Created by Beat Besmer on 14.07.12.
//
#import "BBFloatingHeaderTableViewController.h"
@interface BBFloatingHeaderTableViewController ()
@property (readonly) NSArray *headerCells;
@end
@implementation BBFloatingHeaderTableViewController
#pragma mark - misc
- (NSArray *)headerCells{
NSMutableArray *headerCells = [NSMutableArray array];
for (UIView *cell in self.view.subviews) {
if ([cell isKindOfClass:[BBFloatingHeaderCell class]]){
[headerCells addObject:cell];
}
}
// Sort by y-position
[headerCells sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
int y1 = ((UIView *)obj1).frame.origin.y;
int y2 = ((UIView *)obj2).frame.origin.y;
return y1 > y2;
}];
return headerCells;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSArray *headers = self.headerCells;
if(headers.count < 2){
return;
}
BBFloatingHeaderCell *firstHeader = [headers objectAtIndex:0];
CGRect firstRect = firstHeader.frame;
CGRect secondRect = ((UIView *)[headers objectAtIndex:1]).frame;
UIView *headerContent = firstHeader.contentView;
int headerHeight = ((UIView *)[[headerContent subviews] objectAtIndex:0]).frame.size.height;
// Tail of first - head of second
int overlap = (firstRect.origin.y + headerHeight) - secondRect.origin.y;
if (overlap >= 0){
// Move the first one up
CGRect newRect = headerContent.frame;
newRect.origin.y = -overlap;
headerContent.frame = newRect;
}
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 1;
}
@end