-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMYIntroductionView.m
More file actions
executable file
·694 lines (585 loc) · 30.9 KB
/
MYIntroductionView.m
File metadata and controls
executable file
·694 lines (585 loc) · 30.9 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
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
//
// MYIntroductionView.m
// IntroductionExample
//
// Copyright (C) 2013, Matt York
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import "MYIntroductionView.h"
#define DEFAULT_BACKGROUND_COLOR [UIColor colorWithWhite:0 alpha:0.9]
#define HEADER_VIEW_HEIGHT 45
#define PAGE_CONTROL_PADDING 1
#define TITLE_FONT [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0]
#define TITLE_TEXT_COLOR [UIColor whiteColor]
#define DESCRIPTION_FONT [UIFont fontWithName:@"HelveticaNeue-Light" size:13.0]
#define DESCRIPTION_TEXT_COLOR [UIColor whiteColor]
@implementation MYIntroductionView
@synthesize delegate, device, device_orientation;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeClassVariables];
[self buildUIWithFrame:frame headerViewVisible:YES];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}
return self;
}
- (id)initWithFrame:(CGRect)frame headerText:(NSString *)headerText panels:(NSArray *)panels
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeClassVariables];
Panels = [panels copy];
LanguageDirection = MYLanguageDirectionLeftToRight;
[self buildUIWithFrame:frame headerViewVisible:YES];
[self setHeaderText:headerText];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}
return self;
}
- (id)initWithFrame:(CGRect)frame headerImage:(UIImage *)headerImage panels:(NSArray *)panels
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeClassVariables];
Panels = [panels copy];
LanguageDirection = MYLanguageDirectionLeftToRight;
[self buildUIWithFrame:frame headerViewVisible:YES];
[self setHeaderImage:headerImage];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}
return self;
}
- (id)initWithFrame:(CGRect)frame headerText:(NSString *)headerText panels:(NSArray *)panels languageDirection:(MYLanguageDirection)languageDirection
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeClassVariables];
Panels = [panels copy];
LanguageDirection = languageDirection;
[self buildUIWithFrame:frame headerViewVisible:YES];
[self setHeaderText:headerText];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}
return self;
}
- (id)initWithFrame:(CGRect)frame headerImage:(UIImage *)headerImage panels:(NSArray *)panels languageDirection:(MYLanguageDirection)languageDirection
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeClassVariables];
Panels = [panels copy];
LanguageDirection = languageDirection;
[self buildUIWithFrame:frame headerViewVisible:YES];
[self setHeaderImage:headerImage];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}
return self;
}
- (id)initWithFrame:(CGRect)frame panels:(NSArray *)panels
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeClassVariables];
Panels = [panels copy];
LanguageDirection = MYLanguageDirectionLeftToRight;
[self buildUIWithFrame:frame headerViewVisible:NO];
[self setHeaderText:nil];
}
return self;
}
- (id)initWithFrame:(CGRect)frame panels:(NSArray *)panels languageDirection:(MYLanguageDirection)languageDirection
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeClassVariables];
Panels = [panels copy];
LanguageDirection = languageDirection;
[self buildUIWithFrame:frame headerViewVisible:NO];
[self setHeaderText:nil];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}
return self;
}
-(void)initializeClassVariables{
panelViews = [[NSMutableArray alloc] init];
self.device = [self getCurrentDevice];
self.device_orientation = [self getCurrentOrientation];
}
#pragma mark - UI Builder Methods
-(BOOL)getCurrentDevice {
return [[UIDevice currentDevice] userInterfaceIdiom]; //iPhone 1; iPad 0;
}
-(BOOL)getCurrentOrientation {
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIInterfaceOrientationPortrait:
return 0;
break;
case UIInterfaceOrientationPortraitUpsideDown:
return 0;
break;
case UIInterfaceOrientationLandscapeLeft:
return 1;
break;
case UIInterfaceOrientationLandscapeRight:
return 1;
break;
default:
return 0;
break;
}
}
-(void)buildUIWithFrame:(CGRect)frame headerViewVisible:(BOOL)headerViewVisible{
self.backgroundColor = DEFAULT_BACKGROUND_COLOR;
[self buildBackgroundImage];
[self buildHeaderViewWithFrame:frame visible:headerViewVisible];
[self buildContentScrollViewWithFrame:frame];
[self buildFooterView];
[self.BackgroundImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.HeaderImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.HeaderLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.HeaderView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.PageControl setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.SkipButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
-(void)buildBackgroundImage{
self.BackgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
self.BackgroundImageView.backgroundColor = [UIColor clearColor];
self.BackgroundImageView.contentMode = UIViewContentModeScaleToFill;
self.BackgroundImageView.autoresizesSubviews = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self addSubview:self.BackgroundImageView];
}
-(void)buildHeaderViewWithFrame:(CGRect)frame visible:(BOOL)visible{
if (!visible) {
self.HeaderView = [[UIView alloc] initWithFrame:CGRectZero];
return;
}
float headerYOffset = 5;
if ([MYIntroductionView runningiOS7]) {
headerYOffset = headerYOffset + 20;
}
self.HeaderView = [[UIView alloc] initWithFrame:CGRectMake(5, headerYOffset, frame.size.width - 10, HEADER_VIEW_HEIGHT)]; //Leave 5px padding on all sides
self.HeaderView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.HeaderView.backgroundColor = [UIColor clearColor];
//Setup HeaderImageView
self.HeaderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.HeaderView.frame.size.width, self.HeaderView.frame.size.height)];
self.HeaderImageView.backgroundColor = [UIColor clearColor];
self.HeaderImageView.contentMode = UIViewContentModeScaleAspectFit;
self.HeaderImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.HeaderView addSubview:self.HeaderImageView];
self.HeaderImageView.hidden = YES;
//Setup HeaderLabel
self.HeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.HeaderView.frame.size.width, self.HeaderView.frame.size.height)];
self.HeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:25.0];
self.HeaderLabel.textColor = [UIColor whiteColor];
self.HeaderLabel.backgroundColor = [UIColor clearColor];
self.HeaderLabel.textAlignment = NSTextAlignmentCenter;
self.HeaderLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.HeaderView addSubview:self.HeaderLabel];
self.HeaderLabel.hidden = YES;
[self addSubview:self.HeaderView];
// Add a drop shadow to the header text
/*
self.HeaderLabel.layer.shadowColor = [[UIColor blackColor]CGColor];
self.HeaderLabel.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.HeaderLabel.layer.shadowOpacity = 1.0f;
self.HeaderLabel.layer.shadowRadius = 1.0f;
*/
}
-(void)buildContentScrollViewWithFrame:(CGRect)frame{
float centerPadding = frame.size.width;
float outerPadding = 0;
if (self.device == 0) { // iPhone
if (self.device_orientation == 1) { // 1 for landscape
self.ContentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.HeaderView.frame.origin.y + self.HeaderView.frame.size.height - HEADER_VIEW_HEIGHT, frame.size.width, 0)];
} else {
self.ContentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.HeaderView.frame.origin.y + self.HeaderView.frame.size.height, frame.size.width, 0)];
}
} else { // iPad
if (self.device_orientation == 1) { // 1 for landscape
centerPadding = self.frame.size.height;
outerPadding = (self.frame.size.width - self.frame.size.height)/2;
NSLog(@"C:%f, O:%f", centerPadding, outerPadding);
}
self.ContentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(outerPadding, self.HeaderView.frame.origin.y + self.HeaderView.frame.size.height + 10, centerPadding, 0)];
}
self.ContentScrollView.pagingEnabled = YES;
self.ContentScrollView.showsHorizontalScrollIndicator = NO;
self.ContentScrollView.showsVerticalScrollIndicator = NO;
self.ContentScrollView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.ContentScrollView.delegate = self;
//If panels exist, build views for them and add them to the ContentScrollView
if (Panels) {
if (Panels.count > 0) {
if (LanguageDirection == MYLanguageDirectionLeftToRight) {
[self buildContentScrollViewLeftToRight];
}
else if (LanguageDirection == MYLanguageDirectionRightToLeft) {
[self buildContentScrollViewRightToLeft];
}
}
}
}
-(void)buildContentScrollViewLeftToRight{
//A running x-coordinate. This grows for every page
CGFloat contentXIndex = 0;
for (int ii = 0; ii < Panels.count; ii++) {
//Create a new view for the panel and add it to the array
[panelViews addObject:[self PanelViewForPanel:Panels[ii] atXIndex:&contentXIndex]];
//Add the newly created panel view to ContentScrollView
[self.ContentScrollView addSubview:panelViews[ii]];
}
[self makePanelVisibleAtIndex:0];
//Dynamically sizes the content to fit the text content
[self setContentScrollViewHeightForPanelIndex:0 animated:NO];
//Add a view at the end. This is simply "something to scroll toward" on the final panel.
[self appendCloseViewAtXIndex:&contentXIndex];
//Finally, resize the content size of the scrollview to account for all the new views added to it
self.ContentScrollView.contentSize = CGSizeMake(contentXIndex, self.ContentScrollView.frame.size.height);
//Add the ContentScrollView to the introduction view
[self addSubview:self.ContentScrollView];
}
-(void)buildContentScrollViewRightToLeft{
//A running x-coordinate. This grows for every page
CGFloat contentXIndex = 0;
//Add a view at the end. This is simply "something to scroll toward" on the final panel.
[self appendCloseViewAtXIndex:&contentXIndex];
NSInteger panelViewIndex = 0;
for (int ii = Panels.count-1; ii > -1; ii--) {
//Create a new view for the panel and add it to the array
[panelViews addObject:[self PanelViewForPanel:Panels[ii] atXIndex:&contentXIndex]];
//Add the newly created panel view to ContentScrollView
[self.ContentScrollView addSubview:panelViews[panelViewIndex]];
panelViewIndex++;
}
[self makePanelVisibleAtIndex:panelViews.count-1];
self.CurrentPanelIndex = panelViews.count-1;
self.PageControl.currentPage = panelViews.count -1;
//Dynamically sizes the content to fit the text content
[self setContentScrollViewHeightForPanelIndex:Panels.count-1 animated:NO];
//Finally, resize the content size of the scrollview to account for all the new views added to it
self.ContentScrollView.contentSize = CGSizeMake(contentXIndex, self.ContentScrollView.frame.size.height);
self.ContentScrollView.contentOffset = CGPointMake(contentXIndex-self.ContentScrollView.frame.size.width, 0);
//Add the ContentScrollView to the introduction view
[self addSubview:self.ContentScrollView];
}
-(UIView *)PanelViewForPanel:(MYIntroductionPanel *)panel atXIndex:(CGFloat*)xIndex{
//Build panel now that we have all the desired dimensions
UIView *panelView = [[UIView alloc] initWithFrame:CGRectMake(*xIndex, 0, self.ContentScrollView.frame.size.width, 0)];
CGFloat panelContentHeight = MIN(panel.PanelContentView.frame.size.height, self.frame.size.width - 10);
CGFloat panelContentWidth = MIN(panel.PanelContentView.frame.size.width, self.ContentScrollView.frame.size.width-20);
//Build title container (if applicable)
CGRect panelTitleLabelFrame;
UILabel *panelTitleLabel;
if (![panel.Title isEqualToString:@""]) {
float panelTitleHeight = 0;
if ([MYIntroductionView runningiOS7]) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:TITLE_FONT forKey: NSFontAttributeName];
panelTitleHeight = [panel.Title boundingRectWithSize:CGSizeMake(self.ContentScrollView.frame.size.width - 20, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size.height;
panelTitleHeight = ceilf(panelTitleHeight);
}
else {
panelTitleHeight = [panel.Title sizeWithFont:TITLE_FONT constrainedToSize:CGSizeMake(self.ContentScrollView.frame.size.width - 20, 100) lineBreakMode:NSLineBreakByWordWrapping].height;
}
panelTitleLabelFrame = CGRectMake(10, panelContentHeight+5, self.ContentScrollView.frame.size.width - 20, panelTitleHeight);
panelTitleLabel = [[UILabel alloc] initWithFrame:panelTitleLabelFrame];
panelTitleLabel.text = panel.Title;
panelTitleLabel.numberOfLines = 0;
panelTitleLabel.font = TITLE_FONT;
panelTitleLabel.textColor = TITLE_TEXT_COLOR;
panelTitleLabel.backgroundColor = [UIColor clearColor];
panelTitleLabel.textAlignment = NSTextAlignmentCenter;
panelTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// Add a drop shadow to the title text
/*
panelTitleLabel.layer.shadowColor = [[UIColor blackColor]CGColor];
panelTitleLabel.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
panelTitleLabel.layer.shadowOpacity = 1.0f;
panelTitleLabel.layer.shadowRadius = 1.0f;
*/
}
else {
panelTitleLabelFrame = CGRectMake(10, panelContentHeight+5, self.ContentScrollView.frame.size.width - 20, 0);
panelTitleLabel = [[UILabel alloc] initWithFrame:panelTitleLabelFrame];
}
[panelView addSubview:panelTitleLabel];
//Build description container;
UILabel *panelDescriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.ContentScrollView.frame.size.width, 0)];
panelDescriptionLabel.backgroundColor = [UIColor clearColor];
panelDescriptionLabel.textAlignment = NSTextAlignmentCenter;
panelDescriptionLabel.textColor = DESCRIPTION_TEXT_COLOR;
panelDescriptionLabel.font = DESCRIPTION_FONT;
panelDescriptionLabel.numberOfLines = 0;
panelDescriptionLabel.text = panel.Description;
[panelDescriptionLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[panelView addSubview:panelDescriptionLabel];
// Add a drop shadow to the description text
/*
panelDescriptionTextView.userInteractionEnabled = false;
panelDescriptionTextView.layer.shadowColor = [[UIColor blackColor]CGColor];
panelDescriptionTextView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
panelDescriptionTextView.layer.shadowOpacity = 1.0f;
panelDescriptionTextView.layer.shadowRadius = 1.0f;
*/
//Gather a few layout parameters
//Get the maximum size the description text could be (screenHeight-panelParentContainerOrigin - footersize)
CGFloat maxScrollViewHeight = self.frame.size.height - self.ContentScrollView.frame.origin.y - (36+PAGE_CONTROL_PADDING);
float panelDescriptionHeight = 0;
if ([MYIntroductionView runningiOS7]) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *stringAttributes = @{NSFontAttributeName:DESCRIPTION_FONT, NSParagraphStyleAttributeName:paragraphStyle};
panelDescriptionHeight = [panel.Description boundingRectWithSize:CGSizeMake(self.ContentScrollView.frame.size.width - 20, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size.height;
panelDescriptionHeight = ceilf(panelDescriptionHeight);
}
else {
panelDescriptionHeight = [panel.Description sizeWithFont:DESCRIPTION_FONT constrainedToSize:CGSizeMake(self.ContentScrollView.frame.size.width - 20, 100) lineBreakMode:NSLineBreakByWordWrapping].height;
}
panelDescriptionHeight = panelDescriptionHeight + 10;
int contentWrappedScrollViewHeight = 0;
if ((panelContentHeight + panelDescriptionHeight + panelTitleLabelFrame.size.height) > maxScrollViewHeight) {
contentWrappedScrollViewHeight = maxScrollViewHeight;
panelContentHeight = contentWrappedScrollViewHeight-panelDescriptionHeight - panelTitleLabelFrame.size.height - 10;
}
else if ((panelContentHeight+panelDescriptionHeight + panelTitleLabelFrame.size.height) <= maxScrollViewHeight){
contentWrappedScrollViewHeight = panelContentHeight + panelTitleLabelFrame.size.height + panelDescriptionHeight;
}
panelView.frame = CGRectMake(*xIndex, 0, self.ContentScrollView.frame.size.width, contentWrappedScrollViewHeight);
//Build panel container
panel.PanelContentView.frame = (CGRect) {
.origin = CGPointMake((panelView.frame.size.width-panelContentWidth)/2, 0),
.size = CGSizeMake(panelContentWidth, panelContentHeight),
};
//panel.PanelContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
//panel.PanelContentView.autoresizesSubviews = YES;
[panelView addSubview:panel.PanelContentView];
//Update frames based on the new/scaled image size we just gathered
panelTitleLabel.frame = CGRectMake(10, panelContentHeight + 5, panelTitleLabel.frame.size.width, panelTitleLabel.frame.size.height);
panelDescriptionLabel.frame = CGRectMake(0, panelContentHeight + panelTitleLabel.frame.size.height + 5, self.ContentScrollView.frame.size.width, panelDescriptionHeight);
//Update xIndex
*xIndex += self.ContentScrollView.frame.size.width;
return panelView;
}
-(void)appendCloseViewAtXIndex:(CGFloat*)xIndex{
UIView *closeView = [[UIView alloc] initWithFrame:CGRectMake(*xIndex, 0, self.frame.size.width, 400)];
[self.ContentScrollView addSubview:closeView];
*xIndex += self.ContentScrollView.frame.size.width;
}
-(void)showPanelAtPageControl {
LastPanelIndex = self.PageControl.currentPage;
self.CurrentPanelIndex = self.PageControl.currentPage;
//Format and show new content
[self setContentScrollViewHeightForPanelIndex:self.CurrentPanelIndex animated:YES];
[self makePanelVisibleAtIndex:(NSInteger)self.CurrentPanelIndex];
[self.ContentScrollView setContentOffset:CGPointMake(self.CurrentPanelIndex * 320, 0) animated:YES];
//Call Back, if applicable
if (LastPanelIndex != self.CurrentPanelIndex) { //Keeps from making the callback when just bouncing and not actually changing pages
if ([(id)delegate respondsToSelector:@selector(introductionDidChangeToPanel:withIndex:)]) {
[delegate introductionDidChangeToPanel:Panels[self.CurrentPanelIndex] withIndex:self.CurrentPanelIndex];
}
}
}
-(void)buildFooterView{
//Build Page Control
if (self.device == 1) {
self.PageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, (self.ContentScrollView.frame.origin.y + self.ContentScrollView.frame.size.height + PAGE_CONTROL_PADDING), self.frame.size.width, 36)];
} else {
self.PageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.frame.size.width - 185)/2, (self.ContentScrollView.frame.origin.y + self.ContentScrollView.frame.size.height + PAGE_CONTROL_PADDING), 185, 36)];
}
[self.PageControl setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.PageControl addTarget:self action:@selector(showPanelAtPageControl) forControlEvents:UIControlEventValueChanged];
self.PageControl.numberOfPages = Panels.count;
[self addSubview:self.PageControl];
//Build Skip Button
if (LanguageDirection == MYLanguageDirectionRightToLeft) {
self.SkipButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.PageControl.frame.origin.y, 80, self.PageControl.frame.size.height)];
self.PageControl.currentPage = panelViews.count - 1;
}
else {
// self.SkipButton = [[UIButton alloc] initWithFrame:CGRectMake(self.ContentScrollView.frame.size.width - 80, self.PageControl.frame.origin.y, 80, self.PageControl.frame.size.height)];
self.SkipButton = [[UIButton alloc] initWithFrame:CGRectMake(self.ContentScrollView.frame.size.width - 80, self.PageControl.frame.origin.y, 80, self.PageControl.frame.size.height)];
}
[self.SkipButton setAutoresizingMask: UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[self.SkipButton setTitle:NSLocalizedString(@"Skip", nil) forState:UIControlStateNormal];
[self.SkipButton addTarget:self action:@selector(skipIntroduction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.SkipButton];
}
-(void)setContentScrollViewHeightForPanelIndex:(NSInteger)panelIndex animated:(BOOL)animated{
CGFloat newPanelHeight = [panelViews[panelIndex] frame].size.height;
if (animated){
[UIView animateWithDuration:0.3 animations:^{
self.ContentScrollView.frame = CGRectMake(self.ContentScrollView.frame.origin.x, self.ContentScrollView.frame.origin.y, self.ContentScrollView.frame.size.width, newPanelHeight);
self.PageControl.frame = CGRectMake(self.PageControl.frame.origin.x, (self.ContentScrollView.frame.origin.y + self.ContentScrollView.frame.size.height + PAGE_CONTROL_PADDING), self.PageControl.frame.size.width, self.PageControl.frame.size.height);
self.SkipButton.frame = CGRectMake(self.SkipButton.frame.origin.x, (self.ContentScrollView.frame.origin.y + self.ContentScrollView.frame.size.height + PAGE_CONTROL_PADDING), self.SkipButton.frame.size.width, self.SkipButton.frame.size.height);
}];
}
else {
self.ContentScrollView.frame = CGRectMake(self.ContentScrollView.frame.origin.x, self.ContentScrollView.frame.origin.y, self.ContentScrollView.frame.size.width, newPanelHeight);
self.PageControl.frame = CGRectMake(self.PageControl.frame.origin.x, (self.ContentScrollView.frame.origin.y + self.ContentScrollView.frame.size.height + PAGE_CONTROL_PADDING), self.PageControl.frame.size.width, self.PageControl.frame.size.height);
self.SkipButton.frame = CGRectMake(self.SkipButton.frame.origin.x, (self.ContentScrollView.frame.origin.y + self.ContentScrollView.frame.size.height + PAGE_CONTROL_PADDING), self.SkipButton.frame.size.width, self.SkipButton.frame.size.height);
}
self.ContentScrollView.contentSize = CGSizeMake(self.ContentScrollView.contentSize.width, newPanelHeight);
}
#pragma mark - Header Content
-(void)setHeaderText:(NSString *)headerText{
self.HeaderLabel.hidden = NO;
self.HeaderImageView.hidden = YES;
self.HeaderLabel.text = headerText;
}
-(void)setHeaderImage:(UIImage *)headerImage {
self.HeaderLabel.hidden = YES;
self.HeaderImageView.hidden = NO;
self.HeaderImageView.image = headerImage;
}
#pragma mark - Introduction Content
-(void)setBackgroundImage:(UIImage *)backgroundImage{
self.BackgroundImageView.image = backgroundImage;
[self.BackgroundImageView setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
}
-(void)setBackgroundColor:(UIColor *)backgroundColor {
self.BackgroundImageView.backgroundColor = backgroundColor;
[self.BackgroundImageView setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
}
#pragma mark - Show/Hide
-(void)showInView:(UIView *)view animateDuration:(CGFloat)duration{
//Add introduction view
self.alpha = 0;
[view addSubview:self];
//Fade in
[UIView animateWithDuration:duration animations:^{
self.alpha = 1;
}];
}
-(void)hideWithFadeOutDuration:(CGFloat)duration{
//Fade out
[UIView animateWithDuration:duration animations:^{
self.alpha = 0;
} completion:nil];
}
-(void)makePanelVisibleAtIndex:(NSInteger)panelIndex{
if (LanguageDirection == MYLanguageDirectionLeftToRight) {
[UIView animateWithDuration:0.3 animations:^{
for (int ii = 0; ii < panelViews.count; ii++) {
if (ii == panelIndex) {
[panelViews[ii] setAlpha:1];
}
else {
[panelViews[ii] setAlpha:0];
}
}
}];
}
else {
[UIView animateWithDuration:0.3 animations:^{
for (int ii = panelViews.count-1; ii > 0; ii--) {
if (ii == panelIndex) {
[panelViews[ii] setAlpha:1];
}
else {
[panelViews[ii] setAlpha:0];
}
}
}];
}
}
-(void)skipIntroduction{
if ([(id)delegate respondsToSelector:@selector(introductionDidFinishWithType:)]) {
[delegate introductionDidFinishWithType:MYFinishTypeSkipButton];
}
[self hideWithFadeOutDuration:0.3];
}
#pragma mark - UIScrollView Delegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (LanguageDirection == MYLanguageDirectionLeftToRight) {
self.CurrentPanelIndex = scrollView.contentOffset.x/self.ContentScrollView.frame.size.width;
//remove self if you are at the end of the introduction
if (self.CurrentPanelIndex == (panelViews.count)) {
if ([(id)delegate respondsToSelector:@selector(introductionDidFinishWithType:)]) {
[delegate introductionDidFinishWithType:MYFinishTypeSwipeOut];
}
}
else {
//Update Page Control
LastPanelIndex = self.PageControl.currentPage;
self.PageControl.currentPage = self.CurrentPanelIndex;
//Format and show new content
[self setContentScrollViewHeightForPanelIndex:self.CurrentPanelIndex animated:YES];
[self makePanelVisibleAtIndex:(NSInteger)self.CurrentPanelIndex];
//Call Back, if applicable
if (LastPanelIndex != self.CurrentPanelIndex) { //Keeps from making the callback when just bouncing and not actually changing pages
if ([(id)delegate respondsToSelector:@selector(introductionDidChangeToPanel:withIndex:)]) {
[delegate introductionDidChangeToPanel:Panels[self.CurrentPanelIndex] withIndex:self.CurrentPanelIndex];
}
}
}
}
else if(LanguageDirection == MYLanguageDirectionRightToLeft){
self.CurrentPanelIndex = (scrollView.contentOffset.x-320)/self.ContentScrollView.frame.size.width;
//remove self if you are at the end of the introduction
if (self.CurrentPanelIndex == -1) {
if ([(id)delegate respondsToSelector:@selector(introductionDidFinishWithType:)]) {
[delegate introductionDidFinishWithType:MYFinishTypeSwipeOut];
}
}
else {
//Update Page Control
LastPanelIndex = self.PageControl.currentPage;
self.PageControl.currentPage = self.CurrentPanelIndex;
//Format and show new content
[self setContentScrollViewHeightForPanelIndex:self.CurrentPanelIndex animated:YES];
[self makePanelVisibleAtIndex:(NSInteger)self.CurrentPanelIndex];
//Call Back, if applicable
if (LastPanelIndex != self.CurrentPanelIndex) { //Keeps from making the callback when just bouncing and not actually changing pages
if ([(id)delegate respondsToSelector:@selector(introductionDidChangeToPanel:withIndex:)]) {
[delegate introductionDidChangeToPanel:Panels[self.CurrentPanelIndex] withIndex:self.CurrentPanelIndex];
}
}
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (LanguageDirection == MYLanguageDirectionLeftToRight) {
if (self.CurrentPanelIndex == (panelViews.count - 1)) {
self.alpha = ((self.ContentScrollView.frame.size.width*panelViews.count)-self.ContentScrollView.contentOffset.x)/self.ContentScrollView.frame.size.width;
}
}
else if (LanguageDirection == MYLanguageDirectionRightToLeft){
if (self.CurrentPanelIndex == 0) {
self.alpha = self.ContentScrollView.contentOffset.x/self.ContentScrollView.frame.size.width;
}
}
}
#pragma mark - Support Methods
+(BOOL)runningiOS7{
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if (currSysVer.floatValue >= 7.0) {
return YES;
}
return NO;
}
@end