-
Notifications
You must be signed in to change notification settings - Fork 101
/
LIExposeController.m
1036 lines (859 loc) · 40.8 KB
/
LIExposeController.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
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2012 LinkedIn, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "LIExposeController.h"
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
NSString * const DELETE_BUTTON_IMAGE = @"deleteBtn.png";
/**
Methods are named this way to avoid potential conflicts with anyone else
who may have created a category of UIView
*/
@interface UIView (Expose_Additions)
@property(nonatomic) CGFloat exposeLeft;
@property(nonatomic) CGFloat exposeTop;
@property(nonatomic,readonly) CGFloat exposeRight;
@property(nonatomic,readonly) CGFloat exposeBottom;
@property(nonatomic) CGFloat exposeWidth;
@property(nonatomic) CGFloat exposeHeight;
@end
@interface UIViewController (LIExposeController_Private)
@property (nonatomic, retain) LIExposeController *exposeController;
@end
@interface LIExposeController ()
/**
Subviews
*/
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UIView *footerView;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIView *addChildButton;
@property (nonatomic, retain) UIView *backgroundView;
// Element is a button/NSNull used to delete its respective child view controller
@property (nonatomic, retain) NSMutableArray *deleteButtons;
// Element is a view containing overlay, label and has a tap gesture
@property (nonatomic, retain) NSMutableArray *containerViews;
/**
Current State
*/
@property (nonatomic, assign) BOOL isZoomedOut;
@property (nonatomic, assign) NSUInteger selectedIndex;
@property (nonatomic, assign) UIViewController *selectedViewController;
@property (nonatomic, readonly) UIViewController *selectedContentViewController;
@property (nonatomic, readonly) NSUInteger numPerPage;
@property (nonatomic, assign) NSUInteger currentPage;
/**
Setting Up View Hierarchy
*/
- (void)setupContentView;
- (void)setupBackgroundView;
- (void)setupViewControllers;
/**
Layout
*/
- (void)layoutGrid:(BOOL)animated completion:(void (^)(BOOL finished))completion;
- (void)calculateContentSize;
- (CGPoint)centerWithIndex:(NSUInteger)index;
- (void)setPage;
- (void)setExposeZoomedOut:(BOOL)zoomedOut animated:(BOOL)animated;
/**
Helpers
*/
- (BOOL)isPad;
- (void)newViewController:(UIViewController *)viewController index:(NSUInteger)index;
+ (UIViewController *)getContentViewControllerFromContainer:(UIViewController *)containerViewController;
- (void)bounceView:(UIView *)view;
/**
Gesture Actions
*/
- (void)toggleGestureRecognizer:(BOOL)toggle forView:(UIView *)view;
- (void)selectView:(UITapGestureRecognizer *)gestureRecognizer;
- (void)selectViewController:(UIViewController *)viewController;
- (void)selectViewControllerAtIndex:(NSUInteger)index;
- (void)addViewController:(UITapGestureRecognizer *)gestureRecognizer;
- (void)deleteViewController:(id)sender;
- (void)keepButtonHighlighted:(UIButton *)button;
/**
Zooming Callbacks
*/
- (void)exposeZoomedOut:(BOOL)animated;
- (void)exposeZoomedIn:(BOOL)animated;
@end
@implementation LIExposeController
@synthesize viewControllers=_viewControllers;
@synthesize selectedIndex=_selectedIndex;
@synthesize exposeDelegate=_exposeDelegate;
@synthesize exposeDataSource=_exposeDataSource;
@synthesize isZoomedOut=_isZoomedOut;
@synthesize numRows=_numRows;
@synthesize numCols=_numCols;
@synthesize cornerRadius=_cornerRadius;
@synthesize scaleFactor=_scaleFactor;
@synthesize showsTouchDown=_showsTouchDown;
@synthesize animationDuration=_animationDuration;
@synthesize headerView=_headerView;
@synthesize footerView=_footerView;
@synthesize scrollView=_scrollView;
@synthesize pageControl=_pageControl;
@synthesize addChildButton=_addChildButton;
@synthesize backgroundView=_backgroundView;
@synthesize deleteButtons=_deleteButtons;
@synthesize selectedViewController=_selectedViewController;
@synthesize selectedContentViewController=_selectedContentViewController;
@synthesize rowOffset=_rowOffset;
@synthesize numPerPage=_numPerPage;
@synthesize currentPage=_currentPage;
@synthesize containerViews=_containerViews;
#pragma mark - Initialization/Deallocation Methods
- (id)init {
self = [super init];
if (self) {
_deleteButtons = [[NSMutableArray array] retain];
_containerViews = [[NSMutableArray array] retain];
_viewControllers = nil;
_selectedViewController = nil;
_selectedContentViewController = nil;
_selectedIndex = 0;
_exposeDelegate = nil;
_exposeDataSource = nil;
_isZoomedOut = NO;
_currentPage = 0;
_cornerRadius = 20.0;
_animationDuration = 0.3;
if ([self isPad]) {
_numRows = 3;
_numCols = 3;
_scaleFactor = 0.28;
_rowOffset = -10.0;
} else {
_numRows = 2;
_numCols = 2;
_scaleFactor = 0.36;
_rowOffset = -10.0;
}
_numPerPage = _numRows * _numCols;
}
return self;
}
- (void)dealloc {
self.addChildButton = nil;
self.headerView = nil;
self.footerView = nil;
self.scrollView = nil;
self.pageControl = nil;
self.backgroundView = nil;
self.viewControllers = nil;
self.deleteButtons = nil;
self.containerViews = nil;
[super dealloc];
}
#pragma mark - Setters
- (void)setViewControllers:(NSMutableArray *)controllers {
// Clear Arrays
NSUInteger index = 0;
for (UIViewController *viewController in _viewControllers) {
viewController.exposeController = nil;
[viewController.view removeFromSuperview];
[(UIView *)[self.containerViews objectAtIndex:index] removeFromSuperview];
id deleteButton = [self.deleteButtons objectAtIndex:index];
if (deleteButton != [NSNull null]) {
[(UIView *)deleteButton removeFromSuperview];
}
index++;
}
[_viewControllers autorelease];
[self.deleteButtons removeAllObjects];
[self.containerViews removeAllObjects];
_viewControllers = [controllers retain];
self.selectedViewController = nil;
self.selectedIndex = 0;
if (_viewControllers.count > self.selectedIndex) {
self.selectedViewController = [_viewControllers objectAtIndex:self.selectedIndex];
}
if ([self isViewLoaded]) {
[self setupViewControllers];
if (self.isZoomedOut) {
[self layoutGrid:NO completion:nil];
}
[self setExposeZoomedOut:self.isZoomedOut animated:NO];
}
}
- (void)setNumRows:(NSUInteger)rows {
[self setNumRows:rows animated:NO];
}
- (void)setNumCols:(NSUInteger)cols {
[self setNumCols:cols animated:NO];
}
- (void)setNumRows:(NSUInteger)rows animated:(BOOL)animated {
_numRows = rows;
_numPerPage = _numRows * _numCols;
if (self.isZoomedOut) {
[self layoutGrid:animated completion:nil];
}
}
- (void)setNumCols:(NSUInteger)cols animated:(BOOL)animated {
_numCols = cols;
_numPerPage = _numRows * _numCols;
if (self.isZoomedOut) {
[self layoutGrid:animated completion:nil];
}
}
- (void)setCornerRadius:(CGFloat)radius {
_cornerRadius = radius;
if ([self isViewLoaded]) {
if (self.isZoomedOut) {
for (UIViewController *viewController in self.viewControllers) {
viewController.view.layer.cornerRadius = _cornerRadius;
}
}
for (UIView *containerView in self.containerViews) {
if (containerView.subviews.count) {
((UIView *)[containerView.subviews objectAtIndex:0]).layer.cornerRadius = _cornerRadius * self.scaleFactor;
}
}
}
}
- (void)setScaleFactor:(CGFloat)scale {
_scaleFactor = scale;
if ([self isViewLoaded] && self.isZoomedOut) {
self.addChildButton.transform = CGAffineTransformMakeScale(_scaleFactor, _scaleFactor);
for (UIView *containerView in self.containerViews) {
if (containerView.subviews.count) {
((UIView *)[containerView.subviews objectAtIndex:0]).layer.cornerRadius = _cornerRadius * self.scaleFactor;
}
}
[self layoutGrid:NO completion:nil];
}
}
- (void)setRowOffset:(CGFloat)offset {
_rowOffset = offset;
if ([self isViewLoaded] && self.isZoomedOut) {
[self layoutGrid:NO completion:nil];
}
}
#pragma mark - Getters
- (UIViewController *)selectedContentViewController {
return [[self class] getContentViewControllerFromContainer:self.selectedViewController];
}
- (UIView *)addChildButton {
if (!_addChildButton) {
if ([self.exposeDelegate respondsToSelector:@selector(canAddViewControllersForExposeController:)]) {
if ([self.exposeDelegate canAddViewControllersForExposeController:self]) {
if ([self.exposeDataSource respondsToSelector:@selector(addViewForExposeController:)]) {
_addChildButton = [[self.exposeDataSource addViewForExposeController:self] retain];
_addChildButton.transform = CGAffineTransformMakeScale(self.scaleFactor, self.scaleFactor);
_addChildButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
_addChildButton.userInteractionEnabled = YES;
NSUInteger addIndex = self.viewControllers.count;
_addChildButton.center = [self centerWithIndex:addIndex];
// Add gesture
UITapGestureRecognizer *addGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addViewController:)] autorelease];
addGesture.numberOfTapsRequired = 1;
[_addChildButton addGestureRecognizer:addGesture];
}
}
}
}
return _addChildButton;
}
#pragma mark - View Hierarchy
- (void)loadView {
[super loadView];
self.view.autoresizingMask = ~UIViewAutoresizingNone;
[self setupContentView];
[self setupViewControllers];
[self setupBackgroundView];
[self layoutGrid:NO completion:nil];
[self setExposeZoomedOut:NO animated:NO];
}
#pragma mark - View Setup
- (void)setupContentView {
// Header View
if ([self.exposeDataSource respondsToSelector:@selector(headerViewForExposeController:)]) {
self.headerView = [self.exposeDataSource headerViewForExposeController:self];
self.headerView.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:self.headerView];
}
// Footer View
if ([self.exposeDataSource respondsToSelector:@selector(footerViewForExposeController:)]) {
self.footerView = [self.exposeDataSource footerViewForExposeController:self];
self.footerView.exposeTop = self.view.exposeHeight - self.footerView.exposeHeight;
self.footerView.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:self.footerView];
}
// Scroll View
self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, self.headerView.exposeHeight, self.view.exposeWidth, self.view.exposeHeight - self.headerView.exposeHeight - self.footerView.exposeHeight)] autorelease];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|
UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
if (self.headerView.autoresizingMask & UIViewAutoresizingFlexibleHeight) {
self.scrollView.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin;
}
if (self.footerView.autoresizingMask & UIViewAutoresizingFlexibleHeight) {
self.scrollView.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin;
}
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.scrollView];
//Add page control
self.pageControl = [[[UIPageControl alloc] init] autorelease];
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|
UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleTopMargin;
if (self.footerView.autoresizingMask & UIViewAutoresizingFlexibleHeight) {
self.pageControl.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin;
}
self.pageControl.hidesForSinglePage = YES;
self.pageControl.center = CGPointMake(floorf(self.view.exposeWidth / 2),
self.scrollView.exposeBottom - 10);
self.pageControl.alpha = 0.0;
[self.view addSubview:self.pageControl];
[self.scrollView addSubview:self.addChildButton];
}
- (void)setupBackgroundView {
if ([self.exposeDataSource respondsToSelector:@selector(backgroundViewForExposeController:)]) {
[self.backgroundView removeFromSuperview];
self.backgroundView = [self.exposeDataSource backgroundViewForExposeController:self];
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:self.backgroundView atIndex:0];
}
}
- (void)setupViewControllers {
NSUInteger i = 0;
for (UIViewController *viewController in self.viewControllers) {
[self newViewController:viewController index:i];
i++;
}
[self calculateContentSize];
}
- (CGPoint)centerWithIndex:(NSUInteger)index {
CGFloat page = index / self.numPerPage;
CGFloat col = (index % self.numCols);
CGFloat row = (index % self.numPerPage) / self.numCols;
CGFloat left = 0;
CGFloat top = 0;
CGPoint c = CGPointZero;
CGFloat width = floorf(self.scrollView.exposeWidth * self.scaleFactor);
CGFloat height = floorf((self.scrollView.exposeHeight - self.pageControl.exposeHeight) * self.scaleFactor);
CGFloat colSpacing = floorf((self.scrollView.exposeWidth - (self.numCols * width)) / (self.numCols + 1));
CGFloat rowSpacing = floorf((self.scrollView.exposeHeight - (self.numRows * height)) / (self.numRows + 1));
left = colSpacing + (col * colSpacing) + (col * width) + (page * self.scrollView.exposeWidth);
top = rowSpacing + (row * rowSpacing) + (row * height) + self.rowOffset;
c = CGPointMake(left + floorf(width / 2), top + floorf(height / 2));
return c;
}
- (void)newViewController:(UIViewController *)viewController index:(NSUInteger)index {
viewController.exposeController = self;
// Real View
viewController.view.frame = self.scrollView.bounds;
viewController.view.autoresizingMask = ~UIViewAutoresizingNone;
// Apply Transforms to real view
CGPoint c = [self centerWithIndex:index];
CGFloat tx = c.x - viewController.view.center.x;
CGFloat ty = c.y - viewController.view.center.y;
CGAffineTransform t = CGAffineTransformMakeTranslation(tx, ty);
t = CGAffineTransformScale(t, self.scaleFactor, self.scaleFactor);
viewController.view.transform = t;
[self.scrollView addSubview:viewController.view];
// Add container view
UIView *containerView = [[[UIView alloc] initWithFrame:viewController.view.bounds] autorelease];
containerView.autoresizingMask = viewController.view.autoresizingMask;
containerView.clipsToBounds = NO;
containerView.frame = viewController.view.frame;
containerView.alpha = self.editing ? 1.0 : 0.0;
[self.containerViews addObject:containerView];
[self.scrollView addSubview:containerView];
// Tap to select gesture
UITapGestureRecognizer *selectGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectView:)] autorelease];
selectGesture.enabled = NO;
selectGesture.delegate = self;
[containerView addGestureRecognizer:selectGesture];
// Optional Overlay View
if ([self.exposeDataSource respondsToSelector:@selector(exposeController:overlayViewForViewController:)]) {
UIView *overlayView = [self.exposeDataSource exposeController:self overlayViewForViewController:viewController];
overlayView.autoresizingMask = containerView.autoresizingMask;
overlayView.layer.cornerRadius = self.cornerRadius * self.scaleFactor;
overlayView.frame = containerView.bounds;
[containerView addSubview:overlayView];
}
// Optional Label
if ([self.exposeDataSource respondsToSelector:@selector(exposeController:labelForViewController:)]) {
UILabel *label = [self.exposeDataSource exposeController:self labelForViewController:viewController];
label.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|
UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleHeight;
label.frame = CGRectMake((containerView.exposeWidth - label.exposeWidth) / 2, containerView.exposeHeight + label.exposeTop, label.exposeWidth, label.exposeHeight);
[containerView addSubview:label];
}
// Add delete buttons (optional)
if ([self.exposeDelegate respondsToSelector:@selector(exposeController:canDeleteViewController:)] &&
[self.exposeDelegate exposeController:self canDeleteViewController:viewController]) {
UIImage *deleteImage = [UIImage imageNamed:DELETE_BUTTON_IMAGE];
UIButton *deleteBtn = [[[UIButton alloc] initWithFrame:CGRectMake(containerView.exposeLeft - floorf(deleteImage.size.width / 2),
containerView.exposeTop - floorf(deleteImage.size.height / 2),
deleteImage.size.width,
deleteImage.size.height)] autorelease];
[deleteBtn setImage:deleteImage forState:UIControlStateNormal];
[deleteBtn addTarget:self action:@selector(deleteViewController:) forControlEvents:UIControlEventTouchUpInside];
deleteBtn.hidden = !self.editing;
deleteBtn.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|
UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin;
[self.scrollView addSubview:deleteBtn];
[self.deleteButtons addObject:deleteBtn];
}
else {
[self.deleteButtons addObject:[NSNull null]];
}
}
#pragma mark - Select
- (void)selectView:(UITapGestureRecognizer *)gestureRecognizer {
NSUInteger index = [self.containerViews indexOfObject:gestureRecognizer.view];
[self selectViewControllerAtIndex:index];
}
- (void)selectViewController:(UIViewController *)viewController {
NSUInteger index = [self.viewControllers indexOfObject:viewController];
[self selectViewControllerAtIndex:index];
}
- (void)selectViewControllerAtIndex:(NSUInteger)index {
UIViewController *oldViewController = self.selectedViewController;
self.selectedIndex = index;
self.selectedViewController = [self.viewControllers objectAtIndex:self.selectedIndex];
// Will Switch Callback
if ([self.exposeDelegate respondsToSelector:@selector(exposeController:willSwitchFromViewController:toViewController:)]) {
[self.exposeDelegate exposeController:self
willSwitchFromViewController:oldViewController
toViewController:self.selectedViewController];
}
[self toggleExpose:YES]; // This will collapse spaces
}
#pragma mark - Add a new view controller
- (void)addViewController:(id)sender {
if ([self.exposeDelegate respondsToSelector:@selector(shouldAddViewControllerForExposeController:)]) {
[self.exposeDelegate shouldAddViewControllerForExposeController:self];
}
}
- (void)addNewViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self.viewControllers addObject:viewController];
NSUInteger i = self.viewControllers.count - 1;
[self newViewController:viewController index:i];
[self toggleGestureRecognizer:YES forView:self.containerViews.lastObject];
[self layoutGrid:animated completion:nil];
}
#pragma mark - Delete a view controller
- (void)keepButtonHighlighted:(UIButton *)button {
button.highlighted = YES;
button.selected = YES;
}
- (void)deleteViewController:(id)sender {
if (![sender isKindOfClass:[UIButton class]]) {
return;
}
UIButton *button = (UIButton *)sender;
[self performSelector:@selector(keepButtonHighlighted:) withObject:button afterDelay:0];
NSUInteger deleteIndex = [self.deleteButtons indexOfObject:button];
UIViewController *viewController = [self.viewControllers objectAtIndex:deleteIndex];
UIView *containerView = [self.containerViews objectAtIndex:deleteIndex];
[UIView animateWithDuration:self.animationDuration
animations:^{
viewController.view.alpha = 0.0;
containerView.alpha = 0.0;
button.alpha = 0.0;
}
completion:^(BOOL finished) {
if ([self.exposeDelegate respondsToSelector:@selector(exposeController:didDeleteViewController:atIndex:)]) {
[self.exposeDelegate exposeController:self didDeleteViewController:viewController atIndex:deleteIndex];
}
[viewController.view removeFromSuperview];
[containerView removeFromSuperview];
[button removeFromSuperview];
viewController.exposeController = nil;
[self.viewControllers removeObject:viewController];
[self.deleteButtons removeObject:button];
[self.containerViews removeObject:containerView];
if ([viewController isEqual:self.selectedViewController]) {
self.selectedViewController = nil;
self.selectedIndex = 0;
if (self.viewControllers.count > self.selectedIndex) {
self.selectedViewController = [self.viewControllers objectAtIndex:self.selectedIndex];
}
}
[self layoutGrid:YES completion:nil];
}];
}
#pragma mark - Expose
- (void)toggleExpose {
[self toggleExpose:YES];
}
- (void)toggleExpose:(BOOL)animated {
[self setExposeZoomedOut:!self.isZoomedOut animated:animated];
}
- (void)setExposeZoomedOut:(BOOL)zoomedOut animated:(BOOL)animated {
CGFloat animateDuration = animated ? self.animationDuration : 0.0;
self.isZoomedOut = zoomedOut;
if (self.isZoomedOut) {
// Will Shrink Callback
if ([self.selectedContentViewController conformsToProtocol:@protocol(LIExposeControllerChildViewControllerDelegate)] &&
[self.selectedContentViewController respondsToSelector:@selector(viewWillShrinkInExposeController:animated:)]) {
[(id<LIExposeControllerChildViewControllerDelegate>)self.selectedContentViewController viewWillShrinkInExposeController:self animated:animated];
}
self.pageControl.alpha = 1.0;
// Will Zoom Out Callback
if ([self.exposeDelegate respondsToSelector:@selector(exposeControllerWillZoomOut:animated:)]) {
[self.exposeDelegate exposeControllerWillZoomOut:self animated:animated];
}
// Will Appear Callback
for (NSUInteger i = 0; i < self.viewControllers.count; i++) {
if (i != self.selectedIndex) {
UIViewController *vc = [self.viewControllers objectAtIndex:i];
[vc viewWillAppear:animated];
[self.scrollView sendSubviewToBack:vc.view];
}
}
// Perform Frame Adjustment
[self layoutGrid:animated completion:^(BOOL finished) {
[self exposeZoomedOut:animated];
}];
} else {
// Will Expand Callback
if ([self.selectedContentViewController conformsToProtocol:@protocol(LIExposeControllerChildViewControllerDelegate)] &&
[self.selectedContentViewController respondsToSelector:@selector(viewWillExpandInExposeController:animated:)]) {
[(id<LIExposeControllerChildViewControllerDelegate>)self.selectedContentViewController viewWillExpandInExposeController:self animated:animated];
}
self.pageControl.alpha = 0;
// Bring the selected view to the top of the stack
[self.scrollView bringSubviewToFront:self.selectedViewController.view];
// Will Zoom In Callback
if ([self.exposeDelegate respondsToSelector:@selector(exposeControllerWillZoomIn:animated:)]) {
[self.exposeDelegate exposeControllerWillZoomIn:self animated:animated];
}
// Perform Frame Adjustment
int i = 0;
for (UIViewController *viewController in self.viewControllers) {
UIView *containerView = [self.containerViews objectAtIndex:i];
UIButton *deleteButton = nil;
if (self.deleteButtons.count > 0) {
if ([self.deleteButtons objectAtIndex:i] != [NSNull null]) {
deleteButton = [self.deleteButtons objectAtIndex:i];
}
}
// Will Disappear Callback
if (![viewController isEqual:self.selectedViewController]) {
[viewController viewWillDisappear:animated];
}
// Animate (optional)
[UIView animateWithDuration:animateDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
containerView.alpha = 0.0;
deleteButton.alpha = 0.0;
if ([viewController isEqual:self.selectedViewController]) {
viewController.view.transform = CGAffineTransformIdentity;
viewController.view.layer.cornerRadius = 0;
viewController.view.frame = CGRectMake(self.pageControl.currentPage * self.scrollView.exposeWidth, 0, self.scrollView.exposeWidth, self.scrollView.exposeHeight);
} else {
viewController.view.alpha = 0.0;
}
}
completion:^(BOOL finished) {
self.scrollView.scrollEnabled = NO;
if ([viewController isEqual:self.selectedViewController]) {
[self exposeZoomedIn:animated];
} else {
[viewController viewDidDisappear:animated];
}
}];
i++;
}
// Did Select Callback
if ([self.exposeDelegate respondsToSelector:@selector(exposeController:didSelectViewController:)]) {
[self.exposeDelegate exposeController:self didSelectViewController:self.selectedViewController];
}
}
}
- (void)calculateContentSize {
NSInteger numPages = (NSInteger)ceilf((CGFloat)self.viewControllers.count / self.numPerPage);
if ([self.exposeDelegate respondsToSelector:@selector(canAddViewControllersForExposeController:)]) {
if ([self.exposeDelegate canAddViewControllersForExposeController:self]) {
numPages = (NSInteger)ceilf((CGFloat)(self.viewControllers.count + 1) / self.numPerPage);
}
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.exposeWidth * numPages, self.scrollView.exposeHeight);
self.pageControl.numberOfPages = numPages;
}
- (void)layoutGrid:(BOOL)animated completion:(void (^)(BOOL finished))completion {
CGFloat animateDuration = animated ? self.animationDuration : 0.0;
// This is the currently selected view
NSUInteger i = 0;
for (UIViewController *viewController in self.viewControllers) {
// Real View
[viewController.view endEditing:YES];
// Container View
UIView *containerView = [self.containerViews objectAtIndex:i];
[self.scrollView bringSubviewToFront:containerView];
// Delete Button
UIButton *deleteButton = nil;
if (self.deleteButtons.count > 0) {
if ([self.deleteButtons objectAtIndex:i] != [NSNull null]) {
deleteButton = [self.deleteButtons objectAtIndex:i];
[self.scrollView bringSubviewToFront:deleteButton];
}
}
// Layout the grid
CGPoint c = [self centerWithIndex:i];
CGFloat tx = c.x - floorf(viewController.view.center.x);
CGFloat ty = c.y - floorf(viewController.view.center.y);
// Animate (optional)
[UIView animateWithDuration:animateDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
// Transforms
CGAffineTransform t = CGAffineTransformMakeTranslation(tx, ty);
t = CGAffineTransformScale(t, self.scaleFactor, self.scaleFactor);
viewController.view.transform = t;
viewController.view.layer.masksToBounds = YES;
viewController.view.layer.cornerRadius = self.cornerRadius;
viewController.view.alpha = 1.0;
containerView.alpha = 1.0;
containerView.frame = viewController.view.frame;
if (deleteButton) {
deleteButton.alpha = 1.0;
deleteButton.center = containerView.frame.origin;
}
}
completion:^(BOOL finished) {
self.scrollView.scrollEnabled = YES;
if ([viewController isEqual:self.selectedViewController]) {
if (completion) {
completion(finished);
}
}
}];
i++;
}
// Show Add View
[self.scrollView bringSubviewToFront:self.addChildButton];
// Adjust addChildButton position and frame
NSUInteger addIndex = self.viewControllers.count;
CGPoint c = [self centerWithIndex:addIndex];
[UIView animateWithDuration:animateDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.addChildButton.center = c;
}
completion:nil];
// Recalculate content size
[self calculateContentSize];
}
#pragma mark - Editing
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
for (id deleteButton in self.deleteButtons) {
if ([deleteButton isKindOfClass:[UIButton class]]) {
((UIButton *)deleteButton).hidden = !editing;
}
}
}
#pragma mark - Gesture Recognizers
- (void)toggleGestureRecognizer:(BOOL)toggle forView:(UIView *)view {
((UIGestureRecognizer *)[view.gestureRecognizers objectAtIndex:0]).enabled = toggle;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if(self.showsTouchDown && self.isZoomedOut && ![gestureRecognizer.view isKindOfClass:[UIImageView class]]) {
NSUInteger index = [self.containerViews indexOfObject:gestureRecognizer.view];
[self bounceView:((UIViewController *)[self.viewControllers objectAtIndex:index]).view];
}
if ([touch.view isKindOfClass:[UIButton class]]) {
return NO;
}
else {
return YES;
}
}
#pragma mark - Animations
- (void)exposeZoomedOut:(BOOL)animated {
for (UIView *view in self.containerViews) {
[self toggleGestureRecognizer:YES forView:view];
}
[self bounceView:self.selectedViewController.view];
if ([self.selectedContentViewController conformsToProtocol:@protocol(LIExposeControllerChildViewControllerDelegate)] &&
[self.selectedContentViewController respondsToSelector:@selector(viewDidShrinkInExposeController:animated:)]) {
[(id<LIExposeControllerChildViewControllerDelegate>)self.selectedContentViewController viewDidShrinkInExposeController:self animated:animated];
}
if ([self.exposeDelegate respondsToSelector:@selector(exposeControllerDidZoomOut:animated:)]) {
[self.exposeDelegate exposeControllerDidZoomOut:self animated:animated];
}
for (UIViewController *viewController in self.viewControllers) {
if (![viewController isEqual:self.selectedViewController]) {
[viewController viewDidAppear:animated];
}
}
}
- (void)exposeZoomedIn:(BOOL)animated {
for (UIView *view in self.containerViews) {
[self toggleGestureRecognizer:NO forView:view];
}
self.selectedViewController.view.layer.masksToBounds = YES;
if ([self.selectedContentViewController conformsToProtocol:@protocol(LIExposeControllerChildViewControllerDelegate)] &&
[self.selectedContentViewController respondsToSelector:@selector(viewDidExpandInExposeController:animated:)]) {
[(id<LIExposeControllerChildViewControllerDelegate>)self.selectedContentViewController viewDidExpandInExposeController:self animated:animated];
}
if ([self.exposeDelegate respondsToSelector:@selector(exposeControllerDidZoomIn:animated:)]) {
[self.exposeDelegate exposeControllerDidZoomIn:self animated:animated];
}
}
#pragma mark - Rotation Methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
BOOL shouldRotate = YES;
for (UIViewController *viewController in self.viewControllers) {
shouldRotate &= [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
return shouldRotate;
}
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
for (UIViewController *viewController in self.viewControllers) {
[viewController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateSecondHalfOfRotationFromInterfaceOrientation:fromInterfaceOrientation duration:duration];
for (UIViewController *viewController in self.viewControllers) {
[viewController willAnimateSecondHalfOfRotationFromInterfaceOrientation:fromInterfaceOrientation duration:duration];
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
for (UIViewController *viewController in self.viewControllers) {
[viewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
if (self.isZoomedOut) {
[self layoutGrid:YES completion:nil];
}
self.scrollView.contentOffset = CGPointMake(self.scrollView.exposeWidth * self.currentPage, 0);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
for (UIViewController *viewController in self.viewControllers) {
[viewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
self.currentPage = self.scrollView.contentOffset.x / self.scrollView.exposeWidth;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
for (UIViewController *viewController in self.viewControllers) {
[viewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
[self calculateContentSize];
}
#pragma mark - UIScrollViewDelegate Methods
- (void)setPage {
NSInteger page = (self.scrollView.contentOffset.x + floorf(self.scrollView.exposeWidth / 2)) / self.scrollView.exposeWidth;
self.pageControl.currentPage = page;
}
- (void)scrollViewDidScroll:(UIScrollView *)scroller {
if ([scroller isEqual:self.scrollView]) {
[self setPage];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scroller {
if ([scroller isEqual:self.scrollView]) {
[self setPage];
}
}
#pragma mark - Helpers Methods
- (BOOL)isPad {
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
}
+ (UIViewController *)getContentViewControllerFromContainer:(UIViewController *)containerViewController {
if ([containerViewController isKindOfClass:[UINavigationController class]]) {
return [(UINavigationController *)containerViewController topViewController];
} else if ([containerViewController isKindOfClass:[UITabBarController class]]) {
return [(UITabBarController *)containerViewController selectedViewController];
} else {
return containerViewController;
}
}
- (void)bounceView:(UIView *)view {
CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
bounceAnimation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:self.scaleFactor],
[NSNumber numberWithFloat:self.scaleFactor-0.02],
[NSNumber numberWithFloat:self.scaleFactor],
nil];
bounceAnimation.duration = self.animationDuration;
bounceAnimation.removedOnCompletion = NO;
[view.layer addAnimation:bounceAnimation forKey:@"bounce"];
}
@end
#pragma mark - UIView+Additions
@implementation UIView (Expose_Additions)
- (CGFloat)exposeLeft {
return self.frame.origin.x;
}
- (void)setExposeLeft:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)exposeTop {
return self.frame.origin.y;
}
- (void)setExposeTop:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)exposeRight {
return self.frame.origin.x + self.frame.size.width;
}
- (CGFloat)exposeBottom {
return self.frame.origin.y + self.frame.size.height;
}
- (CGFloat)exposeWidth {
return self.frame.size.width;