一行代碼搞定APP引導(dǎo)頁的創(chuàng)建(真的是一行代碼翁都,只要填入?yún)?shù)即可):
/**
*? DHGuidePageHUD
*
*? @param frame? ? ? 位置大小
*? @param imageArray 引導(dǎo)頁圖片數(shù)組
*? @param isHidden? 開始體驗按鈕是否隱藏(YES:隱藏-引導(dǎo)頁完成自動進(jìn)入APP首頁; NO:不隱藏-引導(dǎo)頁完成點(diǎn)擊開始體驗按鈕進(jìn)入APP主頁)
*
*? @return DHGuidePageHUD對象
*/
- (instancetype)dh_initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray buttonIsHidden:(BOOL)isHidden;
實現(xiàn)方式跟原理:
APP引導(dǎo)頁是由多張圖片組成,引導(dǎo)頁的圖片在美工切圖的時候會給你的這個不用擔(dān)心,并且使多張圖片實現(xiàn)輪番滾動隔缀,每拖動一次會跳轉(zhuǎn)到下一張引導(dǎo)頁圖片冻璃,那我們必定會想到可以滾動的視圖,這里我用的是UIScrollView
(1)創(chuàng)建一個繼承于UIView的類;
(2)將設(shè)置引導(dǎo)視圖的scrollview添加到UIView上录别;
// 設(shè)置引導(dǎo)視圖的scrollview
UIScrollView *guidePageView = [[UIScrollView alloc]initWithFrame:frame];
[guidePageView setBackgroundColor:[UIColor lightGrayColor]];
// 根據(jù)傳入圖片數(shù)組中的個數(shù)來計算UIScrollView的contentSize
[guidePageView setContentSize:CGSizeMake(DDScreenW*imageArray.count, DDScreenH)];
[guidePageView setBounces:NO];
[guidePageView setPagingEnabled:YES];
[guidePageView setShowsHorizontalScrollIndicator:NO];
[guidePageView setDelegate:self];
[self addSubview:guidePageView];
(3)添加引導(dǎo)頁右上角的跳過按鈕(因為跳過按鈕一直停留在屏幕的右上角耕渴,所以這里也要添加的UIView上并且要顯示到UIScrollView的上方拘悦,以便加強(qiáng)用戶交互)
// 設(shè)置引導(dǎo)頁上的跳過按鈕
UIButton *skipButton = [[UIButton alloc]initWithFrame:CGRectMake(DDScreenW*0.8, DDScreenW*0.1, 50, 25)];
[skipButton setTitle:@"跳過" forState:UIControlStateNormal];
[skipButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[skipButton setBackgroundColor:[UIColor grayColor]];
[skipButton.layer setCornerRadius:5.0];
// 添加點(diǎn)擊事件(該事件保持與開始體驗按鈕,自動跳轉(zhuǎn)APP同步;目的是減少代碼的書寫量與其他方式保持同步樣式)
[skipButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:skipButton];
(4)添加引導(dǎo)頁上的多張圖片(如果您設(shè)置的isHidden參數(shù)為NO的話,會在最后一張引導(dǎo)頁圖片上添加"開始體驗按鈕"; 如果您設(shè)置isHidden的參數(shù)為YES的話,當(dāng)滑動到最后一張APP引導(dǎo)頁的時候會自動進(jìn)入APP相關(guān)首頁)
// 添加在引導(dǎo)視圖上的多張引導(dǎo)圖片
for (int i=0; i<imageArray.count; i++){
? ? ? UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(DDScreenW*i, 0, DDScreenW, DDScreenH)];
? ? ?imageView.image = imageArray[i];
? ? ?[guidePageView addSubview:imageView];
? ? ?// 設(shè)置在最后一張圖片上顯示進(jì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:@"開始體驗" 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)擊事件的實現(xiàn),這里使用UIView動畫以及延時的方式來使用APP引導(dǎo)頁進(jìn)入APP相關(guān)首頁時的淡入淡出效果:
- (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)頁跳轉(zhuǎn)APP首頁的時候記得remove掉當(dāng)前APP引導(dǎo)頁橱脸,防止產(chǎn)生不必要的麻煩(最好remove掉??):
1 - (void)removeGuidePageHUD {
2? ? [self removeFromSuperview];
3 }
可能會用到的代碼:
這里我使用NSUserDefaults判斷程序是否第一次啟動(其他方法也可以础米,這里把代碼給大家粘貼出來)
1 if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {
2? ? ? ? [[NSUserDefaults standardUserDefaults] setBool:YES forKey:BOOLFORKEY];
3? ? ? ? // 在這里寫初始化圖片數(shù)組和DHGuidePageHUD庫引導(dǎo)頁的代碼
4 }