在很多应用中我们都可以看到图片轮播器
scrollView.gif下来我将使用两个imageView实现图片的轮播,也是目前我用到最简单的方法,并且可以完美封装,很实用,不说了,直接上代码
@interface UIScrollView ()<UIScrollViewDelegate>
//滚动视图
@property (nonatomic, strong)UIScrollView *scrollView;
//左图片
@property (nonatomic, strong)UIImageView *leftImageView;
//右图片
@property (nonatomic, strong)UIImageView *rightImageView;
//图片数组(用于存放所有轮播图片)
@property (nonatomic, strong)NSArray *imageArray;
//当前下标
@property (nonatomic, assign)NSInteger index;
//小圆点
@property (nonatomic, strong)UIPageControl *pageControl;
//定时器
@property (nonatomic, strong)NSTimer *timer;
声明完所需要的各个属性,下来懒加载,完成基本配置
- (NSTimer *)timer{
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
return _timer;
}
- (UIScrollView *)scrollView{
if (!_scrollView) {
//创建滚动视图及属性
_scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
_scrollView.contentSize = CGSizeMake(KWidth * 2, KHeight);
_scrollView.bounces = NO;
_scrollView.bouncesZoom = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = NO;
}
return _scrollView;
}
- (UIPageControl *)pageControl{
if (!_pageControl) {
//创建小圆点
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, KHeight - 50, 100, 50)];
_pageControl.numberOfPages = self.imageArray.count;
}
return _pageControl;
}
- (UIImageView *)leftImageView{
if (!_leftImageView) {
_leftImageView = [[UIImageView alloc] initWithFrame:self.frame];
_leftImageView.image = self.imageArray[0];
}
return _leftImageView;
}
- (UIImageView *)rightImageView{
if (!_rightImageView) {
_rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth, 0, KWidth, KHeight)];
_rightImageView.image = self.imageArray[self.index];
}
return _rightImageView;
}
- (instancetype)initWithFrame:(CGRect)frame
imageArray:(NSArray *)imageArray{
self = [super initWithFrame:frame];
if (self) {
self.index = 1;
//给数组赋值
self.imageArray = imageArray;
//设置视图
[self createUI];
}
return self;
}
- (void)createUI{
[self.scrollView addSubview:self.leftImageView];
[self.scrollView addSubview:self.rightImageView];
[self addSubview:self.scrollView];
[self addSubview:self.pageControl];
//设置延迟1秒启动定时器
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.timer fire];
});
}
//定时器事件
- (void)timerAction{
[self.scrollView setContentOffset:CGPointMake(KWidth, 0) animated:YES];
}
基本的配置就这些,重要的一步就在UIScrollViewDelegate里面实现
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == KWidth) {
//让scrollView始终处在最初位置
scrollView.contentOffset = CGPointZero;
self.pageControl.currentPage = self.index;
//滑动一次图片数组的下标增加一次
self.index++;
//当轮播到最后一张图片时,图片数组切换到第一张
if (self.index == self.imageArray.count) {
self.index = 0;
}
//视图一直显示的是左边imageView上的图片
self.leftImageView.image = self.rightImageView.image;
//右边imageView起到暂存接下来该播放的那张图片
self.rightImageView.image = self.imageArray[self.index];
}
}
这样就完美的实现了用两个imageView实现图片的无限轮播。看完代码有没有觉得很简单,那还等什么,赶紧去写一个属于自己的图片轮播器吧!
网友评论