需要調(diào)用的頁面
#import "ViewController.h"
#import "SeliceInfininiteScrolloView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//實(shí)現(xiàn)調(diào)用方法 將圖片數(shù)組傳入
SeliceInfininiteScrolloView *scrollowView = [[SeliceInfininiteScrolloView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 200)];
scrollowView.imageArray = @[@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png"];
[self.view addSubview:scrollowView];
}
@end
自定義方法 SeliceInfininiteScrolloView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SeliceInfininiteScrolloView : UIView
@property(nonatomic,strong) NSArray * imageArray;//圖片數(shù)組
@end
NS_ASSUME_NONNULL_END
自定義方法 SeliceInfininiteScrolloView.m
#import "SeliceInfininiteScrolloView.h"
@interface SeliceInfininiteScrolloView()<UIScrollViewDelegate>{
UIScrollView *MainScrollowView;
UIImageView *leftImageView;
UIImageView *MidImageView;
UIImageView *RightImageView;
NSTimer *timer;
UIPageControl *pageControl;
NSInteger currentIndex;
NSInteger count;
}
@end
@implementation SeliceInfininiteScrolloView
static const int viewNumber = 3;
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
currentIndex = 0;
}
return self;
}
-(void)setImageArray:(NSArray *)imageArray{
_imageArray = imageArray;
count = imageArray.count;
// 新建UIScrollowView
[self creatScrollowView];
// 新建定時(shí)器
[self creatTimer];
// 新建UIPageControl
[self creatPagecontrol];
}
#pragma mark 新建UIScrollowView
-(void)creatScrollowView{
// 當(dāng)你滑動(dòng)的時(shí)候代兵,不滑動(dòng)出scrollowView的時(shí)候 那么是不是就不會(huì)出現(xiàn)紅色背景
MainScrollowView = [[UIScrollView alloc]initWithFrame:self.bounds];
MainScrollowView.pagingEnabled = YES;
// 背景顏色的出現(xiàn),本質(zhì)是:滑動(dòng)過界了厌漂,超過了scrollowView的contentsize所以才會(huì)出現(xiàn)背景色
MainScrollowView.bounces = NO;
MainScrollowView.backgroundColor = [UIColor redColor];
MainScrollowView.showsVerticalScrollIndicator = NO;
MainScrollowView.showsHorizontalScrollIndicator = NO;
MainScrollowView.delegate = self;
MainScrollowView.contentSize = CGSizeMake(viewNumber*self.frame.size.width, self.frame.size.height);
[self addSubview:MainScrollowView];
// 添加圖片
leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
leftImageView.image = [UIImage imageNamed:_imageArray[count-1]];
MidImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
MidImageView.image = [UIImage imageNamed:_imageArray[0]];
RightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
RightImageView.image = [UIImage imageNamed:_imageArray[1]];
[MainScrollowView addSubview:leftImageView];
[MainScrollowView addSubview:MidImageView];
[MainScrollowView addSubview:RightImageView];
// 剛剛開始的時(shí)候設(shè)置偏移量
MainScrollowView.contentOffset = CGPointMake(self.frame.size.width, 0.f);
}
#pragma mark 新建定時(shí)器
-(void)creatTimer{
__weak typeof(self)weakSelf = self;
if (@available(iOS 10.0, *)) {
timer = [NSTimer timerWithTimeInterval:2.f repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakSelf timerAction];
}];
} else {
}
[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)timerAction{
[MainScrollowView scrollRectToVisible:CGRectMake(2*self.frame.size.width, 0.f, self.frame.size.width, self.frame.size.height) animated:YES];
}
-(void)invalidateTimer{
[timer invalidate];
timer = nil;
}
#pragma mark 新建UIPageControl
-(void)creatPagecontrol{
CGFloat pageControlHeight = 20.f;
CGFloat pageControlWidth = 80.f;
pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(20, self.frame.size.height-pageControlHeight, pageControlWidth, pageControlHeight)];
pageControl.numberOfPages = count;
pageControl.currentPage = 0.f;
pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
[self addSubview:pageControl];
}
#pragma mark UIScrollView delegate method
//手動(dòng)滑動(dòng)停止減速的時(shí)候調(diào)用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == 2 * self.frame.size.width) {
//滑動(dòng)到最右邊時(shí)候
currentIndex ++;
//重置圖片內(nèi)容 修改偏移量
[self resetImages];
}else if (scrollView.contentOffset.x==0){
currentIndex = currentIndex + count;
currentIndex --;
[self resetImages];
}
}
//定時(shí)器滑動(dòng)調(diào)用
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x ==2*self.frame.size.width) {
//滑動(dòng)到最右邊時(shí)候
currentIndex ++;
//重置圖片內(nèi)容 修改偏移量
[self resetImages];
}
}
#pragma mark //重置圖片內(nèi)容 修改偏移量
-(void)resetImages{
leftImageView.image = [UIImage imageNamed:_imageArray[(currentIndex-1)%count]];
MidImageView.image = [UIImage imageNamed:_imageArray[(currentIndex)%count]];
RightImageView.image = [UIImage imageNamed:_imageArray[(currentIndex+1)%count]];
MainScrollowView.contentOffset = CGPointMake(self.frame.size.width, 0.f);
pageControl.currentPage = (currentIndex)%count;//設(shè)置page控件當(dāng)前位置
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//準(zhǔn)備拖動(dòng)的時(shí)候演熟,定時(shí)器失效
[self invalidateTimer];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//停止拖動(dòng)的時(shí)候飘诗,定時(shí)器開啟
[self creatTimer];
}
@end