美文网首页
图片轮播

图片轮播

作者: xiaocaoera | 来源:发表于2019-08-12 19:05 被阅读0次

    1 添加图片 

    2 设置scrollView的滚动范围 scrollView.contentSize = CGSizeMake(contentW, 0)

    3 设置分页 scrollView.pagingEnabled = YES

    4 监听scrollView的滚动  scrollView.delegate = self

    5 添加计时器

    [NSTimer scheduledTimerWithTimeInterval: 2 target:self selector:@selector(nextImage) userInfo: nil repeats:YES];

    - (void)nextImage {

        if (self.pageControl.currentPage == 4) {

        self.pageControl.currentPage = 0

     } else  {

       self.pageControl.currentPage++;

    }

    滚动时计算页码

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

        CGFloat scrollViewW = scrollView.frame.size.width;

        CGFloat x = scrollView.contentOffset.x;

        int page = (x + scrollViewW / 2) / scrollViewW;

        self.pageControl.currentPage = page;

    }

    开始拖拽的时候关闭定时器

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

          [self.timer removeTimer];

    }

    停止拖拽时重新添加计时器

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

          [self addTimer];

    }

    - (void)addTimer {

        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target: self selector:@selector(nextImage) userInfo:nil  repeats: YES];

    [[NSRunLOOP currentRunLoop] addTimer:self.timer forMode:@"NSRunLoopCommonModes"];

    }

    相关文章

      网友评论

          本文标题:图片轮播

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