美文网首页
引导页 + 定时器

引导页 + 定时器

作者: YQ_苍穹 | 来源:发表于2018-04-23 09:44 被阅读0次

    #import "ViewController.h"

    #import "SecViewController.h"

    @interface ViewController ()<UIScrollviewDelegate>

    {

        UIScrollView *scroll;  //滚动视图

        NSArray *imgArr; //图片数组

        UIPageControl *page; //分页控件

        NSTimer *theTime;

        NSInteger thePageTime;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        //设置背景颜色

        self.view.backgroundColor = [UIColor cyanColor];

        //创建滚动视图

        scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];

        //设置代理

        scroll.delegate = self;

        //将滚动视图添加到视图上

        [self.view addSubview:scroll];

        //创建图片数组

        imgArr = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"];

        //使用for循环设置图片

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

            //创建图片框

            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];

            //设置图片

            image.image = [UIImage imageNamed:imgArr[i]];

            //允许与用户交互

            image.userInteractionEnabled = YES;

            //将图片添加到滚动视图

            [scroll addSubview:image];

            /**

            if (i == 3)

            {

            // 创建立即体验按钮

            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

            btn.frame = CGRectMake((self.view.frame.size.width - 100)/2, 600, 100, 44);

            [btn setTitle:@"立即体验" forState:UIControlStateNormal];

            //            btn.backgroundColor = [UIColor cyanColor];

            // 给按钮添加点击事件

            [btn addTarget:self action:@selector(didClickBtn) forControlEvents:UIControlEventTouchUpInside];

            // 设置圆角

            btn.layer.cornerRadius = 8;

            btn.layer.masksToBounds = YES;

            // 设置边框

            btn.layer.borderWidth = 1;

            btn.layer.borderColor = [UIColor cyanColor].CGColor;

            // 将按钮添加到图片上

            [image addSubview:btn];

            }

            */

        }

        //设置滚动范围

        scroll.contentSize = CGSizeMake(self.view.frame.size.width*4, self.view.frame.size.height);

        //取消弹簧效果

        scroll.bounces = NO;

        //设置分页效果

        scroll.pagingEnabled = YES;

        //隐藏水平滚动条

        scroll.showsHorizontalScrollIndicator = NO;

        //创建分页控件

        page = [[UIPageControl alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, self.view.frame.size.height-100, 100, 30)];

        //设置页数

        page.numberOfPages = 4;

        //设置页码颜色

        page.pageIndicatorTintColor = [UIColor redColor];

        //设置当前页码颜色

        page.currentPageIndicatorTintColor = [UIColor blackColor];

        //添加到视图

        [self.view addSubview:page];

        //记录当前的图片

        thePageTime = page.currentPage;

        //创建定时器

        theTime = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(Scroll) userInfo:nil repeats:YES];

    }

    //滚动视图协议的方法

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{

        //将滚动的页数和滚动视图关联

        page.currentPage = scroll.contentOffset.x/self.view.frame.size.width;

    }

    //定时器方法

    -(void)Scroll{

        thePageTime++;

        if (thePageTime >= imgArr.count) {

            thePageTime = 3;

            [theTime invalidate];

            theTime = nil;

            [self presentViewController:[SecViewController new] animated:YES completion:nil];

        }

        //计算滚动视图的偏移量

        [scroll setContentOffset:CGPointMake(thePageTime * scroll.frame.size.width, 0) animated:YES];

    }

    -(void)didClickBtn{

    }

    @end

    相关文章

      网友评论

          本文标题:引导页 + 定时器

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