引導(dǎo)頁功能簡介
方式一:
判斷程序是否首次啟動,如果是將guidepageviewcontroller作為窗口的根視圖控制器。guidepageviewcontroller有三個子控件:一個uiscrollview、一個uipagecontrol、一個uibutton(默認(rèn)隱藏),uiscrollview有多個uiimageview子控件,當(dāng)滾動到最后一頁uibutton展示,點擊立即體驗然后將窗口的根視圖控制器設(shè)置為uitabbarcontroller;
方式二:
也可以直接將根視圖控制器設(shè)置為uitabbarcontroller, 然后在第一個導(dǎo)航控制器的視圖上展示引導(dǎo)頁視圖,當(dāng)點擊立即體驗再將引導(dǎo)頁視圖隱藏掉即可。
示例代碼
1
2
3
4
5
6
7
8
|
@implementation appdelegate - ( bool )application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // 首次啟動應(yīng)用程序時進(jìn)入到引導(dǎo)頁頁面(暫時沒有判斷,可通過nsuserdefault實現(xiàn)) self.window.rootviewcontroller = [[guidepageviewcontroller alloc] init]; return yes; } @end |
引導(dǎo)頁視圖控制器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,這里簡單實現(xiàn) [uiapplication sharedapplication].keywindow.rootviewcontroller = [[viewcontroller alloc] init]; } @end |
上述代碼中的圖片名稱是寫死在guidepageviewcontroller中的,根視圖控制器也是寫死的,如果其他app想要復(fù)用該功能,就要進(jìn)入該代碼修改這兩哥地方,為了不修改一行代碼就可以使用該功能,可以將這兩個參數(shù)提取出來,初始化該控制器時作為參數(shù)傳遞
封裝示例代碼
初始化時以參數(shù)的形式將圖片和根視圖控制器傳遞給引導(dǎo)頁視圖控制器
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 |
在初始化方法中將引導(dǎo)頁圖片和根視圖控制器保存起來,使用self.images.count獲取引導(dǎo)頁數(shù)量,引導(dǎo)頁圖片直接從images數(shù)組中獲取
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引導(dǎo)頁
1、創(chuàng)建出所有引導(dǎo)頁eaintropage
2、創(chuàng)建引導(dǎo)頁視圖eaintroview 并設(shè)置代理
3、顯示引導(dǎo)頁視圖
創(chuàng)建出所有引導(dǎo)頁eaintropage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// basic: 標(biāo)題和描述 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" ]; |
創(chuàng)建引導(dǎo)頁視圖eaintroview 并設(shè)置代理
eaintroview *intro = [[eaintroview alloc] initwithframe:self.view.bounds andpages:@[page1,page2,page3,page4]];
intro.delegate=self;
顯示引導(dǎo)頁視圖
[intro showinview:self.view animateduration:0.0];
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。