每一個(gè)APP都會(huì)用到APP引導(dǎo)頁(yè),分量不重但是不可缺少技竟,不論是APP的首次安裝還是版本的更新窿凤,首先展現(xiàn)給用戶眼前的也就只有它了,當(dāng)然這里講的不是APP引導(dǎo)頁(yè)的美化而是APP引導(dǎo)頁(yè)的高度集成黔宛,一行代碼搞定APP引導(dǎo)頁(yè)是不是太夸張近刘?下面我們就來(lái)看一下吧!
一臀晃、先上GitHub連接觉渴,給大家節(jié)省時(shí)間:
GitHub地址:https://github.com/dingding3w/DHGuidePageHUD
二、效果圖展示:
三徽惋、一行代碼搞定APP引導(dǎo)頁(yè)的創(chuàng)建(真的是一行代碼案淋,只要填入?yún)?shù)即可):
/**
- DHGuidePageHUD
- @param frame 位置大小
- @param imageArray 引導(dǎo)頁(yè)圖片數(shù)組
- @param isHidden 開始體驗(yàn)按鈕是否隱藏(YES:隱藏-引導(dǎo)頁(yè)完成自動(dòng)進(jìn)入APP首頁(yè); NO:不隱藏-引導(dǎo)頁(yè)完成點(diǎn)擊開始體驗(yàn)按鈕進(jìn)入APP主頁(yè))
- @return DHGuidePageHUD對(duì)象
*/
- (instancetype)dh_initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray buttonIsHidden:(BOOL)isHidden;
四、實(shí)現(xiàn)方式跟原理:
APP引導(dǎo)頁(yè)是由多張圖片組成险绘,引導(dǎo)頁(yè)的圖片在美工切圖的時(shí)候會(huì)給你的這個(gè)不用擔(dān)心踢京,并且使多張圖片實(shí)現(xiàn)輪番滾動(dòng),每拖動(dòng)一次會(huì)跳轉(zhuǎn)到下一張引導(dǎo)頁(yè)圖片宦棺,那我們必定會(huì)想到可以滾動(dòng)的視圖瓣距,這里我用的是UIScrollView
(1)創(chuàng)建一個(gè)繼承于UIView的類;
(2)將設(shè)置引導(dǎo)視圖的scrollview添加到UIView上代咸;
// 設(shè)置引導(dǎo)視圖的scrollview
UIScrollView guidePageView = [[UIScrollView alloc]initWithFrame:frame];
[guidePageView setBackgroundColor:[UIColor lightGrayColor]];
// 根據(jù)傳入圖片數(shù)組中的個(gè)數(shù)來(lái)計(jì)算UIScrollView的contentSize
[guidePageView setContentSize:CGSizeMake(DDScreenWimageArray.count, DDScreenH)];
[guidePageView setBounces:NO];
[guidePageView setPagingEnabled:YES];
[guidePageView setShowsHorizontalScrollIndicator:NO];
[guidePageView setDelegate:self];
[self addSubview:guidePageView];
(3)添加引導(dǎo)頁(yè)右上角的跳過(guò)按鈕(因?yàn)樘^(guò)按鈕一直停留在屏幕的右上角蹈丸,所以這里也要添加的UIView上并且要顯示到UIScrollView的上方,以便加強(qiáng)用戶交互)
// 設(shè)置引導(dǎo)頁(yè)上的跳過(guò)按鈕
UIButton skipButton = [[UIButton alloc]initWithFrame:CGRectMake(DDScreenW0.8, DDScreenW*0.1, 50, 25)];
[skipButton setTitle:@"跳過(guò)" forState:UIControlStateNormal];
[skipButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[skipButton setBackgroundColor:[UIColor grayColor]];
[skipButton.layer setCornerRadius:5.0];
// 添加點(diǎn)擊事件(該事件保持與開始體驗(yàn)按鈕,自動(dòng)跳轉(zhuǎn)APP同步;目的是減少代碼的書寫量與其他方式保持同步樣式)
[skipButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:skipButton];
(4)添加引導(dǎo)頁(yè)上的多張圖片(如果您設(shè)置的isHidden參數(shù)為NO的話,會(huì)在最后一張引導(dǎo)頁(yè)圖片上添加"開始體驗(yàn)按鈕"; 如果您設(shè)置isHidden的參數(shù)為YES的話,當(dāng)滑動(dòng)到最后一張APP引導(dǎo)頁(yè)的時(shí)候會(huì)自動(dòng)進(jìn)入APP相關(guān)首頁(yè))
// 添加在引導(dǎo)視圖上的多張引導(dǎo)圖片
for (int i=0; i<imageArray.count; i++) {
UIImageView imageView = [[UIImageView alloc]initWithFrame:CGRectMake(DDScreenWi, 0, DDScreenW, DDScreenH)];
imageView.image = imageArray[i];
[guidePageView addSubview:imageView];
// 設(shè)置在最后一張圖片上顯示進(jìn)入體驗(yàn)按鈕
if (i == imageArray.count-1 && isHidden == NO) {
[imageView setUserInteractionEnabled:YES];
UIButton *startButton = [[UIButton alloc]initWithFrame:CGRectMake(DDScreenW*0.3, DDScreenH*0.8, DDScreenW*0.4, DDScreenH*0.08)];
[startButton setTitle:@"開始體驗(yàn)" forState:UIControlStateNormal];
[startButton setTitleColor:[UIColor colorWithRed:164/255.0 green:201/255.0 blue:67/255.0 alpha:1.0] forState:UIControlStateNormal];
[startButton.titleLabel setFont:[UIFont systemFontOfSize:21]];
[startButton setBackgroundImage:[UIImage imageNamed:@"GuideImage.bundle/guideImage_button_backgound"] forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:startButton];
}
}
(5)點(diǎn)擊事件的實(shí)現(xiàn),這里使用UIView動(dòng)畫以及延時(shí)的方式來(lái)使用APP引導(dǎo)頁(yè)進(jìn)入APP相關(guān)首頁(yè)時(shí)的淡入淡出效果:
- (void)buttonClick:(UIButton *)button {
[UIView animateWithDuration:DDHidden_TIME animations:^{
self.alpha = 0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DDHidden_TIME * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self performSelector:@selector(removeGuidePageHUD) withObject:nil afterDelay:1];
});
}];
}
(6)最后在APP引導(dǎo)頁(yè)跳轉(zhuǎn)APP首頁(yè)的時(shí)候記得remove掉當(dāng)前APP引導(dǎo)頁(yè)逻杖,防止產(chǎn)生不必要的麻煩(最好remove掉??):
1 - (void)removeGuidePageHUD {2 [self removeFromSuperview]; 3 }
五慨默、可能會(huì)用到的代碼:
這里我使用NSUserDefaults判斷程序是否第一次啟動(dòng)(其他方法也可以,這里把代碼給大家粘貼出來(lái))
1 if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {2 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:BOOLFORKEY]; 3 // 在這里寫初始化圖片數(shù)組和DHGuidePageHUD庫(kù)引導(dǎo)頁(yè)的代碼4 }
以上就是我對(duì)DHGuidePageHUD這個(gè)APP引導(dǎo)頁(yè)第三方SDK的理解與講解弧腥,全部代碼已經(jīng)上傳至GitHub鏈接厦取,詳見閱讀原文,希望大家相互補(bǔ)充相互學(xué)習(xí)管搪。