码迷,mamicode.com
首页 > 移动开发 > 详细

iOS: 无限循环轮播图简单封装

时间:2016-06-28 23:29:39      阅读:408      评论:0      收藏:0      [点我收藏+]

标签:

轮播图思路:

1.首先要有一个ScrollView和两张图片,把图片放到ScrollView上。

2.要想滚动ScrollView的宽度需设置屏宽的2倍。

3.循环滚动,当滚动到最后一张图片时,采用偏移的方法,将偏移量归零,回到第一张图片。

4.将第二张的图片上的所有值赋给第一张图片。

5.设置让它自己滚动,需添加定时器。

需要的第三方数据库:SDWebImage

 

m.文件内:

#imporst "ScrollView.h"

@interface ScrollView ()<UIScrollViewDelegate>

//滚动视图

@property (nonatomic,strong) UIScrollView *scrollView;

//图片

@property (nonatomic,strong) UIImageView *leftImageView;

//图片

@property (nonatomic,strong) UIImageView *rightImageView;

//指示小圆点

@property (nonatomic,strong) UIPageControl *pageControl;

//下标

@property (nonatomic,assign) NSInteger index;

//定时器

@property (nonatomic,strong) NSTimer *timer;

//接收外面传进来的值

@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation ScrollView

//滚动视图的懒加载

- (UIScrollView *)scrollView{

    if (!_scrollView) {

        //初始化滚动视图

        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

        //设置代理

        _scrollView.delegate = self;

        //取消反弹效果

        _scrollView.bounces = NO;

        _scrollView.bouncesZoom = NO;

        //设置整页翻滚

        _scrollView.pagingEnabled = YES;

        //滚动范围

        _scrollView.contentSize = CGSizeMake(self.frame.size.width*2, 0);

        //滚动条的关闭

        _scrollView.showsHorizontalScrollIndicator = NO;

    }

    return _scrollView;

}

//左边图片懒加载

- (UIImageView *)leftImageView{

    if (!_leftImageView) {

        //初始化图片框架

        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

        //添加图片(写这步需要引入第三方数据库)

        [_leftImageView sd_setImageWithURL:[NSURL URLWithString: self.dataArray[0]]];

    }

    return _leftImageView;

}

//右边图片懒加载

- (UIImageView *)rightImageView{

    if (!_rightImageView) {

        //初始化图片框架

        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.leftImageView.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];

        //添加图片(写这步需要引入第三方数据库)

        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.index]]];

    }

    return _rightImageView;

}

//小圆点指示标懒加载

- (UIPageControl *)pageControl{

    if (!_pageControl) {

        //初始化小圆点

        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, self.scrollView.frame.size.height-50, self.frame.size.width-100, 50)];

        //    设置页数

        _pageControl.numberOfPages = self.dataArray.count;

        //    设置圆点指示器的小圆点颜色

        _pageControl.pageIndicatorTintColor = [UIColor redColor];

        //    设置当前小圆点的颜色

        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

    }

      return _pageControl;

}

//定时器懒加载

- (NSTimer *)timer{

    if (!_timer) {

        //定时时间及事件

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

    }

    return _timer;

}

//重写父类

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray{

    if ([super initWithFrame:frame]) {

        //第一张图片

        self.index = 1;

        //将外面的值引入

        self.dataArray = dataArray;

        [self creatScrollViewWithFrame:frame];

    }

    return self;

}

//创建滚动视图

- (void)creatScrollViewWithFrame:(CGRect)frame  {

    [self.scrollView addSubview:self.leftImageView];

    [self.scrollView addSubview:self.rightImageView];

    [self addSubview:self.scrollView];

    [self addSubview:self.pageControl];

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self.timer fire];

    });

}

//代理方法

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

     //如果滚动视图的偏移量为屏幕宽

    if (scrollView.contentOffset.x == self.frame.size.width) {

        //返回0

        self.scrollView.contentOffset = CGPointZero;

        //当前小圆点为下标

        self.pageControl.currentPage = self.index;

        //下标加+

        self.index++;

        //数组的下标设置为index

        if (self.index == self.dataArray.count) {

            //从0开始

            self.index = 0;

        }

        //图片之间传值

    self.leftImageView.image = self.rightImageView.image;

    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.index]]];

    }

}

 //定时器事件

- (void)timerAction{

     [self.scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];

}

 h.文件内:

#import <UIKit/UIKit.h>

@interface ScrollView : UIView

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray;

@end

 

iOS: 无限循环轮播图简单封装

标签:

原文地址:http://www.cnblogs.com/zsl9855soley/p/5625203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!