iOS引導頁

每個客戶端都會有自己的引導頁,啓動app時,通過引導頁簡單介紹客戶端的個性化功能。
好了廢話不多說,直接上代碼:
AppDelegate.h文件:
#import "AppDelegate.h"
#import "GuideVC.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    ViewController *vc = [[ViewController alloc] init];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    
    if([GuideVC everLaunched] == NO)//啓動引導頁
    {
        GuideVC *tGV = [[GuideVC alloc]init];
        self.window.rootViewController = tGV;
        [self.window makeKeyAndVisible];
        return YES;
    }else{
        self.window.rootViewController = vc;
    }
    [self.window makeKeyAndVisible];
    
    return YES;
}


GuideVC.m代碼:
#import "GuideVC.h"
#import "AppDelegate.h"
#import "ViewController.h"
#define Main_Screen_Width [UIScreen mainScreen].bounds.size.width
#define Main_Screen_Height [UIScreen mainScreen].bounds.size.height

#define totalCounts 7  //引導頁數量
#define EVER_LAUNCHED @"GuideVC_EVER_LAUNCHED"

@interface GuideVC ()<UIScrollViewDelegate>

@property (strong, nonatomic)  UIScrollView *pageScroll;
@property (strong, nonatomic)  UIPageControl *pageControl;

@end

@implementation GuideVC


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:EVER_LAUNCHED];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self createScrollView];
}

/**
 *  創建顯示引導圖片ScrollView
 */
-(void)createScrollView{
    
    
    self.pageScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    self.pageScroll.pagingEnabled = YES;
    self.pageScroll.delegate = self;
    self.pageScroll.contentSize = CGSizeMake(Main_Screen_Width * totalCounts, Main_Screen_Height);
    self.pageScroll.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.pageScroll.showsHorizontalScrollIndicator = NO;
    self.pageScroll.showsVerticalScrollIndicator = YES;
    [self.view addSubview:self.pageScroll];
    
    /** 添加引導圖片 */
    [self AddGuideImg];
    
    CGFloat w = 100;
    CGFloat h = 20;
    self.pageControl = [[UIPageControl alloc]init];
    [self.pageControl setFrame:CGRectMake((Main_Screen_Width-w)/2, Main_Screen_Height-30, w, h)];
    self.pageControl.numberOfPages = totalCounts;  //設置引導頁有幾個界面
    self.pageControl.currentPage = 0;
    self.pageControl.tintColor = [UIColor lightGrayColor];
    [self.view addSubview:self.pageControl];
    
}

/**
 *  添加引導圖片
 */
-(void)AddGuideImg{
    for (int i=0; i<totalCounts; i++) {
        UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*Main_Screen_Width, 0, Main_Screen_Width, Main_Screen_Height)];
        NSString* imageNameStr =[NSString stringWithFormat:@"Guide%i.jpg",i+1];//設置引導頁圖片
        imageView.image = [UIImage imageNamed:imageNameStr];
        
        if (i == totalCounts-1) {
            /** 最後一張圖片添加按鈕 */
            [imageView setUserInteractionEnabled:YES];
            UIButton* start = [UIButton buttonWithType:UIButtonTypeCustom];
            UIImage* startBtnImg = [UIImage imageNamed:@"btn"];
            [start setImage:startBtnImg forState:UIControlStateNormal];
            
            [start setFrame:CGRectMake(20,Main_Screen_Height-100,Main_Screen_Width - 20*2,60)];
            start.layer.borderWidth = 5;
            start.layer.borderColor = [UIColor whiteColor].CGColor;
            [start setTitle:@"朕已閱" forState:UIControlStateNormal];
            
            [start addTarget:self action:@selector(gotoMainView) forControlEvents:
             UIControlEventTouchUpInside];
            [imageView addSubview:start];
            
        }
        [self.pageScroll addSubview:imageView];
    }
    
}

/** 跳轉到首頁(登錄頁) */
-(void)gotoMainView{
    ViewController *vc = [[ViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - UIScrollViewDelegate
#pragma mark
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = self.view.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

+(BOOL)everLaunched
{
    return [[NSUserDefaults standardUserDefaults]boolForKey:EVER_LAUNCHED];
}

Demo:https://github.com/ruiruiguo/GuideVC1.git


發佈了43 篇原創文章 · 獲贊 16 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章