-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathM6ParallaxController.m
125 lines (81 loc) · 4.16 KB
/
M6ParallaxController.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
//
// M6ParallaxTableViewController.m
// M6ParallaxTableViewController
//
// Created by Peter Paulis on 1.4.2013.
// Copyright (c) 2013 Min60 s.r.o. - http://min60.com. All rights reserved.
//
#import "M6ParallaxController.h"
@interface M6ParallaxController ()
@property (nonatomic, strong, readwrite) UIViewController * topViewController;
@property (nonatomic, strong, readwrite) UITableViewController * tableViewController;
@property (nonatomic, assign, readwrite) CGFloat topViewControllerStandartHeight;
@end
@implementation M6ParallaxController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Public
////////////////////////////////////////////////////////////////////////
- (void)setupWithTopViewController:(UIViewController *)topViewController height:(CGFloat)height tableViewController:(UITableViewController *)tableViewController {
self.topViewControllerStandartHeight = height;
[tableViewController.tableView setBackgroundColor:[UIColor clearColor]];
[tableViewController.tableView setBackgroundView:nil];
[topViewController.view setClipsToBounds:YES];
[tableViewController.view setClipsToBounds:YES];
[self addChildViewController:topViewController];
[self.view addSubview:topViewController.view];
[topViewController didMoveToParentViewController:self];
[self addChildViewController:tableViewController];
[self.view addSubview:tableViewController.view];
[tableViewController didMoveToParentViewController:self];
tableViewController.tableView.frame = self.view.bounds;
topViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.topViewControllerStandartHeight);
tableViewController.tableView.contentInset = UIEdgeInsetsMake(topViewController.view.frame.size.height, 0, 0, 0);
self.topViewController = topViewController;
self.tableViewController = tableViewController;
}
- (void)tableViewControllerDidScroll:(UITableViewController *)tableViewController {
if (tableViewController != self.tableViewController) {
return;
}
UITableView * tableView = self.tableViewController.tableView;
UIView * parallaxView = self.topViewController.view;
float y = tableView.contentOffset.y + self.topViewControllerStandartHeight;
CGRect currentParallaxFrame = parallaxView.frame;
if (y > 0) {
CGFloat newHeight = self.topViewControllerStandartHeight - y;
[parallaxView setHidden:(newHeight <= 0)];
if (!parallaxView.hidden) {
[self willChangeHeightOfTopViewControllerFromHeight:parallaxView.frame.size.height toHeight:newHeight];
parallaxView.frame = CGRectMake(currentParallaxFrame.origin.x, currentParallaxFrame.origin.y, currentParallaxFrame.size.width, newHeight);
}
//uncomment if you want to support section headers - doesnt work 100%
// if (y >= self.topViewControllerStandartHeight) {
//
// tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//
// } else {
//
// tableView.contentInset = UIEdgeInsetsMake(self.topViewControllerStandartHeight - y, 0, 0, 0);
//
// }
} else {
[parallaxView setHidden:NO];
CGFloat newHeight = self.topViewControllerStandartHeight - y;
[self willChangeHeightOfTopViewControllerFromHeight:parallaxView.frame.size.height toHeight:newHeight];
parallaxView.frame = CGRectMake(currentParallaxFrame.origin.x, currentParallaxFrame.origin.y, currentParallaxFrame.size.width, newHeight);
//uncomment if you want to support section headers - doesnt work 100%
// tableView.contentInset = UIEdgeInsetsMake(self.topViewControllerStandartHeight, 0, 0, 0);
}
[tableView setShowsVerticalScrollIndicator:parallaxView.hidden];
}
- (void)willChangeHeightOfTopViewControllerFromHeight:(CGFloat)oldHeight toHeight:(CGFloat)newHeight {
}
@end