iOS引导页

作者: Hither | 来源:发表于2016-04-09 10:21 被阅读5157次

在我们项目中经常会用到引导页,引导页主要功能就是向用户展示你的产品。

这是我写的一个例子的效果图(图片是随便找的):

在AppDelegate.m中:
我们需要两个Viewcongtroller来实现;
myViewController是我的引导页面视图控制器
MainViewController是我们滑动完引导页 点击按钮以后进入的主页面。

#import "AppDelegate.h"
#import "myViewController.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate

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

myViewController.m中:
#import "myViewController.h"
#import "MainViewController.h"

#define WIDTH (NSInteger)self.view.bounds.size.width
#define HEIGHT (NSInteger)self.view.bounds.size.height

@interface myViewController ()<UIScrollViewDelegate>
{
    // 创建页码控制器
    UIPageControl *pageControl;
    // 判断是否是第一次进入应用
    BOOL flag;
}
@end

@implementation myViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    for (int i=0; i<3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Y%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 whiteColor] forState:UIControlStateNormal];
            button.layer.borderWidth = 2;
            button.layer.cornerRadius = 5;
            button.clipsToBounds = YES;
            button.layer.borderColor = [UIColor whiteColor].CGColor;
            [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 = [[MainViewController alloc] init];
}

相关文章

  • ios引导页

    首先修改 App Transport Security SettingsAllow Arbitrary Loads...

  • iOS 引导页

    在AppDelegate.m中:我们需要两个Viewcongtroller来实现;myViewController...

  • ios 引导页

    目标功能 能够快速实现普通引导页功能. 提供自定义view的加载模式. 提供特定样式的加载模式,只需要配置即可. ...

  • iOS引导页

    在我们项目中经常会用到引导页,引导页主要功能就是向用户展示你的产品。 这是我写的一个例子的效果图(图片是随便找的):

  • iOS引导页

    引导页是App中的基本功能,指导用户理解某些操作或版本变化等等。 引导页可能出现在任何时候,页面内容会根据可交互度...

  • iOS引导页、启动页

    前言 这里使用 launchScreen 、.storyboard 文件创建启动图和引导页。首次打开项目或者更新后...

  • iOS 引导页适配

    1,图片适配,最早以前是自己命名规范,例如@1x,@2x,@3x等,3套图基本上就够用了 2,在iPhone X之...

  • ios开发,引导页

    在viewController.m里面 @interface ViewController () { UISc...

  • iOS 引导页 --LaunchIntroduction

    一、前言 引导页,一个酷炫的页面,基本上每个应用程序刚安装后启动的时候都会有一个引导页,用于引导用户使用APP,怎...

  • ios 引导页 新闻

    首先修改 App Transport Security Settings Allow Arbitrary Load...

网友评论

本文标题:iOS引导页

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