Newer
Older
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
//
// OnboardingContentViewController.m
// Onboard
//
// Created by Mike on 8/17/14.
// Copyright (c) 2014 Mike Amaral. All rights reserved.
//
#import "OnboardingContentViewController.h"
#import "OnboardingViewController.h"
@import AVFoundation;
static NSString * const kDefaultOnboardingFont = @"Helvetica-Light";
#define DEFAULT_TEXT_COLOR [UIColor whiteColor];
static CGFloat const kContentWidthMultiplier = 0.9;
static CGFloat const kDefaultImageViewSize = 100;
static CGFloat const kDefaultTopPadding = 60;
static CGFloat const kDefaultUnderIconPadding = 30;
static CGFloat const kDefaultUnderTitlePadding = 30;
static CGFloat const kDefaultBottomPadding = 0;
static CGFloat const kDefaultUnderPageControlPadding = 0;
static CGFloat const kDefaultTitleFontSize = 38;
static CGFloat const kDefaultBodyFontSize = 28;
static CGFloat const kDefaultButtonFontSize = 24;
static CGFloat const kActionButtonHeight = 50;
static CGFloat const kMainPageControlHeight = 35;
NSString * const kOnboardMainTextAccessibilityIdentifier = @"OnboardMainTextAccessibilityIdentifier";
NSString * const kOnboardSubTextAccessibilityIdentifier = @"OnboardSubTextAccessibilityIdentifier";
NSString * const kOnboardActionButtonAccessibilityIdentifier = @"OnboardActionButtonAccessibilityIdentifier";
NSString * const kOnboardInputViewAccessibilityIdentifier = @"OnboardInputViewAccessibilityIdentifier";
@interface OnboardingContentViewController ()
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic) BOOL wasPreviouslyVisible;
@end
@implementation OnboardingContentViewController
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Initializers
+ (instancetype)contentWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image buttonText:(NSString *)buttonText action:(dispatch_block_t)action {
return [[self alloc] initWithTitle:title body:body image:image buttonText:buttonText action:action];
}
- (instancetype)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image buttonText:(NSString *)buttonText action:(dispatch_block_t)action {
return [self initWithTitle:title body:body image:image buttonText:buttonText actionBlock:^(OnboardingViewController *onboardController) {
if (action) {
action();
}
}];
}
+ (instancetype)contentWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image buttonText:(NSString *)buttonText actionBlock:(action_callback)actionBlock {
return [[self alloc] initWithTitle:title body:body image:image buttonText:buttonText actionBlock:actionBlock];
}
+ (instancetype)contentWithTitle:(NSString *)title body:(NSString *)body videoURL:(NSURL *)videoURL buttonText:(NSString *)buttonText action:(dispatch_block_t)action {
return [[self alloc] initWithTitle:title body:body videoURL:videoURL buttonText:buttonText action:action];
}
+ (instancetype)contentWithTitle:(NSString *)title body:(NSString *)body videoURL:(NSURL *)videoURL inputView:(UIView *)inputView buttonText:(NSString *)buttonText actionBlock:(action_callback)actionBlock {
return [[self alloc] initWithTitle:title body:body image:nil videoURL:videoURL inputView:inputView buttonText:buttonText actionBlock:actionBlock];
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
}
- (instancetype)initWithTitle:(NSString *)title body:(NSString *)body videoURL:(NSURL *)videoURL buttonText:(NSString *)buttonText action:(dispatch_block_t)action {
return [self initWithTitle:title body:body image:nil videoURL:videoURL buttonText:buttonText actionBlock:^(OnboardingViewController *onboardController) {
if (action) {
action();
}
}];
}
+ (instancetype)contentWithTitle:(NSString *)title body:(NSString *)body videoURL:(NSURL *)videoURL buttonText:(NSString *)buttonText actionBlock:(action_callback)actionBlock {
return [[self alloc] initWithTitle:title body:body image:nil videoURL:videoURL buttonText:buttonText actionBlock:actionBlock];
}
- (instancetype)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image buttonText:(NSString *)buttonText actionBlock:(action_callback)actionBlock {
return [self initWithTitle:title body:body image:image videoURL:nil buttonText:buttonText actionBlock:actionBlock];
}
- (instancetype)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image videoURL:(NSURL *)videoURL buttonText:(NSString *)buttonText actionBlock:(action_callback)actionBlock {
self = [super init];
if (self == nil) {
return nil;
}
// Icon image view
self.iconImageView = [[UIImageView alloc] initWithImage:image];
self.iconWidth = image ? image.size.width : kDefaultImageViewSize;
self.iconHeight = image ? image.size.height : kDefaultImageViewSize;
// Title label
self.titleLabel = [UILabel new];
self.titleLabel.accessibilityIdentifier = kOnboardMainTextAccessibilityIdentifier;
self.titleLabel.text = title;
self.titleLabel.textColor = DEFAULT_TEXT_COLOR;
self.titleLabel.font = [UIFont fontWithName:kDefaultOnboardingFont size:kDefaultTitleFontSize];
self.titleLabel.numberOfLines = 0;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
// Body label
self.bodyLabel = [UILabel new];
self.bodyLabel.accessibilityIdentifier = kOnboardSubTextAccessibilityIdentifier;
self.bodyLabel.text = body;
self.bodyLabel.textColor = DEFAULT_TEXT_COLOR;
self.bodyLabel.font = [UIFont fontWithName:kDefaultOnboardingFont size:kDefaultBodyFontSize];
self.bodyLabel.numberOfLines = 0;
self.bodyLabel.textAlignment = NSTextAlignmentCenter;
// Action button
self.actionButton = [UIButton new];
self.actionButton.accessibilityIdentifier = kOnboardActionButtonAccessibilityIdentifier;
self.actionButton.titleLabel.font = [UIFont fontWithName:kDefaultOnboardingFont size:kDefaultButtonFontSize];
[self.actionButton setTitle:buttonText forState:UIControlStateNormal];
[self.actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.actionButton addTarget:self action:@selector(handleButtonPressed) forControlEvents:UIControlEventTouchUpInside];
self.buttonActionHandler = actionBlock ?: ^(OnboardingViewController *controller){};
// Movie player
self.videoURL = videoURL;
// Auto-navigation
self.movesToNextViewController = NO;
// Default padding values
self.topPadding = kDefaultTopPadding;
self.underIconPadding = kDefaultUnderIconPadding;
self.underTitlePadding = kDefaultUnderTitlePadding;
self.bottomPadding = kDefaultBottomPadding;
self.underPageControlPadding = kDefaultUnderPageControlPadding;
// Default blocks
self.viewWillAppearBlock = ^{};
self.viewDidAppearBlock = ^{};
self.viewWillDisappearBlock = ^{};
self.viewDidDisappearBlock = ^{};
return self;
}
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
- (instancetype)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image videoURL:(NSURL *)videoURL inputView:(UIView *)inputView buttonText:(NSString *)buttonText actionBlock:(action_callback)actionBlock {
self = [super init];
if (self == nil) {
return nil;
}
// Icon image view
self.iconImageView = [[UIImageView alloc] initWithImage:image];
self.iconWidth = image ? image.size.width : kDefaultImageViewSize;
self.iconHeight = image ? image.size.height : kDefaultImageViewSize;
// Title label
self.titleLabel = [UILabel new];
self.titleLabel.accessibilityIdentifier = kOnboardMainTextAccessibilityIdentifier;
self.titleLabel.text = title;
self.titleLabel.textColor = DEFAULT_TEXT_COLOR;
self.titleLabel.font = [UIFont fontWithName:kDefaultOnboardingFont size:kDefaultTitleFontSize];
self.titleLabel.numberOfLines = 0;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
// Body label
self.bodyLabel = [UILabel new];
self.bodyLabel.accessibilityIdentifier = kOnboardSubTextAccessibilityIdentifier;
self.bodyLabel.text = body;
self.bodyLabel.textColor = DEFAULT_TEXT_COLOR;
self.bodyLabel.font = [UIFont fontWithName:kDefaultOnboardingFont size:kDefaultBodyFontSize];
self.bodyLabel.numberOfLines = 0;
self.bodyLabel.textAlignment = NSTextAlignmentCenter;
// Input view
self.inputView = inputView;
if (self.inputView) {
self.inputView.accessibilityIdentifier = kOnboardInputViewAccessibilityIdentifier;
}
// Action button
self.actionButton = [UIButton new];
self.actionButton.accessibilityIdentifier = kOnboardActionButtonAccessibilityIdentifier;
self.actionButton.titleLabel.font = [UIFont fontWithName:kDefaultOnboardingFont size:kDefaultButtonFontSize];
[self.actionButton setTitle:buttonText forState:UIControlStateNormal];
[self.actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.actionButton addTarget:self action:@selector(handleButtonPressed) forControlEvents:UIControlEventTouchUpInside];
self.buttonActionHandler = actionBlock ?: ^(OnboardingViewController *controller){};
// Movie player
self.videoURL = videoURL;
// Auto-navigation
self.movesToNextViewController = NO;
// Default padding values
self.topPadding = kDefaultTopPadding;
self.underIconPadding = kDefaultUnderIconPadding;
self.underTitlePadding = kDefaultUnderTitlePadding;
self.bottomPadding = kDefaultBottomPadding;
self.underPageControlPadding = kDefaultUnderPageControlPadding;
// Default blocks
self.viewWillAppearBlock = ^{};
self.viewDidAppearBlock = ^{};
self.viewWillDisappearBlock = ^{};
self.viewDidDisappearBlock = ^{};
return self;
}
#pragma mark - View life cycle
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppEnteredForeground) name:UIApplicationDidBecomeActiveNotification object:nil];
self.view.backgroundColor = [UIColor clearColor];
// Add all our subviews
if (self.videoURL) {
self.player = [[AVPlayer alloc] initWithURL:self.videoURL];
self.moviePlayerController = [AVPlayerViewController new];
self.moviePlayerController.player = self.player;
self.moviePlayerController.showsPlaybackControls = NO;
[self.view addSubview:self.moviePlayerController.view];
}
[self.view addSubview:self.iconImageView];
[self.view addSubview:self.titleLabel];
[self.view addSubview:self.bodyLabel];
if (self.inputView) {
[self.view addSubview:self.inputView];
}
[self.view addSubview:self.actionButton];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// If we have a delegate set, mark ourselves as the next page now that we're
// about to appear
if (self.delegate) {
[self.delegate setNextPage:self];
}
// If we have a video URL, start playing
if (self.videoURL) {
[self.player play];
}
// Call our view will appear block
if (self.viewWillAppearBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.viewWillAppearBlock();
});
}
self.wasPreviouslyVisible = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// If we have a delegate set, mark ourselves as the current page now that
// we've appeared
if (self.delegate) {
[self.delegate setCurrentPage:self];
}
// Call our view did appear block
if (self.viewDidAppearBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.viewDidAppearBlock();
});
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Call our view will disappear block
if (self.viewWillDisappearBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.viewWillDisappearBlock();
});
}
self.wasPreviouslyVisible = NO;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// Call our view did disappear block
if (self.viewDidDisappearBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.viewDidDisappearBlock();
});
}
// Pause our video if we have one.
if ((self.player.rate != 0.0) && !self.player.error) {
[self.player pause];
}
}
#pragma mark - App life cycle
- (void)handleAppEnteredForeground {
// If we have a video URL and this view controller was previously on screen
// restart it as it will be paused when the app enters the foreground.
if (self.videoURL && self.wasPreviouslyVisible) {
[self.player play];
}
}
#pragma mark - Layout
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
if (self.videoURL) {
self.moviePlayerController.view.frame = self.view.frame;
}
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat contentWidth = viewWidth * kContentWidthMultiplier;
CGFloat xPadding = (viewWidth - contentWidth) / 2.0;
[self.iconImageView setFrame:CGRectMake((viewWidth / 2.0) - (self.iconWidth / 2.0), self.topPadding, self.iconWidth, self.iconHeight)];
CGFloat titleYOrigin = CGRectGetMaxY(self.iconImageView.frame) + self.underIconPadding;
self.titleLabel.frame = CGRectMake(xPadding, titleYOrigin, contentWidth, 0);
[self.titleLabel sizeToFit];
self.titleLabel.frame = CGRectMake(xPadding, titleYOrigin, contentWidth, CGRectGetHeight(self.titleLabel.frame));
CGFloat bodyYOrigin = CGRectGetMaxY(self.titleLabel.frame) + self.underTitlePadding;
self.bodyLabel.frame = CGRectMake(xPadding, bodyYOrigin, contentWidth, 0);
[self.bodyLabel sizeToFit];
self.bodyLabel.frame = CGRectMake(xPadding, bodyYOrigin, contentWidth, CGRectGetHeight(self.bodyLabel.frame));
self.actionButton.frame = CGRectMake((CGRectGetMaxX(self.view.frame) / 2) - (contentWidth / 2), CGRectGetMaxY(self.view.frame) - self.underPageControlPadding - kMainPageControlHeight - kActionButtonHeight - self.bottomPadding, contentWidth, kActionButtonHeight);
CGFloat inputYOrigin = CGRectGetMaxY(self.bodyLabel.frame) + self.underTitlePadding;
if (self.inputView) {
self.inputView.frame = CGRectMake(xPadding, inputYOrigin, contentWidth, CGRectGetHeight(self.inputView.frame));
for (UIView *view in self.inputView.subviews){
view.frame = CGRectMake(CGRectGetMinX(view.frame), CGRectGetMinY(view.frame), contentWidth, CGRectGetHeight(view.frame));
}
self.actionButton.frame = CGRectMake((CGRectGetMaxX(self.view.frame) / 2) - (contentWidth / 2), CGRectGetMaxY(self.view.frame) - self.underPageControlPadding - kMainPageControlHeight - kActionButtonHeight - self.bottomPadding - CGRectGetMaxY(self.inputView.frame), contentWidth, kActionButtonHeight);
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
}
}
#pragma mark - Transition alpha
- (void)updateAlphas:(CGFloat)newAlpha {
self.iconImageView.alpha = newAlpha;
self.titleLabel.alpha = newAlpha;
self.bodyLabel.alpha = newAlpha;
self.actionButton.alpha = newAlpha;
}
#pragma mark - Action button stuff
- (void)handleButtonPressed {
// if we want to navigate to the next view controller, tell our delegate
// to handle it
if (self.movesToNextViewController) {
[self.delegate moveNextPage];
}
// call the provided action handler
if (self.buttonActionHandler) {
self.buttonActionHandler(self.delegate);
}
}
@end