iOS之引導頁

1.每個應用程序剛安裝后啟動的時候都會有一個引導頁,用于引導用戶使用APP,怎么實現呢,首先來把引導頁寫好,說白了,它就是一個scrollview,UIPageControl,按鈕或者手勢(進入主界面), 這里我用的是手勢點擊進入主界面,(當然 也你可以添加按鈕)

/**
 * 創建基礎控件
 */
-(void)creatWelcomeView
{
   
    /*
     添加滾動視圖
     */
    _scrollview = [[UIScrollView alloc]initWithFrame:screen];
//    _scrollview.backgroundColor = [UIColor greenColor];
    _scrollview.pagingEnabled = YES;
    _scrollview.contentSize = CGSizeMake(WIDTH*_images.count, HEIGHT);
    _scrollview.delegate = self;
    [self.view addSubview:_scrollview];
    
    for (int i =0; i<_images.count; i++) {
        _imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[_images objectAtIndex:i]]];
//        NSLog(@"%@",[NSString stringWithFormat:@"%d.jpeg",i]);
        _imageview.frame = CGRectMake(i*WIDTH, 0, WIDTH, HEIGHT);
        _imageview.userInteractionEnabled = YES;
        [_imageview setTag:100+i];
        [_scrollview addSubview:_imageview];
        [_imageViews addObject:_imageview];
    }
    
    /*
     添加分頁控制
     */
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, HEIGHT-80, WIDTH, WIDTH/4)];
    _pageControl.backgroundColor = [UIColor clearColor];
    _pageControl.numberOfPages = _images.count;
    _pageControl.tintColor = [UIColor colorWithWhite:255.0/254 alpha:1.0];
    _pageControl.currentPageIndicatorTintColor= [UIColor colorWithWhite:255.0/250 alpha:0.8];
    [_pageControl addTarget:self action:@selector(pageControlClicked) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    /**
     *可以在Imageview上添加你要的處理的事件,通過在imageview添加button 或者 手勢 來處理事件
     */
    /*
     *這里我為最后一張圖片添加點擊手勢 (進入下個視圖控制器)
     */
    UITapGestureRecognizer *tapGo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapdo:)];
    /*
     遍歷獲取最后一個imageview
     */
    for (UIImageView *view in _imageViews) {
        if (view.tag-100 == _images.count - 1) {
            [view addGestureRecognizer:tapGo];
        }
    }
    
}

2.接下來就是關鍵了,就是怎么來判斷這個程序是不是剛安裝呢,在這里我用了NSUserDefaults這個類,它會將數據存到應用里,
NSUserDefaults 可以存儲數據類型(CGflot,NSInteger,BOOL等)和對象(NSData,NSArray,NSString,NSDictionary 等),我用了一個BOOL值來記錄第一次運行,
所以我在Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /**
     * NSUserDefaults 可以存儲數據類型(CGflot,NSInteger,BOOL等)和對象(NSData,NSArray,NSString,NSDictionary 等)
     * 這里利用NSUserDefaults 設置一個bool值來判斷是不是第一次運行
     */
    

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"FirstRun"]) {
        //如果是第一次運行就添加BOOL并賦值
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstRun"];
       
    }else{
       
        
    }
 
    return YES;
}

3.效果圖

Simulator Screen Shot 2016年6月28日 00.15.22.png

Simulator Screen Shot 2016年6月28日 00.15.31.png

Simulator Screen Shot 2016年6月28日 00.15.35.png

Simulator Screen Shot 2016年6月28日 00.15.38.png

Snip20160628_1.png

4.是不是很簡單啊,各位,詳細代碼已上傳github,做了簡單的封裝,只需調用一句話傳入圖片數組即可實現引導頁,[github網址](https://github.com/zlfyuan/WelComeView.git

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容