forked from mayoita/AOTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAOTutorialViewController.m
More file actions
344 lines (264 loc) · 11.5 KB
/
Copy pathAOTutorialViewController.m
File metadata and controls
344 lines (264 loc) · 11.5 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
//
// AOTutorialViewController.m
// BabyPlanner
//
// Created by Loïc GRIFFIE on 11/10/2013.
// Copyright (c) 2013 Appsido. All rights reserved.
//
#import "AOTutorialViewController.h"
#define headerLeftMargin 20.0f
#define headerFontSize 15.0f
#define headerFontType @"Helvetica"
#define headerColor [UIColor whiteColor]
#define labelLeftMargin 40.0f
#define labelFontSize 12.0f
#define labelFontType @"Helvetica-Light"
#define labelColor [UIColor whiteColor]
#define buttonColorTop @"#FFFFFF"
#define buttonColorBottom @"#D5D5D5"
#define loginLabelColor @"#000000"
#define signupLabelColor @"#B80000"
#define dismissLabelColor @"#000000"
@interface UIColor (AOAdditions)
/**
* Generate UIColor with given Hexadecimal color such as 0x123456
*
* @param UInt32 The hexadecimal color
* @return UIColor translation
*/
+ (UIColor *)colorWithHex:(UInt32)col;
/**
* Generate UIColor with given Hexadecimal color string representationsuch as #123456
*
* @param NSString The hexadecimal color
* @return UIColor translation
*/
+ (UIColor *)colorWithHexString:(NSString *)str;
@end
@implementation UIColor (AOAdditions)
+ (UIColor *)colorWithHexString:(NSString *)str {
const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
long x = strtol(cStr+1, NULL, 16);
return [UIColor colorWithHex:(int)x];
}
+ (UIColor *)colorWithHex:(UInt32)col {
unsigned char r, g, b;
b = col & 0xFF;
g = (col >> 8) & 0xFF;
r = (col >> 16) & 0xFF;
return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
}
@end
@interface AOButton : UIButton
@end
@implementation AOButton
- (void)drawRect:(CGRect)rect
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
NSArray *gradientColors = @[(id)[[UIColor colorWithHexString:buttonColorTop] CGColor], (id)[[UIColor colorWithHexString:buttonColorBottom] CGColor]];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5];
CGContextSaveGState(context);
[roundedRectanglePath addClip];
CGContextDrawLinearGradient(context, gradient, CGPointMake(rect.size.width/2, 0), CGPointMake(rect.size.width/2, rect.size.height), 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
@end
@interface AOTutorialViewController ()
{
NSUInteger _index;
}
@property (strong, nonatomic) NSMutableArray *backgroundImages;
@property (strong, nonatomic) NSMutableArray *informationLabels;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundBottomImage;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundTopImage;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIPageControl *pageController;
@property (weak, nonatomic) IBOutlet UIImageView *logo;
@property (weak, nonatomic) IBOutlet AOButton *signupButton;
@property (weak, nonatomic) IBOutlet AOButton *loginButton;
@property (weak, nonatomic) IBOutlet AOButton *dismissButton;
@end
@implementation AOTutorialViewController
#pragma mark - Helper function
CGSize ACMStringSize(NSString *string, CGSize size, NSDictionary *attributes)
{
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:string];
[textStorage setAttributes:attributes range:NSMakeRange(0, [textStorage length])];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:size];
textContainer.lineFragmentPadding = 0;
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
return [layoutManager usedRectForTextContainer:textContainer].size;
}
#pragma mark - Life cycle methods
- (instancetype)initWithBackgroundImages:(NSArray *)images andInformations:(NSArray *)informations
{
self = [self initWithNibName:@"AOTutorialViewController" bundle:nil];
if (self)
{
self.backgroundImages = [NSMutableArray arrayWithArray:images];
self.informationLabels = [NSMutableArray arrayWithArray:informations];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
_index = 0;
_buttons = AOTutorialButtonNone;
self.backgroundImages = [NSMutableArray array];
self.informationLabels = [NSMutableArray array];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.header)
{
[self.logo setHidden:NO];
[self.logo setImage:self.header];
}
[self.pageController setNumberOfPages:[self.backgroundImages count]];
[self.scrollview setContentSize:CGSizeMake(self.scrollview.frame.size.width * [self.backgroundImages count], self.scrollview.frame.size.height)];
[self layoutViews];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)addButton
{
[self.loginButton setTitleColor:[UIColor colorWithHexString:loginLabelColor] forState:UIControlStateNormal];
[self.signupButton setTitleColor:[UIColor colorWithHexString:signupLabelColor] forState:UIControlStateNormal];
[self.dismissButton setTitleColor:[UIColor colorWithHexString:dismissLabelColor] forState:UIControlStateNormal];
[self.dismissButton setHidden:YES];
switch (self.buttons) {
case AOTutorialButtonSignup:
[self.signupButton setHidden:NO];
break;
case AOTutorialButtonLogin:
[self.loginButton setHidden:NO];
break;
case AOTutorialButtonSignup | AOTutorialButtonLogin:
[self.signupButton setHidden:NO];
[self.loginButton setHidden:NO];
break;
default:
[self.signupButton setHidden:YES];
[self.loginButton setHidden:YES];
[self.dismissButton setHidden:NO];
}
}
- (void)layoutViews
{
[self addButton];
[self addInformationsLabels];
[self resetBackgroundImageState];
[self.backgroundBottomImage setImage:[UIImage imageNamed:[self.backgroundImages objectAtIndex:_index+1]]];
}
- (void)addInformationsLabels
{
NSUInteger index = 0;
for (NSDictionary *labels in self.informationLabels)
{
CGSize hSize = ACMStringSize([labels valueForKey:@"Header"], CGSizeMake(([[UIScreen mainScreen] bounds].size.width - (headerLeftMargin * 2)), 60.0f), [self headerTextStyleAttributes]);
CGSize lSize = ACMStringSize([labels valueForKey:@"Label"], CGSizeMake(([[UIScreen mainScreen] bounds].size.width - (labelLeftMargin * 2)), 60.0f), [self labelTextStyleAttributes]);
NSLog(@"Frame > %@", NSStringFromCGRect(self.scrollview.frame));
UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0.0f + ([[UIScreen mainScreen] bounds].size.width * index) + headerLeftMargin, [[UIScreen mainScreen] bounds].size.height - 120.0f - lSize.height, ([[UIScreen mainScreen] bounds].size.width - (headerLeftMargin * 2)), hSize.height + 5.0f)];
[header setNumberOfLines:1];
[header setLineBreakMode:NSLineBreakByTruncatingTail];
[header setShadowOffset:CGSizeMake(1, 1)];
[header setShadowColor:[UIColor blackColor]];
[header setText:[labels valueForKey:@"Header"]];
[header setTextAlignment:NSTextAlignmentCenter];
[header setBackgroundColor:[UIColor clearColor]];
[header setTextColor:headerColor];
[header setFont:[UIFont fontWithName:headerFontType size:headerFontSize]];
[self.scrollview addSubview:header];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f + ([[UIScreen mainScreen] bounds].size.width * index) + labelLeftMargin, header.frame.origin.y + hSize.height + 5.0f, ([[UIScreen mainScreen] bounds].size.width - (labelLeftMargin * 2)), lSize.height)];
[label setNumberOfLines:0];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setShadowOffset:CGSizeMake(1, 1)];
[label setShadowColor:[UIColor blackColor]];
[label setText:[labels valueForKey:@"Label"]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:labelColor];
[label setFont:[UIFont fontWithName:labelFontType size:labelFontSize]];
[self.scrollview addSubview:label];
index++;
}
}
- (void)resetBackgroundImageState
{
[self.backgroundTopImage setImage:[UIImage imageNamed:[self.backgroundImages objectAtIndex:_index]]];
[self.backgroundTopImage setAlpha:1.0];
[self.backgroundBottomImage setAlpha:0.0];
}
- (void)setHeaderImage:(UIImage *)logo
{
[self setHeader:logo];
}
#pragma mark - Labels methods
- (NSDictionary *)headerTextStyleAttributes
{
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSTextAlignmentCenter];
[style setLineSpacing:5.0f];
[style setLineBreakMode:NSLineBreakByTruncatingTail];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(1.0, 1.0);
return @{NSFontAttributeName:[UIFont fontWithName:headerFontType size:headerFontSize], NSForegroundColorAttributeName:headerColor, NSParagraphStyleAttributeName:style, NSShadowAttributeName:shadow};
}
- (NSDictionary *)labelTextStyleAttributes
{
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSTextAlignmentCenter];
[style setLineSpacing:5.0f];
[style setLineBreakMode:NSLineBreakByWordWrapping];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(1.0, 1.0);
return @{NSFontAttributeName:[UIFont fontWithName:labelFontType size:labelFontSize], NSForegroundColorAttributeName:labelColor, NSParagraphStyleAttributeName:style, NSShadowAttributeName:shadow};
}
#pragma mark - User interface methods
- (IBAction)signup:(id)sender
{
}
- (IBAction)login:(id)sender
{
}
- (IBAction)dismiss:(id)sender
{
}
#pragma mark - Scrollview delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat alpha = [scrollView contentOffset].x / [[UIScreen mainScreen] bounds].size.width - _index;
NSInteger newIndex = (alpha < 0 ? _index-1 : _index+1);
UIImage *nextImage = [UIImage imageNamed:[self.backgroundImages objectAtIndex:(newIndex < 0 ? 0 : (newIndex >= [self.backgroundImages count] ? [self.backgroundImages count]-1 : newIndex))]];
if (![[self.backgroundBottomImage image] isEqual:nextImage]) [self.backgroundBottomImage setImage:nextImage];
[self.backgroundTopImage setAlpha:1-fabs(alpha)];
[self.backgroundBottomImage setAlpha:fabs(alpha)];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.scrollview.frame.size.width;
_index = floor((self.scrollview.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
[self.pageController setCurrentPage:_index];
[self resetBackgroundImageState];
}
@end