-
Notifications
You must be signed in to change notification settings - Fork 28
/
RootViewController.m
192 lines (132 loc) · 8.44 KB
/
RootViewController.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
//
// ViewController.m
// Greetings-ObjC
//
// Created by Erik Malyak on 9/20/16.
// Copyright © 2016 Erik Malyak. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupGreetings];
// Instantiate EMPageViewController and set the data source and delegate to 'self'
EMPageViewController *pageViewController = [[EMPageViewController alloc] init];
// Or, for a vertical orientation
// EMPageViewController *pageViewController = [[EMPageViewController alloc] initWithNavigationOrientation:EMPageViewControllerNavigationOrientationVertical];
pageViewController.dataSource = self;
pageViewController.delegate = self;
// Set the initially selected view controller
// IMPORTANT: If you are using a dataSource, make sure you set it BEFORE calling selectViewController:direction:animated:completion
GreetingViewController *currentViewController = [self viewControllerAtIndex:0];
[pageViewController selectViewController:currentViewController direction:EMPageViewControllerNavigationDirectionForward animated:false completion:nil];
// Add EMPageViewController to the root view controller
[self addChildViewController:pageViewController];
[self.view insertSubview:pageViewController.view atIndex:0]; // Insert the page controller view below the navigation buttons
[pageViewController didMoveToParentViewController:self];
self.pageViewController = pageViewController;
}
- (void)setupGreetings {
self.greetings = @[@"Hello!", @"¡Hola!", @"Salut!", @"Hallo!", @"Ciao!"];
self.greetingColors = @[
[UIColor colorWithRed:108.0/255.0 green:122.0/255.0 blue:137.0/255.0 alpha:1.0],
[UIColor colorWithRed:135.0/255.0 green:211.0/255.0 blue:124.0/255.0 alpha:1.0],
[UIColor colorWithRed:34.0/255.0 green:167.0/255.0 blue:240.0/255.0 alpha:1.0],
[UIColor colorWithRed:245.0/255.0 green:171.0/255.0 blue:53.0/255.0 alpha:1.0],
[UIColor colorWithRed:214.0/255.0 green:69.0/255.0 blue:65.0/255.0 alpha:1.0]
];
}
# pragma mark - Convienient EMPageViewController scroll / transition methods
- (IBAction)forward:(id)sender {
[self.pageViewController scrollForwardAnimated:true completion:nil];
}
- (IBAction)reverse:(id)sender {
[self.pageViewController scrollReverseAnimated:true completion:nil];
}
- (IBAction)scrollTo:(id)sender {
UIAlertController *choiceAlertController = [UIAlertController alertControllerWithTitle:@"Scroll To..." message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSUInteger selectedIndex = [self indexOfViewController:(GreetingViewController *)self.pageViewController.selectedViewController];
NSUInteger index = 0;
for (NSString *greeting in self.greetings) {
if (index != selectedIndex) {
UIAlertAction *action = [UIAlertAction actionWithTitle:greeting style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
GreetingViewController *viewController = [self viewControllerAtIndex:index];
EMPageViewControllerNavigationDirection direction = index > selectedIndex ? EMPageViewControllerNavigationDirectionForward : EMPageViewControllerNavigationDirectionReverse;
[self.pageViewController selectViewController:viewController direction:direction animated:YES completion:nil];
}];
[choiceAlertController addAction:action];
}
index++;
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[choiceAlertController addAction:cancelAction];
[self presentViewController:choiceAlertController animated:YES completion:nil];
}
# pragma mark - EMPageViewController Data Source
- (UIViewController *)em_pageViewController:(EMPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger viewControllerIndex = [self indexOfViewController:(GreetingViewController *)viewController];
if (viewControllerIndex == NSNotFound) {
return nil;
} else {
return [self viewControllerAtIndex:viewControllerIndex - 1];
}
}
- (UIViewController *)em_pageViewController:(EMPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger viewControllerIndex = [self indexOfViewController:(GreetingViewController *)viewController];
if (viewControllerIndex == NSNotFound) {
return nil;
} else {
return [self viewControllerAtIndex:viewControllerIndex + 1];
}
}
- (GreetingViewController * _Nullable)viewControllerAtIndex: (NSUInteger)index {
if ((self.greetings.count == 0) || (index >= self.greetings.count)) {
return nil;
}
GreetingViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"GreetingViewController"];
viewController.greeting = self.greetings[index];
viewController.color = self.greetingColors[index];
return viewController;
}
- (NSUInteger)indexOfViewController:(GreetingViewController * _Nonnull)viewController {
NSString *greeting = viewController.greeting;
return [self.greetings indexOfObject:greeting];
}
# pragma mark - EMPageViewController Delegate
- (void)em_pageViewController:(EMPageViewController *)pageViewController willStartScrollingFrom:(UIViewController *)startingViewController destinationViewController:(UIViewController *)destinationViewController {
GreetingViewController *startGreetingViewController = (GreetingViewController *)startingViewController;
GreetingViewController *destinationGreetingViewController = (GreetingViewController *)destinationViewController;
NSLog(@"Will start scrolling from %@ to %@.", startGreetingViewController.greeting, destinationGreetingViewController.greeting);
}
- (void)em_pageViewController:(EMPageViewController *)pageViewController isScrollingFrom:(UIViewController *)startingViewController destinationViewController:(UIViewController *)destinationViewController progress:(CGFloat)progress {
GreetingViewController *startGreetingViewController = (GreetingViewController *)startingViewController;
GreetingViewController *destinationGreetingViewController = (GreetingViewController *)destinationViewController;
// Ease the labels' alphas in and out
CGFloat absoluteProgress = fabs(progress);
startGreetingViewController.label.alpha = pow(1 - absoluteProgress, 2);
destinationGreetingViewController.label.alpha = pow(absoluteProgress, 2);
NSLog(@"Is scrolling from %@ to %@ with progress %f.", startGreetingViewController.greeting, destinationGreetingViewController.greeting, progress);
}
- (void)em_pageViewController:(EMPageViewController *)pageViewController didFinishScrollingFrom:(UIViewController *)startingViewController destinationViewController:(UIViewController *)destinationViewController transitionSuccessful:(BOOL)transitionSuccessful {
GreetingViewController *startGreetingViewController = (GreetingViewController *)startingViewController;
GreetingViewController *destinationGreetingViewController = (GreetingViewController *)destinationViewController;
// If the transition is successful, the new selected view controller is the destination view controller.
// If it wasn't successful, the selected view controller is the start view controller
if (transitionSuccessful) {
NSUInteger destinationViewControllerIndex = [self indexOfViewController:destinationGreetingViewController];
if (destinationViewControllerIndex == 0) {
[self.reverseButton setEnabled:NO];
} else {
[self.reverseButton setEnabled:YES];
}
if (destinationViewControllerIndex == self.greetings.count - 1) {
[self.forwardButton setEnabled:NO];
} else {
[self.forwardButton setEnabled:YES];
}
}
NSLog(@"Finished scrolling from %@ to %@. Transition successful? %@", startGreetingViewController.greeting, destinationGreetingViewController.greeting, (transitionSuccessful ? @"YES" : @"NO"));
}
@end