最近相对来说不是很忙,所以网上看了一些别人写的框架的源码,印象深刻的是SDCycleScrollView
这个第三方框架,写的也非常好,github上面的评星也5千多,查看了源码之后大概思路是用UICollectionView
来做的,按照分页滚动,传入多少张图片就乘以100,然后代码来操作UICollectionView
滚动到一半的位置,这样操作的话用户短时间内是滑不出这么多的;然后我手动滑动到最后一个测试过,还是有一闪而过的操作,个人感觉不太友好,(这里仅代表本人对这个框架的看法)所以今天我写了这个工具类,记录一下
思路:使用三个控件左右更换位置
SHInfiniteShufflingView.h
#import <UIKit/UIKit.h>
typedef void(^DidSlectIndex)(NSInteger index,NSString *url);
/*!
*无限循环图片轮播
*/
@interface SHInfiniteShufflingView : UIScrollView
/// 设置图片和间隔时间
/// @param dataArray 图片数组(自动识别远程地址和本地图片)
/// @param time 间隔时间,0代表不启动定时器(自动创建和销毁定时器)
- (void)setImageArray:(NSArray *)dataArray InteralTime:(NSInteger)time;
/*!
*页码
*/
@property (nonatomic , strong) UIPageControl *pageControl;
/*!
*点击回调
*/
@property (nonatomic , copy) DidSlectIndex didSlect;
@end
SHInfiniteShufflingView.m
#import "SHInfiniteShufflingView.h"
#import <SDWebImage.h>
@interface SHInfiniteShufflingView ()<UIScrollViewDelegate>
@property (nonatomic , strong) NSMutableArray *subViewArray;
@property (nonatomic , assign) BOOL isCanRolling;
@property (nonatomic , copy) NSArray *imageArray;
@property (nonatomic , assign) NSInteger intervalSecond;
@end
@implementation SHInfiniteShufflingView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_isCanRolling = YES;
self.pagingEnabled = YES;
self.bounces = NO;
self.delegate = self;
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
[self addSubview:self.pageControl];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.pageControl.frame = CGRectMake(self.contentOffset.x, self.frame.size.height-30, self.frame.size.width, 30);
}
- (void)setImageArray:(NSArray *)dataArray InteralTime:(NSInteger)time
{
if (dataArray.count == 0) return;
_imageArray = dataArray; //初始化控件
if (_imageArray.count == 1) {
UIImageView *imageView = [[UIImageView alloc] init];
NSString *imageUrl = _imageArray.lastObject;
if ([imageUrl containsString:@"https://"] || [imageUrl containsString:@"http://"]) {
[imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
}else{
imageView.image = [UIImage imageNamed:imageUrl];
}
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panClick)]];
imageView.frame = self.bounds;
[self addSubview:imageView];
}else{
self.contentSize = CGSizeMake(self.frame.size.width*3,self.frame.size.height);
[self setContentOffset:CGPointMake(self.frame.size.width, 0)];
for (NSInteger i = 0; i < 3; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height)];
NSString *imageUrl = _imageArray[i == 0 ? [self getLessNum] : i == 1 ? self.pageControl.currentPage : [self getMoreNum]];
if ([imageUrl containsString:@"https://"] || [imageUrl containsString:@"http://"]) {
[imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
}else{
imageView.image = [UIImage imageNamed:imageUrl];
}
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panClick)]];
[self addSubview:imageView];
[self.subViewArray addObject:imageView];
}
}
[self bringSubviewToFront:self.pageControl];
self.pageControl.numberOfPages = _imageArray.count;
if (self.imageArray.count > 1 && time > 0) {
[NSTimer scheduledTimerWithTimeInterval:time repeats:YES block:^(NSTimer * _Nonnull timer) {
if (self.isCanRolling) {
[self setContentOffset:CGPointMake(self.frame.size.width*2, 0) animated:YES];
}
}];
}
}
- (void)panClick
{
!self.didSlect ? : self.didSlect(self.pageControl.currentPage,self.imageArray[self.pageControl.currentPage]);
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self layoutIfNeeded];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == 0 || scrollView.contentOffset.x == 2 * self.frame.size.width) {
[self layoutSubview];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == 0 || scrollView.contentOffset.x == 2 * self.frame.size.width) {
[self layoutSubview];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
_isCanRolling = NO;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
_isCanRolling = YES;
}
- (void)layoutSubview{
if (self.contentOffset.x == 0 || self.imageArray.count == 0) {
self.pageControl.currentPage = self.pageControl.currentPage == 0 ? self.imageArray.count-1 : self.pageControl.currentPage-1;
[self.subViewArray exchangeObjectAtIndex:0 withObjectAtIndex:self.subViewArray.count-1];
[self.subViewArray exchangeObjectAtIndex:1 withObjectAtIndex:self.subViewArray.count-1];
UIImageView *imageView = self.subViewArray.firstObject;
NSString *imageUrl = self.imageArray[[self getLessNum]];
if ([imageUrl containsString:@"http://"] || [imageUrl containsString:@"https://"]) {
[imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
}else{
imageView.image = [UIImage imageNamed:imageUrl];
}
}else{
self.pageControl.currentPage = self.pageControl.currentPage == self.imageArray.count-1 ? 0 : self.pageControl.currentPage+1;
[self.subViewArray exchangeObjectAtIndex:1 withObjectAtIndex:self.subViewArray.count-1];
[self.subViewArray exchangeObjectAtIndex:0 withObjectAtIndex:self.subViewArray.count-1];
UIImageView *imageView = self.subViewArray.lastObject;
NSString *imageUrl = self.imageArray[[self getMoreNum]];
if ([imageUrl containsString:@"http://"] || [imageUrl containsString:@"https://"]) {
[imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
}else{
imageView.image = [UIImage imageNamed:imageUrl];
}
}
NSInteger coun = self.subViewArray.count;
for (NSInteger i = 0; i < coun; i++) {
UIImageView *imageView = self.subViewArray[i];
imageView.frame = CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height);
}
[self setContentOffset:CGPointMake(self.frame.size.width, 0)];
}
#pragma mark - lazy
- (NSMutableArray *)subViewArray
{
if (!_subViewArray) {
_subViewArray = [NSMutableArray array];
}
return _subViewArray;
}
- (UIPageControl *)pageControl
{
if (!_pageControl) {
_pageControl = [[UIPageControl alloc] init];
_pageControl.hidesForSinglePage = YES;
_pageControl.currentPage = 0;
_pageControl.numberOfPages = 1;
}
return _pageControl;
}
#pragma mark - customMetod
- (NSInteger)getMoreNum{
return self.pageControl.currentPage == self.imageArray.count-1 ? 0 : self.pageControl.currentPage+1;
}
- (NSInteger)getLessNum
{
return self.pageControl.currentPage == 0 ? self.imageArray.count-1 : self.pageControl.currentPage-1;
}
@end
代码量不多就没有单独写demo,小伙伴们可以直接复制过去就能运行,UI层面的封装,简单适配一下SDWebImage
等相关框架,自动识别本地图片文件和远程图片文件,有点击事件block回调
使用
SHInfiniteShufflingView *shufflingView = [[SHInfiniteShufflingView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 300)];
shufflingView.backgroundColor = UIColor.lightGrayColor;
[shufflingView setImageArray:@[@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3984473917,238095211&fm=26&gp=0.jpg",
@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2462146637,4274174245&fm=26&gp=0.jpg",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596172460861&di=e7f4f1f6046e639b148a24af0d985443&imgtype=0&src=http%3A%2F%2Ffile02.16sucai.com%2Fd%2Ffile%2F2014%2F0929%2F0befba7c8a1702dec85affb2ef4cd90b.jpg",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596172460856&di=ab20841811dc58d90a1121dff4373295&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F48%2F69%2F01300000169041121120698749544.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596189818908&di=7de8f60ed9c1930e0442f0d721b0453c&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F56%2F12%2F01300000164151121576126282411.jpg"] InteralTime:3];
shufflingView.didSlect = ^(NSInteger index, NSString *url) {
NSLog(@"--%ld----%@",index,url);
};
[self.view addSubview:shufflingView];
完事,收工