iOS最笨的办法实现无限轮播图(网络加载)

时间:2021-11-16 03:43:07

iOS最笨的办法实现无限轮播图(网络加载)

简单的做了一下:

使用方法: 把 请求返回的 图片地址(字符串类型)放进数组中就行 
可以使用SDWebImage(我就是用的这个)等。。需要自己导入并引用,然后修改部分代码 
.h文件

//  ScrollViewTimerView.h
// ScrollViewTimer
//
// Created by 郑鹏 on 2016/12/9.
// Copyright © 2016年 郑鹏. All rights reserved.
// #import <UIKit/UIKit.h> @protocol ScrollViewTimerViewDelegate <NSObject> - (void)didSelectScrollViewWithSelectIndex:(NSInteger)selectIndex; @end @interface ScrollViewTimerView : UIView
@property (nonatomic, weak) id<ScrollViewTimerViewDelegate> littleSunDelegate; - (instancetype)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration;
@property (nonatomic, strong) NSMutableArray * wheelImgArray;//轮播图 数组 //是否需要
@property (nonatomic, assign) BOOL isHidePageControl; @end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

.m文件

//
// ScrollViewTimerView.m
// ScrollViewTimer
//
// Created by 郑鹏 on 2016/12/9.
// Copyright © 2016年 郑鹏. All rights reserved.
// #import "ScrollViewTimerView.h"
#import "UIImageView+WebCache.h" @interface ScrollViewTimerView ()<UIScrollViewDelegate>{ NSTimer *_animationTimer;
UIScrollView *_littleSunScrollView;
UIPageControl *_pageControl;
} @end @implementation ScrollViewTimerView - (instancetype)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration{
self = [super initWithFrame:frame]; _littleSunScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
_littleSunScrollView.delegate = self;
_littleSunScrollView.pagingEnabled = YES;
_littleSunScrollView.showsHorizontalScrollIndicator = NO;
_littleSunScrollView.showsVerticalScrollIndicator = NO;
_littleSunScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
[self addSubview:_littleSunScrollView];
_animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationDuration
target:self
selector:@selector(animationTimerDidFired:)
userInfo:nil
repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:UITrackingRunLoopMode];
[self pauseTimer]; return self;
}
- (void)animationTimerDidFired:(NSTimer *)timer{
CGPoint newOffset = CGPointMake(_littleSunScrollView.contentOffset.x + CGRectGetWidth(self.frame), _littleSunScrollView.contentOffset.y);
[_littleSunScrollView setContentOffset:newOffset animated:YES]; if (_littleSunScrollView.contentOffset.x >= (_wheelImgArray.count+1)*self.frame.size.width) {
[self startTimerAfterTimeInterval:-2.0f];
[_littleSunScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}
} #pragma mark- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self pauseTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self startTimerAfterTimeInterval:2.0f];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (_littleSunScrollView.contentOffset.x >= (_wheelImgArray.count+1)*self.frame.size.width) {
[_littleSunScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}else if (_littleSunScrollView.contentOffset.x <= 0) {
[_littleSunScrollView setContentOffset:CGPointMake(_wheelImgArray.count*self.frame.size.width, 0) animated:NO];
}
if (!_isHidePageControl) {
_pageControl.currentPage = [self getCurrentPageBy:scrollView.contentOffset.x];
}
} //公开的属性设置
- (void)setWheelImgArray:(NSMutableArray *)wheelImgArray{
if (_wheelImgArray != wheelImgArray) {
_wheelImgArray = wheelImgArray;
_littleSunScrollView.contentSize = CGSizeMake(self.frame.size.width * (wheelImgArray.count + 2), self.frame.size.height);
for (NSInteger i = 0; i < wheelImgArray.count+2; i++) {
if (i == 0) {
[self creatImgViewWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 + wheelImgArray.count - 1 WithImgName:wheelImgArray[wheelImgArray.count - 1]];
}else if(i != wheelImgArray.count+1){
[self creatImgViewWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 + i-1 WithImgName:wheelImgArray[i-1]];
}else if(i == wheelImgArray.count+1){
[self creatImgViewWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 WithImgName:wheelImgArray[0]];
}
} _pageControl = [[UIPageControl alloc] init];
_pageControl.numberOfPages = _wheelImgArray.count;
_pageControl.frame = CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 10);
_pageControl.currentPage = 0;
_pageControl.userInteractionEnabled = NO;
[_pageControl setPageIndicatorTintColor:[UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:0.5]];
[_pageControl setCurrentPageIndicatorTintColor:[UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:1]];
[self addSubview:_pageControl]; [self startTimerAfterTimeInterval:2.0f];
}
}
- (void)creatImgViewWithFrame:(CGRect)frame WithTag:(NSInteger)tag WithImgName:(NSString *)imgName{ UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
imgView.userInteractionEnabled = YES;
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.tag = tag;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickItem:)];
[imgView addGestureRecognizer:tap];
NSURL *imgUrl = [NSURL URLWithString:imgName];
[imgView sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageNamed:@"jiazai"]];
[_littleSunScrollView addSubview:imgView];
}
-(void)setisHidePageControl:(BOOL)isHidePageControl{
if (_isHidePageControl != isHidePageControl) {
_isHidePageControl = isHidePageControl;
if (_isHidePageControl) {
[_pageControl removeFromSuperview];
}
}
} //通过 计算 获取当前页
- (NSInteger)getCurrentPageBy:(CGFloat)scrollViewContentOfX{
if (scrollViewContentOfX < self.frame.size.width) {
return _wheelImgArray.count - 1;
}else if (scrollViewContentOfX >= self.frame.size.width && scrollViewContentOfX < (_wheelImgArray.count+1)*self.frame.size.width) {
return scrollViewContentOfX/self.frame.size.width - 1;
}else{
return 0;
}
} //_animationTimer暂停
-(void)pauseTimer{
if (![_animationTimer isValid]) {
return ;
}
[_animationTimer setFireDate:[NSDate distantFuture]];
} //_animationTimer开始
-(void)startTimer{
if (![_animationTimer isValid]) {
return ;
}
[_animationTimer setFireDate:[NSDate date]];
}
//_animationTimer在多久后开始
- (void)startTimerAfterTimeInterval:(NSTimeInterval)interval{
if (![_animationTimer isValid]) {
return ;
}
[_animationTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
} #pragma mark- 实现代理
- (void)clickItem:(UITapGestureRecognizer *)tap{
UIView *view = tap.view;
if ([self.littleSunDelegate respondsToSelector:@selector(didSelectScrollViewWithSelectIndex:)]) {
[self.littleSunDelegate didSelectScrollViewWithSelectIndex:view.tag - 20161212];
} }
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ @end