-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNavigationContentSlider.m
More file actions
256 lines (190 loc) · 9.48 KB
/
NavigationContentSlider.m
File metadata and controls
256 lines (190 loc) · 9.48 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// NavigationSlideViewController.m
//
// Created by Ed Rackham on 14/11/2012.
// Copyright (c) 2012 edrackham.com. All rights reserved.
//
#import "NavigationContentSlider.h"
// Mini subclas of UIView allows for UIScrollView events to occur
// outside of it's frame (useful for paging when the width is
// smaller than the required overall width)
@interface TitleBarView : UIView
@property (strong, nonatomic) UIScrollView *scrollView;
@end
@implementation TitleBarView
@synthesize scrollView;
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *child = nil;
if ((child = [super hitTest:point withEvent:event]) == self) return self.scrollView;
return child;
}
@end
@interface NavigationContentSlider ()
@property (assign, nonatomic) NSInteger totalSections;
@property (strong, nonatomic) NSMutableArray *sectionViews;
@property (assign, nonatomic) NSInteger sectionTitleWidth;
@property (strong, nonatomic) NSDictionary *sectionTitleTextAttributes;
@property (strong, nonatomic) UIScrollView *titleBarScrollView;
@property (strong, nonatomic) TitleBarView *titleBarView;
@property (strong, nonatomic) UIScrollView *contentScrollView;
@property (assign, nonatomic) CGFloat contentXOffsetScale;
- (UILabel *)titleLabelForSectionTitleAtIndex:(NSInteger)index;
@end
@implementation NavigationContentSlider
#pragma mark - Standard Init
- (void)initialise{
if(self.navigationController == nil)
[NSException raise:@"NavigationContentSlider" format:@"NavigationSlideViewController can only be used on view controllers that are part of a UINavigationController stack."];
}
- (id)initWithCoder:(NSCoder *)aCoder{
self = [super initWithCoder:aCoder];
if(self){
[self initialise];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self initialise];
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
[self.navigationController setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated{
if(!_manualInit) [self initNavigationContentSlider];
if(_isInitalised) [self scrollViewDidScroll:_titleBarScrollView];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
#pragma mark - Private Methods
- (UILabel *)titleLabelForSectionTitleAtIndex:(NSInteger)index{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(_sectionTitleWidth * index, 0, _sectionTitleWidth, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
[label setText:[_dataSource navigationContentSlider:self titleForSectionAtIndex:index]];
if(_sectionTitleTextAttributes == nil){
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:20.0]];
[label setShadowColor:[UIColor colorWithWhite:0.0 alpha:0.5]];
}else{
[label setTextColor:[_sectionTitleTextAttributes objectForKey:UITextAttributeTextColor]];
[label setFont:[_sectionTitleTextAttributes objectForKey:UITextAttributeFont]];
[label setShadowColor:[_sectionTitleTextAttributes objectForKey:UITextAttributeTextShadowColor]];
[label setShadowOffset:[[_sectionTitleTextAttributes objectForKey:UITextAttributeTextShadowOffset] CGSizeValue]];
}
return label;
}
#pragma mark - Public Methods
- (void)initNavigationContentSlider{
if(_isInitalised){
[self scrollViewDidScroll:_titleBarScrollView];
}
if(_isInitalised) return;
// Prepare iVars
_totalSections = [_dataSource numberOfSectionsInNavigationContentSlider:self];
_sectionTitleWidth = [self.dataSource widthOfSectionTitlesInNavigationContentSlider:self];
_sectionViews = [[NSMutableArray alloc] initWithCapacity:_totalSections];
CGSize viewSize = [self maximumUsableFrame].size;
int padding = floor((viewSize.width - _sectionTitleWidth) / 2) - 5;
int navControllerHeight = self.navigationController.navigationBar.frame.size.height;
_contentXOffsetScale = viewSize.width / _sectionTitleWidth;
// Use title text attributes if set (and available)
// If it's available, try to grab from local titleTextAttributes (for the local nav bar)
// If that's nil, try the global titleTextAttributes from UIAppearance.
if(NSProtocolFromString(@"UIAppearance")){
if(self.navigationController.navigationBar.titleTextAttributes != nil){
_sectionTitleTextAttributes = self.navigationController.navigationBar.titleTextAttributes;
}else if([[UINavigationBar appearance] titleTextAttributes] != nil){
_sectionTitleTextAttributes = [[UINavigationBar appearance] titleTextAttributes];
}
}
// Validate
if(_totalSections < 1)
[NSException raise:@"NavigationContentSlider" format:@"Number of sections must be more than zero"];
// Construct both the title and content UIScrollViews
// Title bar scroll view
_titleBarScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(padding, 0, _sectionTitleWidth, navControllerHeight)];
[_titleBarScrollView setDelegate:self];
[_titleBarScrollView setPagingEnabled:YES];
[_titleBarScrollView setClipsToBounds:NO];
[_titleBarScrollView setShowsHorizontalScrollIndicator:NO];
// TitleBarView
_titleBarView = [[TitleBarView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, navControllerHeight)];
[_titleBarView setScrollView:_titleBarScrollView];
[_titleBarView addSubview:_titleBarScrollView];
// Content scroll view
_contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height)];
[_contentScrollView setPagingEnabled:YES];
[_contentScrollView setShowsHorizontalScrollIndicator:NO];
[_contentScrollView setScrollEnabled:NO];
int i;
for(i=0; i<_totalSections; i++){
// Add title
[_titleBarScrollView addSubview:[self titleLabelForSectionTitleAtIndex:i]];
// Add view
UIView *sectionView = [self.dataSource navigationContentSlider:self viewForSectionAtIndex:i];
CGRect frame = sectionView.frame;
[sectionView setFrame:CGRectMake((viewSize.width * i) + frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
[_sectionViews addObject:sectionView];
[_contentScrollView addSubview:sectionView];
}
[_titleBarScrollView setContentSize:CGSizeMake(i*_sectionTitleWidth, navControllerHeight)];
[_contentScrollView setContentSize:CGSizeMake(i*viewSize.width, viewSize.height)];
[_titleBarScrollView setContentOffset:CGPointMake(0, 0)];
[_contentScrollView setContentOffset:CGPointMake(0, 0)];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[_titleBarView addGestureRecognizer:tapGesture];
[self.navigationItem setTitleView:_titleBarView];
[self.view addSubview:_contentScrollView];
_isInitalised = YES;
}
- (void)handleTapGesture:(UITapGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized) {
CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view];
float startPos = (self.view.frame.size.width / 2) - (_sectionTitleWidth / 2); // indent from left
float contentOffset = _titleBarScrollView.contentOffset.x; // content offset
float actualXPos = location.x - startPos + contentOffset;
int tabIndex = (actualXPos / _sectionTitleWidth);
[self setCurrentSlideIndex:tabIndex animated:YES];
}
}
- (CGRect)maximumUsableFrame{
CGRect maxFrame = [UIScreen mainScreen].applicationFrame;
if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight){
NSInteger width, height;
width = maxFrame.size.height;
height = maxFrame.size.width;
maxFrame.size.height = height;
maxFrame.size.width = width;
}
if(self.navigationController)
maxFrame.size.height -= self.navigationController.navigationBar.frame.size.height;
if(self.tabBarController && !self.tabBarController.tabBar.hidden)
maxFrame.size.height -= self.tabBarController.tabBar.frame.size.height;
return maxFrame;
}
// Useful for replacing the content (for example, refreshing the data that controls
// the view could mean there's more or less section titles). Call this method to remove
// all the subviews, allowing you to perform a manual init on it.
- (void)clearNavigationContentSlider{
_isInitalised = NO;
[_titleBarView removeFromSuperview];
[_contentScrollView removeFromSuperview];
}
- (void)setCurrentSlideIndex:(NSInteger)currentSlideIndex animated:(BOOL)animated{
NSInteger xOffset = currentSlideIndex * _sectionTitleWidth;
[_titleBarScrollView setContentOffset:CGPointMake(xOffset, 0) animated:animated];
[_contentScrollView setContentOffset:CGPointMake(xOffset * _contentXOffsetScale, 0) animated:animated];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
int xOffset = scrollView.contentOffset.x;
[_contentScrollView setContentOffset:CGPointMake(xOffset * _contentXOffsetScale, 0)];
}
@end