ios程序 引导屏

本文介绍如何在iOS应用中创建一个简单的引导页,并通过滚动视图和分页控件实现页面切换。包括按钮交互、动画效果及初始化过程。

项目中有时候需要引导页,本人是用scrollview自己写的一个非常简单地实现,下文中的UIBarButtonItem *right 这个导航条上得右侧按钮就是触发引导页的,(我的程序开始要求第一次运行的时候要有引导屏,然后在程序的其他地方还能查看引导屏,下文中我没有写程序第一次启动的时候引导屏的过程,那个其实也不难,有需要的可以自己判断程序是不是第一次启动后,添加一个viewcontroller也行或者直接在appdelegate中实现,关键是你判断程序第一次运行)

下文中的bu是一个在导航页最后一页上得一个按钮,用于点击后回到主页面的操作,引导页效果有很多种,这里为大家带来最简单的一种。

前提要设置好uiscrollview的代理,

- (void)viewdidload

{

 //右边的问号    引导页显示按钮

    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"引导屏"] style:(UIBarButtonItemStyleBordered) target:self action:@selector(guidView)];

    self.navigationItem.rightBarButtonItem = right;

    _backView = [[UIView alloc]initWithFrame:CGRectMake(320, 0, 320, 480)];

    _backView.backgroundColor = [UIColor grayColor];

    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];

    [_backView addSubview:_scrollView];

    _scrollView.contentSize = CGSizeMake(320*7, self.view. bounds.size.height);

    _scrollView.showsHorizontalScrollIndicator = NO;

    _scrollView.pagingEnabled=YES;//是否自己动适应

    _scrollView.delegate = self;


    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(60, 455, 200, 20)];

    _pageControl.numberOfPages = 7;

    _pageControl.currentPage = 0;

    [_pageControl setBounds:CGRectMake(0,0,16*(7-1)+16,16)]; //页面控件上的圆点间距基本在16左右。

    [_pageControl.layer setCornerRadius:8]; // 圆角层

    [_pageControl setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.5]];

    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];

    [_backView addSubview:_pageControl];

    

    NSArray *arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",nil];

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

        UIImageView *omg = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, self.view.bounds.size.height)];

        [_scrollView addSubview:omg];

        omg.image = [UIImage imageNamed:[arr objectAtIndex:i]];

        if (i == 6) {

            bu = [UIButton buttonWithType:UIButtonTypeCustom];

            bu.frame = CGRectMake(20, 400, 70, 40);

            [omg addSubview:bu];

            omg.userInteractionEnabled = YES;

            [bu setImage:[UIImage imageNamed:@"点击前"] forState:UIControlStateNormal];

            [bu addTarget:self action:@selector(ToMainView) forControlEvents:UIControlEventTouchUpInside];

        }

    }

}

- (void)guidView

{

    //添加那一层view覆盖掉导航条

    [[UIApplication sharedApplication].keyWindow addSubview:_backView];

    //动画出现 从右向左

    [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{

        _backView.frame = CGRectMake(0, 0, 320, 480);

    } completion:^(BOOL finished) {

        

    }];

}

//uiscrollview的代理  滑动的时候改变uipagecontrol的当前位置

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    //更新UIPageControl的当前页

    CGPoint offset = scrollView.contentOffset;

    CGRect bounds = scrollView.frame;

    [_pageControl setCurrentPage:offset.x / bounds.size.width];

    if (_pageControl.currentPage == 6) {

        [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

            bu.frame = CGRectMake(20, 395, 120, 40);

        } completion:^(BOOL finished) {

            

        }];

    }else{

        bu.frame = CGRectMake(20, 400, 70, 40);

    }  

}

//当引导页走完时再动画消失  从左向右

- (void)ToMainView

{

    [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{

        _backView.frame = CGRectMake(320, 0, 320, 480);

    } completion:^(BOOL finished) {

        _pageControl.currentPage = 0;

        //从新设置uiscrollview的偏移量  当引导页消失后,把_scrollView从新放到开始的位置处,保证下次再次点击的时候还是从最开始的位置滑动

        _scrollView.contentOffset = CGPointMake(0, 0);

        bu.frame = CGRectMake(20, 400, 70, 40);

    }];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值