美文网首页
轮播图L

轮播图L

作者: leaderleader | 来源:发表于2017-08-21 09:14 被阅读0次

    判断是不是第一次打开如果是就走轮播图 如果不是就不走轮播图

    标签控制器继承


    ViewController *theVc = [[ViewController alloc]init];

    self.window.rootViewController = theVc;

    self.window.backgroundColor = [UIColor whiteColor];

    <UIScrollViewDelegate>

    {

    //创建滑动控件

    UIScrollView *theScroll;

    //分页控件

    UIPageControl *thePage;

    //创建整型类

    NSInteger tegPageTime;

    //创建数组;

    NSArray *theArray;

    //计数器

    NSTimer *theTime;

    }

    //===========

    //获取屏幕的宽

    float width = self.view.frame.size.width;

    //获取屏幕的高

    float height = self.view.frame.size.height;

    //代替 初始化滚动控件

    theScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, height)];

    //获取屏幕X轴

    float X = 0.0;

    //一共四张图片

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

    {

    //图片初始化

    UIImageView *theImg = [[UIImageView alloc] initWithFrame:CGRectMake(X, 0, width, height)];

    //将图片添加到视图里

    theArray = @[[UIImage imageNamed:@"1"],[UIImage imageNamed:@"1"],[UIImage imageNamed:@"1"],[UIImage imageNamed:@"1"],];

    //将图片与数组下标绑定

    theImg.image = theArray[i];

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

    [theScroll addSubview:theImg];

    //设置滚动视图的坐标 原代码

    //(width=self.view.frame.size.width+self.view.frame.size.width+self.view.frame.size.width+self.view.frame.size.width,)

    X += width;

    }

    //设置滚动视图的大小

    theScroll.contentSize = CGSizeMake(width * 4, height);

    // 设置滚动视图按页滚动

    theScroll.pagingEnabled = YES;

    //滚动条隐藏

    theScroll.showsHorizontalScrollIndicator = NO;

    //设置代理

    theScroll.delegate = self;

    //初始化页码

    thePage = [[UIPageControl alloc]initWithFrame:CGRectMake(width /2-50, 570, 100, 30)];

    //页数

    thePage.numberOfPages = 4;

    //初始页第一页

    thePage.currentPage = 0;

    //清除原来颜色

    thePage.backgroundColor = [UIColor clearColor];

    //没动原来

    thePage.pageIndicatorTintColor = [UIColor greenColor];

    //当前翻页显示的颜色

    thePage.currentPageIndicatorTintColor = [UIColor redColor];

    //顺序不能乱

    [self.view addSubview:theScroll];

    [self.view addSubview:thePage];

    //记录当前图片

    tegPageTime = thePage.currentPage;

    //第一个几秒钟换一下 事件

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

    ///===========

    //事件

    -(void)scroll

    {

    tegPageTime ++;

    if (tegPageTime>=theArray.count)

    {

    tegPageTime = 0;

    }

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

    }

    //表示在滑动滚动视图的时候调用此方法

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

    CGPoint point=scrollView.contentOffset;//偏移

    //获得所在位置

    thePage.currentPage=point.x / scrollView.frame.size.width;

    UIButton *theBtn = [[UIButton alloc]init];

    if (thePage.currentPage == 3)

    {

    //停止图片滚动

    [theTime setFireDate:[NSDate distantFuture]];

    theBtn.frame = CGRectMake(160, 607, 100, 40);

    //设置按钮背景颜色

    theBtn.backgroundColor = [UIColor blueColor];

    //设置按钮为圆角

    theBtn.layer.cornerRadius = 8;

    //添加按钮文字

    [theBtn setTitle:@"立即登录" forState:UIControlStateNormal];

    //添加按钮触发事件:必须要用(UIControlEventTouchUpInside)

    [theBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:theBtn];

    //设置与用户无法交互(这样即可解决按钮在前面的那页中显示)

    //  scrollView.userInteractionEnabled = NO;

    }

    }

    //实现页码和滚动视图的关联

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

    thePage.currentPage = theScroll.contentOffset.x / self.view.frame.size.width;

    }

    -(void)click

    {

    oneViewController *VC = [[oneViewController alloc]init];

    [self presentViewController:VC animated:YES completion:nil];

    }

    相关文章

      网友评论

          本文标题:轮播图L

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