1. 程式人生 > >iOS最笨的辦法實現無限輪播圖(網路載入)

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

.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