iOS App引导页开发教程

时间:2021-10-12 03:19:42

引导页功能简介

方式一:
判断程序是否首次启动,如果是将guidepageviewcontroller作为窗口的根视图控制器。guidepageviewcontroller有三个子控件:一个uiscrollview、一个uipagecontrol、一个uibutton(默认隐藏),uiscrollview有多个uiimageview子控件,当滚动到最后一页uibutton展示,点击立即体验然后将窗口的根视图控制器设置为uitabbarcontroller;

方式二:
也可以直接将根视图控制器设置为uitabbarcontroller, 然后在第一个导航控制器的视图上展示引导页视图,当点击立即体验再将引导页视图隐藏掉即可。

示例代码

?
1
2
3
4
5
6
7
8
@implementation appdelegate
 
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
 // 首次启动应用程序时进入到引导页页面(暂时没有判断,可通过nsuserdefault实现)
 self.window.rootviewcontroller = [[guidepageviewcontroller alloc] init];
 return yes;
}
@end

引导页视图控制器guidepageviewcontroller

 

?
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
#import "guidepageviewcontroller.h"
#import "viewcontroller.h"
 
#define kscreenwidth ([uiscreen mainscreen].bounds.size.width)
#define kscreenheight ([uiscreen mainscreen].bounds.size.height)
#define kguidepagecount 4
 
@interface guidepageviewcontroller () <uiscrollviewdelegate>
@property (weak, nonatomic) uipagecontrol *pagecontrol;
@property (weak, nonatomic) uibutton *startappbutton;
@end
 
@implementation guidepageviewcontroller
 
- (void)viewdidload {
 [super viewdidload];
 
 // uiscrollview
 uiscrollview *guidepagescrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 0, kscreenwidth, kscreenheight)];
 guidepagescrollview.contentsize = cgsizemake(kscreenwidth * kguidepagecount, 0);
 guidepagescrollview.showshorizontalscrollindicator = no;
 guidepagescrollview.pagingenabled = yes;
 guidepagescrollview.bounces = no;
 guidepagescrollview.delegate = self;
 for (int i = 0; i < kguidepagecount; i++) {
  uiimageview *guideimageview = [[uiimageview alloc] initwithframe:cgrectmake(kscreenwidth * i, 0, kscreenwidth, kscreenheight)];
  guideimageview.image = [uiimage imagenamed:[nsstring stringwithformat:@"guide-page-%d", i + 1]];
  [guidepagescrollview addsubview:guideimageview];
 }
 [self.view addsubview:guidepagescrollview];
 
 // uipagecontrol(分页)
 uipagecontrol *pagecontrol = [[uipagecontrol alloc] initwithframe:cgrectmake((kscreenwidth - 100) / 2, kscreenheight- 50, 100, 30)];
 pagecontrol.numberofpages = kguidepagecount;
 pagecontrol.currentpage = 0;
 pagecontrol.currentpageindicatortintcolor = [uicolor greencolor];
 [self.view addsubview:pagecontrol];
 self.pagecontrol = pagecontrol;
 
 // uibutton(立即体验)
 uibutton *startappbutton = [uibutton buttonwithtype:uibuttontypecustom];
 startappbutton.frame = cgrectmake((kscreenwidth - 100) / 2, 550, 100, 40);
 [startappbutton settitle:@"立即体验" forstate:uicontrolstatenormal];
 startappbutton.backgroundcolor = [uicolor graycolor];
 [startappbutton addtarget:self action:@selector(startappaction) forcontrolevents:uicontroleventtouchupinside];
 startappbutton.hidden = yes;
 [self.view addsubview:startappbutton];
 _startappbutton = startappbutton;
}
 
- (void)scrollviewdidscroll:(uiscrollview *)scrollview {
 nsinteger currentpage = scrollview.contentoffset.x / kscreenwidth;
 self.pagecontrol.currentpage = currentpage;
 if (currentpage == (kguidepagecount - 1)) {
  self.startappbutton.hidden = no;
 }
}
 
- (void)startappaction {
 // 根视图控制器一般是uitabbarcontroller,这里简单实现
 [uiapplication sharedapplication].keywindow.rootviewcontroller = [[viewcontroller alloc] init];
}
@end

上述代码中的图片名称是写死在guidepageviewcontroller中的,根视图控制器也是写死的,如果其他app想要复用该功能,就要进入该代码修改这两哥地方,为了不修改一行代码就可以使用该功能,可以将这两个参数提取出来,初始化该控制器时作为参数传递

封装示例代码

初始化时以参数的形式将图片和根视图控制器传递给引导页视图控制器

?
1
2
3
4
5
6
7
@implementation appdelegate
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
 viewcontroller *viewcontroller = [[viewcontroller alloc] init];
 self.window.rootviewcontroller = [[guidepageviewcontroller alloc] initwithimages:@[@"guide-page-1", @"guide-page-2", @"guide-page-3", @"guide-page-4"] rootviewcontroller:viewcontroller];
 return yes;
}
@end
?
1
2
3
4
5
6
#import <uikit/uikit.h>
@interface guidepageviewcontroller : uiviewcontroller
 
- (instancetype)initwithimages:(nsarray *)images rootviewcontroller:(uiviewcontroller *)rootviewcontroller;
 
@end

在初始化方法中将引导页图片和根视图控制器保存起来,使用self.images.count获取引导页数量,引导页图片直接从images数组中获取

 

?
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
#import "guidepageviewcontroller.h"
#import "viewcontroller.h"
 
#define kscreenwidth ([uiscreen mainscreen].bounds.size.width)
#define kscreenheight ([uiscreen mainscreen].bounds.size.height)
 
@interface guidepageviewcontroller () <uiscrollviewdelegate>
@property (weak, nonatomic) uipagecontrol *pagecontrol;
@property (weak, nonatomic) uibutton *startappbutton;
 
@property (strong, nonatomic) nsarray *images;
@property (strong, nonatomic) uiviewcontroller *rootviewcontroller;
@end
 
@implementation guidepageviewcontroller
 
- (instancetype)initwithimages:(nsarray *)images rootviewcontroller:(uiviewcontroller *)rootviewcontroller {
 if (self = [super init]) {
  _images = images;
  _rootviewcontroller = rootviewcontroller;
 }
 
 return self;
}
 
- (void)viewdidload {
 [super viewdidload];
 
 // uiscrollview
 uiscrollview *guidepagescrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 0, kscreenwidth, kscreenheight)];
 guidepagescrollview.contentsize = cgsizemake(kscreenwidth * self.images.count, 0);
 guidepagescrollview.showshorizontalscrollindicator = no;
 guidepagescrollview.pagingenabled = yes;
 guidepagescrollview.bounces = no;
 guidepagescrollview.delegate = self;
 for (int i = 0; i < self.images.count; i++) {
  uiimageview *guideimageview = [[uiimageview alloc] initwithframe:cgrectmake(kscreenwidth * i, 0, kscreenwidth, kscreenheight)];
  guideimageview.image = [uiimage imagenamed:self.images[i]];
  [guidepagescrollview addsubview:guideimageview];
 }
 [self.view addsubview:guidepagescrollview];
 
 // uipagecontrol
 uipagecontrol *pagecontrol = [[uipagecontrol alloc] initwithframe:cgrectmake((kscreenwidth - 100) / 2, kscreenheight- 50, 100, 30)];
 pagecontrol.numberofpages = self.images.count;
 pagecontrol.currentpage = 0;
 pagecontrol.currentpageindicatortintcolor = [uicolor greencolor];
 [self.view addsubview:pagecontrol];
 self.pagecontrol = pagecontrol;
 
 uibutton *startappbutton = [uibutton buttonwithtype:uibuttontypecustom];
 startappbutton.frame = cgrectmake((kscreenwidth - 100) / 2, 550, 100, 40);
 [startappbutton settitle:@"立即体验" forstate:uicontrolstatenormal];
 startappbutton.backgroundcolor = [uicolor graycolor];
 [startappbutton addtarget:self action:@selector(startappaction) forcontrolevents:uicontroleventtouchupinside];
 startappbutton.hidden = yes;
 [self.view addsubview:startappbutton];
 _startappbutton = startappbutton;
}
 
- (void)scrollviewdidscroll:(uiscrollview *)scrollview {
 nsinteger currentpage = scrollview.contentoffset.x / kscreenwidth;
 self.pagecontrol.currentpage = currentpage;
 if (currentpage == (self.images.count - 1)) {
  self.startappbutton.hidden = no;
 }
}
 
 
- (void)startappaction {
 [uiapplication sharedapplication].keywindow.rootviewcontroller = self.rootviewcontroller;
}
@end

终极解决方案

直接使用github上的开源功能即可github引导页

1、创建出所有引导页eaintropage
2、创建引导页视图eaintroview 并设置代理
3、显示引导页视图

创建出所有引导页eaintropage

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// basic: 标题和描述
eaintropage *page1 = [eaintropage page];
page1.title = @"hello world";
page1.desc = sampledescription1;
 
// custom
eaintropage *page2 = [eaintropage page];
page2.title = @"this is page 2";
page2.titlefont = [uifont fontwithname:@"georgia-bolditalic" size:20];
page2.titlepositiony = 220;
page2.desc = sampledescription2;
page2.descfont = [uifont fontwithname:@"georgia-italic" size:18];
page2.descpositiony = 200;
page2.titleiconview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"title2"]];
page2.titleiconpositiony = 100;
 
// custom view from nib
eaintropage *page3 = [eaintropage pagewithcustomviewfromnibnamed:@"intropage"];
page3.bgimage = [uiimage imagenamed:@"bg2"];

创建引导页视图eaintroview 并设置代理
eaintroview *intro = [[eaintroview alloc] initwithframe:self.view.bounds andpages:@[page1,page2,page3,page4]];
intro.delegate=self;

显示引导页视图
[intro showinview:self.view animateduration:0.0];

iOS App引导页开发教程

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。