引导页

作者: pingui | 来源:发表于2016-01-07 15:23 被阅读419次

    第一种方法: 通过点击按键的方式进入应用###

    该方法需要两个视图控制器,一个用来创建引导页的滚动视图,另一个创建应用界面

    在AppDelegate.m文件中实现下面的方法

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
        // 使用 NSUserDefaults 读取用户数据
        if (![useDef boolForKey:@"notFirst"]) {
            // 如果是第一次进入引导页
            _window.rootViewController = [[ViewController alloc] init];
        }
        else{
            // 否则直接进入应用
            _window.rootViewController = [[GPSecondViewController alloc] init];
        }
        return YES;
    }
    

    引导页视图控制器

    #import "ViewController.h"
    #import "GPSecondViewController.h"
    
    #define WIDTH (NSInteger)self.view.bounds.size.width
    #define HEIGHT (NSInteger)self.view.bounds.size.height
    
    @interface ViewController ()<UIScrollViewDelegate>
    {
        // 创建页码控制器
        UIPageControl *pageControl;
        // 判断是否是第一次进入应用
        BOOL flag;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        for (int i=0; i<3; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
            // 在最后一页创建按钮
            if (i == 2) {
                // 必须设置用户交互 否则按键无法操作
                imageView.userInteractionEnabled = YES;
                UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
                button.frame = CGRectMake(WIDTH / 3, HEIGHT * 7 / 8, WIDTH / 3, HEIGHT / 16);
                [button setTitle:@"点击进入" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
                [imageView addSubview:button];
            }
            imageView.image = image;
            [myScrollView addSubview:imageView];
        }
        myScrollView.bounces = NO;
        myScrollView.pagingEnabled = YES;
        myScrollView.showsHorizontalScrollIndicator = NO;
        myScrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);
        myScrollView.delegate = self;
        [self.view addSubview:myScrollView];
        
        pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];
        // 设置页数
        pageControl.numberOfPages = 3;
        // 设置页码的点的颜色
        pageControl.pageIndicatorTintColor = [UIColor yellowColor];
        // 设置当前页码的点颜色
        pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
        [self.view addSubview:pageControl];
    }
    
    #pragma mark - UIScrollViewDelegate
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        // 计算当前在第几页
        pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
    }
    
    // 点击按钮保存数据并切换根视图控制器
    - (void) go:(UIButton *)sender{
        flag = YES;
        
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
        // 保存用户数据
        [useDef setBool:flag forKey:@"notFirst"];
        [useDef synchronize];
        // 切换根视图控制器
        self.view.window.rootViewController = [[GPSecondViewController alloc] init];
    }
    
    @end
    
    引导页-按键.png

    点击按钮进入应用界面

    应用界面.png

    第二种方法: 通过滑动方式进入应用###

    该方法只需要一个视图控制器,如果用户第一次使用才创建引导页的滚动视图,否则直接进入应用

    #import "ViewController.h"
    
    #define WIDTH (NSInteger)self.view.bounds.size.width
    #define HEIGHT (NSInteger)self.view.bounds.size.height
    
    @interface ViewController ()<UIScrollViewDelegate>
    {
        UIPageControl *pageControl;
        UIScrollView *myScrollView;
        BOOL flag;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor cyanColor];
        
        UILabel *label = [[UILabel alloc] initWithFrame:[UIScreen mainScreen].bounds];
        label.text = @"欢迎使用xxx";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:28];
        [self.view addSubview:label];
    
        NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
        
        // 如果是第一次启动就创建myScrollView
        if (![userDef boolForKey:@"notFirst"]) {
            myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
            for (int i=0; i<3; i++) {
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i + 1]];
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
                imageView.image = image;
                [myScrollView addSubview:imageView];
            }
            myScrollView.delegate = self;
            myScrollView.bounces = NO;
            myScrollView.pagingEnabled = YES;
            myScrollView.showsHorizontalScrollIndicator = NO;
            // 提示:滚动视图的宽度要设成4倍
            myScrollView.contentSize = CGSizeMake(WIDTH * 4, HEIGHT);
            [self.view addSubview:myScrollView];
            
            pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT - 40, WIDTH, 40)];
            pageControl.numberOfPages = 3;
            pageControl.currentPage = 0;
            pageControl.pageIndicatorTintColor = [UIColor whiteColor];
            pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
            [self.view addSubview:pageControl];
            
            // 保存数据
            flag = YES;
            [userDef setBool:flag forKey:@"notFirst"];
            [userDef synchronize];
        }
    }
    
    #pragma mark - UIScrollViewDelegate
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGFloat x = myScrollView.contentOffset.x;
        if (scrollView == myScrollView) {
            // 四舍五入
            pageControl.currentPage = lroundf(x / WIDTH);
        }
        // 当滑动到第4页时移除myScrollView和pageControl
        if (lroundf(x / WIDTH) == 3) {
            [myScrollView removeFromSuperview];
            [pageControl removeFromSuperview];
        }
    }
    @end
    
    引导页-滑动.png

    向左滑动进入应用界面

    第一种方法: 通过点击按键的方式进入应用###

    该方法需要两个视图控制器,一个用来创建引导页的滚动视图,另一个创建应用界面

    在AppDelegate.m文件中实现下面的方法

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
        // 使用 NSUserDefaults 读取用户数据
        if (![useDef boolForKey:@"notFirst"]) {
            // 如果是第一次进入引导页
            _window.rootViewController = [[ViewController alloc] init];
        }
        else{
            // 否则直接进入应用
            _window.rootViewController = [[GPSecondViewController alloc] init];
        }
        return YES;
    }
    

    引导页视图控制器

    #import "ViewController.h"
    #import "GPSecondViewController.h"
    
    #define WIDTH (NSInteger)self.view.bounds.size.width
    #define HEIGHT (NSInteger)self.view.bounds.size.height
    
    @interface ViewController ()<UIScrollViewDelegate>
    {
        // 创建页码控制器
        UIPageControl *pageControl;
        // 判断是否是第一次进入应用
        BOOL flag;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        for (int i=0; i<3; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
            // 在最后一页创建按钮
            if (i == 2) {
                // 必须设置用户交互 否则按键无法操作
                imageView.userInteractionEnabled = YES;
                UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
                button.frame = CGRectMake(WIDTH / 3, HEIGHT * 7 / 8, WIDTH / 3, HEIGHT / 16);
                [button setTitle:@"点击进入" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
                [imageView addSubview:button];
            }
            imageView.image = image;
            [myScrollView addSubview:imageView];
        }
        myScrollView.bounces = NO;
        myScrollView.pagingEnabled = YES;
        myScrollView.showsHorizontalScrollIndicator = NO;
        myScrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);
        myScrollView.delegate = self;
        [self.view addSubview:myScrollView];
        
        pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];
        // 设置页数
        pageControl.numberOfPages = 3;
        // 设置页码的点的颜色
        pageControl.pageIndicatorTintColor = [UIColor yellowColor];
        // 设置当前页码的点颜色
        pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
        [self.view addSubview:pageControl];
    }
    
    #pragma mark - UIScrollViewDelegate
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        // 计算当前在第几页
        pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
    }
    
    // 点击按钮保存数据并切换根视图控制器
    - (void) go:(UIButton *)sender{
        flag = YES;
        
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
        // 保存用户数据
        [useDef setBool:flag forKey:@"notFirst"];
        [useDef synchronize];
        // 切换根视图控制器
        self.view.window.rootViewController = [[GPSecondViewController alloc] init];
    }
    
    @end
    
    引导页-按键.png

    点击按钮进入应用界面

    应用界面.png

    第二种方法: 通过滑动方式进入应用###

    该方法只需要一个视图控制器,如果用户第一次使用才创建引导页的滚动视图,否则直接进入应用

    #import "ViewController.h"
    
    #define WIDTH (NSInteger)self.view.bounds.size.width
    #define HEIGHT (NSInteger)self.view.bounds.size.height
    
    @interface ViewController ()<UIScrollViewDelegate>
    {
        UIPageControl *pageControl;
        UIScrollView *myScrollView;
        BOOL flag;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor cyanColor];
        
        UILabel *label = [[UILabel alloc] initWithFrame:[UIScreen mainScreen].bounds];
        label.text = @"欢迎使用xxx";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:28];
        [self.view addSubview:label];
    
        NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
        
        // 如果是第一次启动就创建myScrollView
        if (![userDef boolForKey:@"notFirst"]) {
            myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
            for (int i=0; i<3; i++) {
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i + 1]];
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
                imageView.image = image;
                [myScrollView addSubview:imageView];
            }
            myScrollView.delegate = self;
            myScrollView.bounces = NO;
            myScrollView.pagingEnabled = YES;
            myScrollView.showsHorizontalScrollIndicator = NO;
            // 提示:滚动视图的宽度要设成4倍
            myScrollView.contentSize = CGSizeMake(WIDTH * 4, HEIGHT);
            [self.view addSubview:myScrollView];
            
            pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT - 40, WIDTH, 40)];
            pageControl.numberOfPages = 3;
            pageControl.currentPage = 0;
            pageControl.pageIndicatorTintColor = [UIColor whiteColor];
            pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
            [self.view addSubview:pageControl];
            
            // 保存数据
            flag = YES;
            [userDef setBool:flag forKey:@"notFirst"];
            [userDef synchronize];
        }
    }
    
    #pragma mark - UIScrollViewDelegate
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGFloat x = myScrollView.contentOffset.x;
        if (scrollView == myScrollView) {
            // 四舍五入
            pageControl.currentPage = lroundf(x / WIDTH);
        }
        // 当滑动到第4页时移除myScrollView和pageControl
        if (lroundf(x / WIDTH) == 3) {
            [myScrollView removeFromSuperview];
            [pageControl removeFromSuperview];
        }
    }
    @end
    
    引导页-滑动.png

    向左滑动进入应用界面

    应用界面.png

    相关文章

      网友评论

        本文标题:引导页

        本文链接:https://www.haomeiwen.com/subject/hxyfkttx.html