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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章