iOS首次啟動(dòng)程序引導(dǎo)圖

第一次運(yùn)行一個(gè)程序時(shí)候,我們會(huì)看到新手引導(dǎo)頁,就是程序引導(dǎo)圖,除非后來版本升級,或者卸載軟件后重裝才會(huì)再次看到引導(dǎo)頁

我們先來看效果:


Untit.gif

這里我第一次運(yùn)行程序時(shí)候,直接是有引導(dǎo)圖的,后來兩次運(yùn)行都沒有再次出現(xiàn)引導(dǎo)圖。除非下次版本更新之后,或者卸載再次運(yùn)行程序才會(huì)出現(xiàn)引導(dǎo)圖。
先給大家兩張圖片分析一下


Snip20160911_3.png

Snip20160911_2.png

現(xiàn)在來教一下大家如何用代碼實(shí)現(xiàn)引導(dǎo)圖
第一步:在AppDelegate.m文件中
Snip20160910_1.png

代碼呈上

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 1.創(chuàng)建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    // 2.設(shè)置根控制器
    NSString *key = @"CFBundleVersion";
    // 上一次的使用版本(存儲(chǔ)在沙盒中的版本號)
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    // 當(dāng)前軟件的版本號(從Info.plist中獲得)
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    if ([currentVersion isEqualToString:lastVersion]) { // 版本號相同:這次打開和上次打開的是同一個(gè)版本
        self.window.rootViewController = [[LYWTabBarViewController alloc] init];
    } else { // 這次打開的版本和上一次不一樣,顯示新特性
        self.window.rootViewController = [[LYWNewfeatureViewController alloc] init];
        // 將當(dāng)前的版本號存進(jìn)沙盒
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    // 3.顯示窗口
    [self.window makeKeyAndVisible];
    return YES;
}
第二步:在LYWNewfeatureViewController.m 中實(shí)現(xiàn)簡單的輪播圖效果
#import "LYWNewfeatureViewController.h"
#import "LYWTabBarViewController.h"

#define NewfeatureCount 3
@interface LYWNewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIPageControl *pageControl;
@end

@implementation LYWNewfeatureViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupScrollView];
}

//創(chuàng)建UIScrollView并添加圖片
- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:scrollView];
    
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(3*kScreenWidth, 0);
    scrollView.delegate = self;
    
    for (NSInteger i = 0; i < NewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.x = i * kScreenWidth;
        imageView.y = 0;
        imageView.width = kScreenWidth;
        imageView.height = kScreenHeight;
        NSString *name = [NSString stringWithFormat:@"f%ld-5",i+1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        if (i == NewfeatureCount - 1) {
            [self setupStartBtn:imageView];
        }
    }
    
    // 4.添加pageControl:分頁,展示目前看的是第幾頁
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = NewfeatureCount;
    pageControl.backgroundColor = [UIColor redColor];
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = KRGB(189, 189, 189);
    pageControl.centerX = kScreenWidth * 0.5;
    pageControl.centerY = kScreenHeight - 50;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

//左上角的灰色跳過按鈕
-(void)createSkipBt
{
    UIButton *skipBt = [UIButton buttonWithType:UIButtonTypeCustom];
    skipBt.frame = CGRectMake(kScreenWidth - 90, 40, 80, 30);
    skipBt.backgroundColor = [UIColor colorWithRed:0.3 green:0.3f blue:0.3f alpha:0.3];
    [skipBt setTitle:@"跳過" forState:UIControlStateNormal];
    [skipBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    skipBt.layer.cornerRadius = 10;
    skipBt.clipsToBounds = YES;
    skipBt.tag = 10;
    [skipBt addTarget:self action:@selector(BtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:skipBt];
}

//手動(dòng)拖拽結(jié)束時(shí)候調(diào)用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入計(jì)算出頁碼
    self.pageControl.currentPage = (int)(page + 0.5);
    // 1.3四舍五入 1.3 + 0.5 = 1.8 強(qiáng)轉(zhuǎn)為整數(shù)(int)1.8= 1
    // 1.5四舍五入 1.5 + 0.5 = 2.0 強(qiáng)轉(zhuǎn)為整數(shù)(int)2.0= 2
    // 1.6四舍五入 1.6 + 0.5 = 2.1 強(qiáng)轉(zhuǎn)為整數(shù)(int)2.1= 2
    // 0.7四舍五入 0.7 + 0.5 = 1.2 強(qiáng)轉(zhuǎn)為整數(shù)(int)1.2= 1
}

//給最后一張圖片添加 進(jìn)入問醫(yī)生按鈕
- (void)setupStartBtn:(UIImageView *)imgView
{
    imgView.userInteractionEnabled = YES;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"cancel_ask"] forState:UIControlStateNormal];
    btn.size = btn.currentBackgroundImage.size;
    btn.centerX = imgView.width * 0.5;
    btn.centerY = imgView.height * 0.75;
    [btn setTitle:@"進(jìn)入問醫(yī)生" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(BtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
    [imgView addSubview:btn];
}

//進(jìn)入問醫(yī)生按鈕點(diǎn)擊事件
-(void)BtnDidClicked
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[LYWTabBarViewController alloc] init];
}

@end

這樣就??了,其實(shí)很簡單,不過是最近一個(gè)外包項(xiàng)目過來,然后我接手了,給它做一了一個(gè)啟動(dòng)引導(dǎo)圖,閑來沒事,就寫寫這片文章打法自己無聊的時(shí)間,也方便很多初學(xué)者學(xué)習(xí)

備注:

如果有不足或者錯(cuò)誤的地方還望各位讀者批評指正,可以評論留言,筆者收到后第一時(shí)間回復(fù)。

QQ/微信:2366889552 /lan2018yingwei。

簡書號:凡塵一笑:[簡書]

http://www.itdecent.cn/users/0158007b8d17/latest_articles

感謝各位觀眾老爺?shù)拈喿x,如果覺得筆者寫的還湊合,可以關(guān)注或收藏一下,不定期分享一些好玩的實(shí)用的demo給大家。

文/凡塵一笑(簡書作者)

原文鏈接: http://www.itdecent.cn/p/8ae080edb3ea

著作權(quán)歸作者所有,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容