1、使用方法:
NSMutableArray *listArray = @[].mutableCopy;
for (NSInteger i = 0; i < 5; i++) {
ZGCyclePictureModel *model = [[ZGCyclePictureModel alloc] init];
model.type = ZGCyclePictureTypeLocal;
model.image = [UIImage imageNamed:[NSString stringWithFormat:@"CircularBrowsePicture-0%ld.JPG", i + 1]];
[listArray addObject:model];
}
__weak typeof(self) weakSelf = self;
self.cycleView = [ZGCycleScrollView showPictures:listArray onSelect:^(ZGCyclePictureModel *model, NSInteger index) {
NSLog(@"点击的第%@张图片", @(index));
} didScroll:^(ZGCyclePictureModel *model, NSInteger index) {
weakSelf.title = [NSString stringWithFormat:@"第%@张图片", @(index)];
}];
[self.contentView addSubview:self.cycleView];
[self.cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(70);
make.left.right.mas_equalTo(0);
make.height.mas_equalTo(self.cycleView.mas_width).multipliedBy(0.75);
}];
2、利用UIScrollView实现循环轮播图片的功能,以下为.h和.m文件:
//
// ZGCycleScrollView.h
// SummaryDemo
//
// Created by zhaogang on 2018/4/28.
//
#import <UIKit/UIKit.h>
#import "ZGCyclePictureModel.h"
typedef void (^ZGCycleScrollViewSelectHandler)(ZGCyclePictureModel *model, NSInteger index);
typedef void (^ZGCycleScrollViewDidScrollHandler)(ZGCyclePictureModel *model, NSInteger index);
@interface ZGCycleScrollView : UIView
+ (ZGCycleScrollView *)showPictures:(NSArray<ZGCyclePictureModel *> *)pictures onSelect:(ZGCycleScrollViewSelectHandler)select didScroll:(ZGCycleScrollViewDidScrollHandler)scroll;
- (void)closeTimer;
@end
//
// ZGCycleScrollView.m
// SummaryDemo
//
// Created by zhaogang on 2018/4/28.
//
#import "ZGCycleScrollView.h"
#import "Masonry.h"
#import "SDWebImageManager.h"
#import "UIView+WebCache.h"
#import "UIImageView+WebCache.h"
#import "UIView+ZGTapGesture.h"
@interface ZGCycleScrollView()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pagecontrol;
@property (nonatomic, strong) NSTimer *showTimer;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *centerImageView;
@property (nonatomic, strong) UIImageView *rightImageView;
@property (nonatomic, strong) NSMutableArray *pictures;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, copy) ZGCycleScrollViewSelectHandler selectHandler;
@property (nonatomic, copy) ZGCycleScrollViewDidScrollHandler didScrollHandler;
@end
@implementation ZGCycleScrollView
+ (ZGCycleScrollView *)showPictures:(NSArray<ZGCyclePictureModel *> *)pictures onSelect:(ZGCycleScrollViewSelectHandler)select didScroll:(ZGCycleScrollViewDidScrollHandler)scroll
{
ZGCycleScrollView *view = [[ZGCycleScrollView alloc] initWithPictures:pictures onSelect:select didScroll:scroll];
return view;
}
- (instancetype)initWithPictures:(NSArray<ZGCyclePictureModel *> *)pictures onSelect:(ZGCycleScrollViewSelectHandler)select didScroll:(ZGCycleScrollViewDidScrollHandler)scroll
{
self = [super init];
if (self) {
self.selectHandler = select;
self.didScrollHandler = scroll;
[self creatUI];
self.pictures = pictures.mutableCopy;
}
return self;
}
- (void)creatUI
{
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO; // 防止滑动快的时候右侧出现白的
[self addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.bottom.equalTo(self);
}];
self.leftImageView = [[UIImageView alloc] init];
[self.leftImageView addTapTarget:self action:@selector(imageViewClicked:)];
[self.scrollView addSubview:self.leftImageView];
[self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.mas_equalTo(0);
make.width.mas_equalTo(self.mas_width);
make.height.mas_equalTo(self.mas_height);
}];
self.centerImageView = [[UIImageView alloc] init];
[self.centerImageView addTapTarget:self action:@selector(imageViewClicked:)];
[self.scrollView addSubview:self.centerImageView];
[self.centerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.mas_equalTo(self.leftImageView.mas_right);
make.width.mas_equalTo(self.mas_width);
make.height.mas_equalTo(self.mas_height);
}];
self.rightImageView = [[UIImageView alloc] init];
[self.rightImageView addTapTarget:self action:@selector(imageViewClicked:)];
[self.scrollView addSubview:self.rightImageView];
[self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.mas_equalTo(self.centerImageView.mas_right);
make.width.mas_equalTo(self.mas_width).priorityHigh();
make.right.mas_equalTo(0);
make.height.mas_equalTo(self.mas_height);
}];
//创建分页的标志(小红点)
self.pagecontrol = [[UIPageControl alloc] init];
self.pagecontrol.pageIndicatorTintColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.7];
self.pagecontrol.currentPageIndicatorTintColor = [UIColor redColor];
self.pagecontrol.backgroundColor = [UIColor clearColor];
[self addSubview:self.pagecontrol];
[self.pagecontrol mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(0);
make.bottom.mas_equalTo(-8);
make.height.mas_equalTo(20);
}];
}
#pragma mark - 设置展示的图片的数组
- (void)setPictures:(NSMutableArray *)pictures
{
_pictures = pictures;
if (_pictures.count == 0) {
[self closeTimer];
return;
}
self.currentIndex = 0; // 当前的位置
self.pagecontrol.numberOfPages = _pictures.count; //总分页数量
[self updateImages];
[self openTimer];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);//偏移
self.scrollView.contentSize = CGSizeMake(self.frame.size.width*3, self.frame.size.height);//总的页数
}
- (void)imageViewClicked:(UITapGestureRecognizer *)tap
{
NSInteger index = 0;
UIImageView *imageView = (UIImageView *)tap.view;
if (imageView == self.leftImageView) {
index = (self.currentIndex - 1 + _pictures.count) % _pictures.count;
}
else if (imageView == self.centerImageView) {
index = self.currentIndex;
}
else {
index = (self.currentIndex + 1) % _pictures.count;
}
if (self.selectHandler) {
self.selectHandler(_pictures[index], index);
}
}
#pragma mark - 定时器
- (void)openTimer
{
[self closeTimer];
self.showTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.showTimer forMode:NSRunLoopCommonModes];
}
- (void)closeTimer
{
[self.showTimer invalidate];
self.showTimer = nil;
}
- (void)timerAction:(int)counts
{
self.currentIndex = (self.currentIndex + 1) % _pictures.count;
[self updateImages];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scroll
{
// 开始拖拽 (防止滑到中间的时候过一会儿自动移动到下一张)
[self closeTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scroll
{
CGPoint offset = [self.scrollView contentOffset];
if (offset.x > self.scrollView.frame.size.width) { //向右滑动
self.currentIndex = (self.currentIndex + 1) % _pictures.count;
}
else if(offset.x < self.scrollView.frame.size.width){ //向左滑动
self.currentIndex = (self.currentIndex + _pictures.count - 1) % _pictures.count;
}
[self updateImages];
[self openTimer];
}
- (void)updateImages
{
ZGCyclePictureModel *leftModel = _pictures[(self.currentIndex - 1 + _pictures.count) % _pictures.count];
ZGCyclePictureModel *centerModel = _pictures[self.currentIndex];
ZGCyclePictureModel *rightModel = _pictures[(self.currentIndex + 1) % _pictures.count];
[self setImageWithModel:leftModel imageView:self.leftImageView];
[self setImageWithModel:centerModel imageView:self.centerImageView];
[self setImageWithModel:rightModel imageView:self.rightImageView];
[self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
self.pagecontrol.currentPage = self.currentIndex;
if (self.didScrollHandler) {
self.didScrollHandler(_pictures[self.currentIndex], self.currentIndex);
}
}
- (void)setImageWithModel:(ZGCyclePictureModel *)model imageView:(UIImageView *)imageView
{
if (model.type == ZGCyclePictureTypeLocal) {
imageView.image = model.image;
}
else {
imageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
[imageView sd_setShowActivityIndicatorView:YES];
[imageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray];
[imageView sd_setImageWithURL:model.imageUrl placeholderImage:[UIImage imageNamed:@"animation.png"]];
}
}
- (void)dealloc
{
}
@end
3、ZGCyclePictureModel的内容:
//
// ZGCyclePictureModel.h
// SummaryDemo
//
// Created by zhaogang on 2018/4/28.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, ZGCyclePictureType) {
ZGCyclePictureTypeUrl = 1,
ZGCyclePictureTypeLocal = 2,
};
@interface ZGCyclePictureModel : NSObject
@property (nonatomic, assign) ZGCyclePictureType type;
@property (nonatomic, strong) NSURL *imageUrl;
@property (nonatomic, strong) UIImage *image;
@end
//
// ZGCyclePictureModel.m
// SummaryDemo
//
// Created by zhaogang on 2018/4/28.
//
#import "ZGCyclePictureModel.h"
@implementation ZGCyclePictureModel
@end