Repository navigation
Expand file tree
/
Copy pathSlidingPanelsViewController.m
More file actions
602 lines (452 loc) · 16.8 KB
/
Copy pathSlidingPanelsViewController.m
File metadata and controls
602 lines (452 loc) · 16.8 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//
// SlidingPanelsViewController.m
//
// Created by Suraj Thomas K on 3/30/15.
// 2015 No Heaven Promised
//
#import "SlidingPanelsViewController.h"
float const kHeaderHeight = 48.0f;
@interface CustomButton : UIButton
{
}
@property(nonatomic, readonly) UIImageView *customImage;
@property(nonatomic, readonly) UILabel *customLabel;
@end
@implementation CustomButton
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self != nil)
{
}
return self;
}
- (void)setContentsWithString:(NSString *)string
{
UIImage *image = [UIImage imageNamed:string];
if(image != nil)
{
[_customLabel removeFromSuperview];
_customLabel = nil;
if(_customImage == nil)
{
_customImage = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:_customImage];
}
_customImage.image = image;
}
else
{
[_customImage removeFromSuperview];
_customImage = nil;
if(_customLabel == nil)
{
_customLabel = [[UILabel alloc] initWithFrame:self.bounds];
_customLabel.textColor = [UIColor whiteColor];
_customLabel.textAlignment = NSTextAlignmentCenter;
_customLabel.font = [UIFont fontWithName:@"Helvetica" size:self.bounds.size.height * 0.8f];
[self addSubview:_customLabel];
}
_customLabel.text = string;
}
}
- (void)setIsSelectedButton:(BOOL)isSelected
{
if(_customLabel != nil)
_customLabel.transform = CGAffineTransformScale(CGAffineTransformIdentity, isSelected?1.0f:0.5f, isSelected?1.0f:0.5f);
if(_customImage != nil)
_customImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, isSelected?1.0f:0.5f, isSelected?1.0f:0.5f);
}
@end
@protocol SlidingHeaderDataSource <NSObject>
- (int)numberOfItems;
- (NSString *)imageNameForItemAtIndex:(int)index;
- (void)touchedOnHeaderButtonAtIndex:(int)index;
@end
@interface SlidingHeader : UIView
{
int _numItems;
int _animTargetIndex;
NSMutableArray *_buttons;
}
@property (nonatomic, weak) id<SlidingHeaderDataSource> dataSource;
@property (nonatomic, readwrite) int selectedItemIndex;
@property (nonatomic, readonly) UIImageView *backgroundView;
- (instancetype)initWithFrame:(CGRect)frame andDataSource:(id<SlidingHeaderDataSource>)dataSource;
- (void)layoutSubviewsForNewViewSize:(CGSize)size;
- (void)reloadItems;
@end
@implementation SlidingHeader
- (instancetype)initWithFrame:(CGRect)frame andDataSource:(id<SlidingHeaderDataSource>)dataSource
{
self = [super initWithFrame:frame];
if(self != nil)
{
_dataSource = dataSource;
_selectedItemIndex = 0;
_backgroundView = [[UIImageView alloc] initWithFrame:frame];
_backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_backgroundView];
}
return self;
}
- (void)reloadItems
{
for (UIButton *button in _buttons)
{
[button removeFromSuperview];
}
[_buttons removeAllObjects];
_numItems = [_dataSource numberOfItems];
float headerHeight = self.bounds.size.height;
float distance = (self.bounds.size.width - headerHeight)/2.0f;
_buttons = [NSMutableArray array];
for (int i = 0; i < _numItems; i++)
{
CustomButton *button = [CustomButton buttonWithType:UIButtonTypeCustom];
button.tag = i;
button.frame = CGRectMake((i - (_selectedItemIndex - 1)) * distance, 0.0f, headerHeight, headerHeight);
NSString *imageName = [_dataSource imageNameForItemAtIndex:i + 1];
[button setContentsWithString:imageName];
[button setIsSelectedButton:(i == _selectedItemIndex)];
[self addSubview:button];
[button addTarget:self action:@selector(touchedOnButton:) forControlEvents:UIControlEventTouchUpInside];
[_buttons addObject:button];
}
}
- (void)touchedOnButton:(UIButton *)button
{
[self setSelectedItemIndex:(int)button.tag];
}
- (void)setSelectedItemIndex:(int)selectedItemIndex
{
_selectedItemIndex = selectedItemIndex;
_animTargetIndex = selectedItemIndex;
[_dataSource touchedOnHeaderButtonAtIndex:_animTargetIndex];
int indexDifference = _animTargetIndex - _selectedItemIndex;
[self animateToIndex:(indexDifference == 0)?@(_selectedItemIndex):@(_selectedItemIndex + (indexDifference/abs(indexDifference)))];
}
- (void)animateToIndex:(NSNumber *)index
{
int indexDifference = _animTargetIndex - _selectedItemIndex;
float duration = 0.3f/((indexDifference == 0)?1:abs(indexDifference));
float headerHeight = self.bounds.size.height;
float distance = (self.bounds.size.width - headerHeight)/2.0f;
int itemToSelect = [index intValue];
[UIView animateWithDuration:duration animations:^
{
for (int i = 0; i < [_buttons count]; i++)
{
CustomButton *button = [_buttons objectAtIndex:i];
button.frame = CGRectMake((i - (itemToSelect - 1)) * distance, 0.0f, headerHeight, headerHeight);
[button setIsSelectedButton:(i == _selectedItemIndex)];
}
}
completion:^(BOOL finished)
{
if(itemToSelect == _animTargetIndex)
{
_selectedItemIndex = itemToSelect;
}
else
{
[self animateToIndex:@(itemToSelect+((indexDifference == 0)?1:indexDifference/abs(indexDifference)))];
}
}];
}
- (void)layoutSubviewsForNewViewSize:(CGSize)size
{
float headerHeight = size.height;
float distance = (size.width - headerHeight)/2.0f;
for (int i = 0; i < [_buttons count]; i++)
{
CustomButton *button = [_buttons objectAtIndex:i];
button.frame = CGRectMake((i - (_selectedItemIndex - 1)) * distance, 0.0f, headerHeight, headerHeight);
[button setIsSelectedButton:(i == _selectedItemIndex)];
}
}
@end
#pragma mark -
#pragma mark SlidingPanelsViewController
#pragma mark -
@interface SlidingPanelsViewController () <SlidingHeaderDataSource>
{
int _numberOfPanels;
SlidingHeader *_headerView;
NSMutableDictionary *_viewControllers;
}
@end
@implementation SlidingPanelsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self != nil)
{
[self initializeDefaultParameters];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self initializeDefaultParameters];
}
- (void)initializeDefaultParameters
{
_numberOfPanels = 4;
_viewControllers = [NSMutableDictionary dictionary];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureEvent:)];
swipeLeftGesture.numberOfTouchesRequired = 1;
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftGesture];
UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureEvent:)];
swipeRightGesture.numberOfTouchesRequired = 1;
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightGesture];
}
- (int)selectedViewIndex
{
return _headerView.selectedItemIndex;
}
- (UIViewController *)currentViewController
{
return [_viewControllers objectForKey:@(self.selectedViewIndex)];
}
- (UIViewController *)previousViewController
{
return [_viewControllers objectForKey:@(self.selectedViewIndex-1)];
}
- (UIViewController *)nextViewController
{
return [_viewControllers objectForKey:@(self.selectedViewIndex+1)];
}
- (UIImageView *)headerBackground
{
return _headerView.backgroundView;
}
- (void)selectViewControllerAtIndex:(int)index
{
index = (index < 0)?0:index;
index = (index > [_viewControllers count]-1)?[_viewControllers count]-1:index;
[_headerView setSelectedItemIndex:index];
}
- (void)selectViewController:(UIViewController *)viewController
{
[_viewControllers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if(viewController == obj)
{
[self selectViewControllerAtIndex:[key intValue]];
*stop = NO;
}
}];
}
#pragma mark -
#pragma mark Gestures
- (void)swipeGestureEvent:(UISwipeGestureRecognizer *)recognizer
{
if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
if(self.selectedViewIndex < ([_viewControllers count] - 1))
[_headerView setSelectedItemIndex:self.selectedViewIndex+1];
}
else if(recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
if(self.selectedViewIndex > 0)
[_headerView setSelectedItemIndex:self.selectedViewIndex-1];
}
}
#pragma mark -
#pragma mark ViewControllers
- (void)addViewController:(UIViewController *)viewController
{
if(_headerView == nil)
{
[self addHeader];
}
int index = (int)[_viewControllers count];
CGRect frame = self.view.bounds;
float yPos = _headerView.frame.origin. y + _headerView.frame.size.height;
viewController.view.frame = CGRectMake((index + self.selectedViewIndex) * frame.size.width, yPos, frame.size.width, frame.size.height - yPos);
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
if(_headerView == nil)
[self.view addSubview:viewController.view];
else
[self.view insertSubview:viewController.view belowSubview:_headerView];
[self addChildViewController:viewController];
[_viewControllers setObject:viewController forKey:@(index)];
[self updateHeader];
}
- (void)insertViewController:(UIViewController *)viewController atIndex:(int)index
{
if(index >= [_viewControllers count])
{
[self addViewController:viewController];
}
else
{
CGRect frame = self.view.bounds;
float yPos = _headerView.frame.origin. y + _headerView.frame.size.height;
for (int i = (int)[_viewControllers count] - 1; i >= index ; i--)
{
UIViewController *tempVC = [_viewControllers objectForKey:@(i)];
[_viewControllers setObject:tempVC forKey:@(i+1)];
tempVC.view.frame = CGRectMake((i + self.selectedViewIndex) * frame.size.width, yPos, frame.size.width, frame.size.height - yPos);
}
[_viewControllers setObject:viewController forKey:@(index)];
viewController.view.frame = CGRectMake((index + self.selectedViewIndex) * frame.size.width, yPos, frame.size.width, frame.size.height - yPos);
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
if(_headerView == nil)
[self.view addSubview:viewController.view];
else
[self.view insertSubview:viewController.view belowSubview:_headerView];
[self addChildViewController:viewController];
[self updateHeader];
}
}
- (void)removeViewController:(UIViewController *)viewController
{
__block NSNumber *objectKey = nil;
[_viewControllers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if(viewController == obj)
{
objectKey = (NSNumber *)key;
[self removeViewControllerAtIndex:[objectKey intValue]];
*stop = NO;
}
}];
}
- (void)removeViewControllerAtIndex:(int)index
{
UIViewController *vc = [_viewControllers objectForKey:@(index)];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
[_viewControllers removeObjectForKey:@(index)];
CGRect frame = self.view.bounds;
for (int i = index + 1; i <= [_viewControllers count]; i++)
{
UIViewController *tempVC = [_viewControllers objectForKey:@(i)];
if(tempVC == nil)
{
[_viewControllers removeObjectForKey:@(i-1)];
}
else
{
[_viewControllers setObject:tempVC forKey:@(i-1)];
float yPos = _headerView.frame.origin. y + _headerView.frame.size.height;
tempVC.view.frame = CGRectMake((i - 1 + self.selectedViewIndex) * frame.size.width, yPos, frame.size.width, frame.size.height - yPos);
}
}
[self updateHeader];
}
#pragma mark -
#pragma mark Header
- (void)addHeader
{
CGRect frame = self.view.bounds;
frame.size.height = kHeaderHeight;
_headerView = [[SlidingHeader alloc] initWithFrame:frame andDataSource:self];
_headerView.backgroundColor = [UIColor grayColor];
_headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:_headerView];
}
- (void)updateHeader
{
if(_headerView == nil)
{
[self addHeader];
}
[_headerView reloadItems];
}
- (int)numberOfItems
{
return (int)[_viewControllers count];
}
- (NSString *)imageNameForItemAtIndex:(int)index
{
return [NSString stringWithFormat:@"%d", index];
}
- (void)touchedOnHeaderButtonAtIndex:(int)index
{
int numViews = (int)[_viewControllers count];
CGRect frame = self.view.bounds;
[UIView animateWithDuration:0.3f animations:^{
for (int i = 0; i < numViews; i++)
{
UIViewController *viewController = [_viewControllers objectForKey:@(i)];
float yPos = _headerView.frame.origin. y + _headerView.frame.size.height;
viewController.view.frame = CGRectMake((i - index) * frame.size.width, yPos, frame.size.width, frame.size.height - yPos);
}
}];
UIViewController *vc = [_viewControllers objectForKey:@(index)];
[_delegate didSelectViewController:vc atIndex:index];
}
- (void)hideHeaderView:(BOOL)hide animated:(BOOL)animated
{
if(animated)
{
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = _headerView.frame;
frame.origin.y = hide?-frame.size.height:0.0f;
_headerView.frame = frame;
} completion:^(BOOL finished)
{
}];
for (int i = 0; i < [_viewControllers count]; i++)
{
UIViewController *viewController = [_viewControllers objectForKey:@(i)];
CGRect frame = _headerView.frame;
CGRect viewFrame = viewController.view.frame;
viewFrame.origin.y = frame.origin.y + frame.size.height;
viewFrame.size.height = self.view.bounds.size.height - viewFrame.origin.y;
[UIView animateWithDuration:0.3f animations:^{
viewController.view.frame = viewFrame;
[viewController.view layoutIfNeeded];
}
completion:^(BOOL finished)
{
}];
}
}
else
{
CGRect frame = _headerView.frame;
frame.origin.y = hide?-frame.size.height:0.0f;
_headerView.frame = frame;
for (int i = 0; i < [_viewControllers count]; i++)
{
UIViewController *viewController = [_viewControllers objectForKey:@(i)];
CGRect viewFrame = viewController.view.frame;
viewFrame.origin.y = frame.origin.y + frame.size.height;
viewFrame.size.height = self.view.bounds.size.height - viewFrame.origin.y;
viewController.view.frame = viewFrame;
}
}
}
#pragma mark -
#pragma mark Memory Warning
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
}
#pragma mark -
#pragma mark AutoRotation
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
size.height = kHeaderHeight;
[_headerView layoutSubviewsForNewViewSize:size];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[_headerView layoutSubviewsForNewViewSize:CGSizeMake(self.view.frame.size.height, kHeaderHeight)];
}
@end