1. .h
#import <UIKit/UIKit.h>
typedef void(^ClickImageBlock)(NSInteger currentIndex);
@interface LoopScrollView : UIView
- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray clickImage:(ClickImageBlock)clickImageBlock;
- (void)invalidateTimer;
@end
2. .m
//
// LoopScrollView.m
// 轮播器
//
// Created by 李文良 on 2017/4/20.
// Copyright © 2017年 王新伟. All rights reserved.
//
#define kWidth self.frame.size.width
#define kHeight self.frame.size.height
#define kImageCount 3
#import "LoopScrollView.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
@interface LoopScrollView()<UIScrollViewDelegate>
/**
*滚动视图的控件
*/
@property(nonatomic,strong)UIScrollView* scroll;
/**
*页码指示视图的控件
*/
@property(nonatomic,strong)UIPageControl* pageControl;
/**
*显示左边图片的控件
*/
@property(nonatomic,strong)UIImageView* LeftImageView;
/**
*显示中心图片的控件
*/
@property(nonatomic,strong)UIImageView* centerImageView;
/**
*显示右边图片的控件
*/
@property(nonatomic,strong)UIImageView* rightImageView;
/**
*保存图片的数组
*/
@property(nonatomic,strong)NSArray* imageArray;
/**
*图片的当前下标索引
*/
@property(nonatomic,assign)NSInteger currentIndex;
/**
*图片总数
*/
@property(nonatomic,assign)NSInteger imageCount;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,copy)ClickImageBlock clickImageBlock;
@property(nonatomic,strong)UITapGestureRecognizer *tap;
@end
@implementation LoopScrollView
- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray clickImage:(ClickImageBlock)clickImageBlock
{
self.imageArray = imageArray;
self.imageCount = imageArray.count;
self.clickImageBlock = clickImageBlock;
return [self initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.currentIndex=0;
[self createScrollView];
[self createImageView];
[self createPageControl];
[self setImageByIndex:self.currentIndex];
self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(freshScroll) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
return self;
}
/**
*以下是搭建UI界面的方法
*/
-(void)createScrollView
{
self.scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,kWidth, kHeight)];
self.scroll.backgroundColor=[UIColor redColor];
self.scroll.showsHorizontalScrollIndicator=NO;
self.scroll.showsVerticalScrollIndicator=NO;
self.scroll.pagingEnabled=YES;
self.scroll.bounces=NO;
self.scroll.delegate=self;
self.scroll.contentOffset=CGPointMake(kWidth, 0);
self.scroll.contentSize=CGSizeMake(kWidth*kImageCount, kHeight);
[self addSubview:self.scroll];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
[self.scroll addGestureRecognizer:self.tap];
}
-(void)createPageControl
{
self.pageControl = [[UIPageControl alloc] init];
self.pageControl.currentPageIndicatorTintColor=[UIColor redColor];
self.pageControl.pageIndicatorTintColor=[UIColor blueColor];
self.pageControl.enabled=YES;
self.pageControl.numberOfPages = self.imageArray.count;
[self addSubview:self.pageControl];
[self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.bottom.equalTo(self.mas_bottom).offset(-5);
make.width.mas_equalTo(60);
make.height.mas_equalTo(20);
}];
}
-(void)createImageView
{
self.LeftImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 220)];
[self.scroll addSubview:self.LeftImageView];
self.centerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(kWidth, 0, kWidth, 220)];
[self.scroll addSubview:self.centerImageView];
self.centerImageView.backgroundColor = [UIColor yellowColor];
self.rightImageView=[[UIImageView alloc]initWithFrame:CGRectMake(2 * kWidth, 0, kWidth, 220)];
[self.scroll addSubview:self.rightImageView];
}
#pragma mark------定时器
- (void)freshScroll
{
[self scrollViewDidEndDecelerating:self.scroll];
}
#pragma mark-----点击图片
- (void)tapImage:(UITapGestureRecognizer *)tap
{
if (self.clickImageBlock) {
self.clickImageBlock(self.currentIndex);
}
}
- (void)invalidateTimer
{
[self.timer invalidate];
self.timer = nil;
[self.tap removeTarget:self action:@selector(tapImage:)];
}
#pragma mark ----刷新图片
-(void)refreshImage
{
if (self.scroll.contentOffset.x>=kWidth) {
self.currentIndex=((self.currentIndex+1)%self.imageCount);
}
else if(self.scroll.contentOffset.x<kWidth){
self.currentIndex=((self.currentIndex-1+self.imageCount)%self.imageCount);
}
[self setImageByIndex:self.currentIndex];
}
#pragma mark ----该方法根据传回的下标设置三个ImageView的图片
-(void)setImageByIndex:(NSInteger )currentIndex
{
NSString *currentImageUrl = self.imageArray[currentIndex];
[self.centerImageView sd_setImageWithURL:[NSURL URLWithString:currentImageUrl] placeholderImage:nil];
NSString *leftImageUrl = self.imageArray[((self.currentIndex-1+self.imageCount)%self.imageCount)];
[self.LeftImageView sd_setImageWithURL:[NSURL URLWithString:leftImageUrl] placeholderImage:nil];
NSString *rightImageUrl = self.imageArray[((self.currentIndex+1)%self.imageCount)];
[self.rightImageView sd_setImageWithURL:[NSURL URLWithString:rightImageUrl] placeholderImage:nil];
self.pageControl.currentPage=currentIndex;
}
#pragma mark ----UIScrollViewDelegate代理方法(停止加速时调用)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self refreshImage];
self.scroll.contentOffset = CGPointMake(kWidth,0);
self.pageControl.currentPage = self.currentIndex;
NSLog(@"停止了加速,停在第%ld页",self.pageControl.currentPage+1);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.timer invalidate];
self.timer = nil;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(freshScroll) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
@end