美文网首页
iOS开发------引导页

iOS开发------引导页

作者: HMZ4978 | 来源:发表于2017-08-09 10:07 被阅读0次

            每个app都会有引导页,  由于引导页是在第一次启动这个app的时候才会出现, 出于他的这个特点,  我是这样做的.

            首先, 先把引导页所在的控制器设置为根视图控制器, 当引导页展示完之后, 再将正常时app的控制器设置为根视图控制器.

    #import "BaseViewController.h"

    @interface MyViewController : BaseViewController

    @end

    #import "MyViewController.h"

    @interface MyViewController (){

    // 创建页码控制器

    UIPageControl *pageControl;

    // 判断是否是第一次进入应用

    BOOL flag;

    }

    @end

    @implementation MyViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    [self creatSub];

    }

    - (void)creatSub {

    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    for (int i=0; i<4; i++) {

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"引导%d.png",i+1]];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];

    // 在最后一页创建按钮

    if (i == 3) {

    // 必须设置用户交互 否则按键无法操作

    imageView.userInteractionEnabled = YES;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(WIDTH *2/ 5, HEIGHT * 7 / 8, WIDTH / 5, HEIGHT / 16);

    [button setTitle:@"点击进入" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    button.layer.borderWidth = 2;

    button.layer.cornerRadius = 5;

    button.clipsToBounds = YES;

    button.layer.borderColor = [UIColor blackColor].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 * 4, HEIGHT);

    myScrollView.delegate = self;

    [self.view addSubview:myScrollView];

    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];

    pageControl.centerX = WIDTH/2;

    // 设置页数

    pageControl.numberOfPages = 4;

    // 设置页码的点的颜色

    pageControl.pageIndicatorTintColor = RGBA(187, 192, 191, 1);

    // 设置当前页码的点颜色

    pageControl.currentPageIndicatorTintColor = RGBA(78, 170, 155, 1);

    [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];

    // 发送通知, 修改根视图控制器  to  appdelegate

    [[NSNotificationCenter defaultCenter] postNotificationName:@"rootVC" object:nil];

    }

    当引导页展示完成后, 修改根视图控制器, 这样就ok了.

    相关文章

      网友评论

          本文标题:iOS开发------引导页

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