效果圖:
效果圖
代碼:
創(chuàng)建一個(gè)UIView類.h中
//圖片數(shù)組&frame
-(instancetype)initWithFrame:(CGRect)frame imageNames:(NSArray *)imageNames;
//時(shí)間間隔&循環(huán)滾動(dòng)
- (void)scrollWithTimeInterval:(NSTimeInterval)timeInterval;
.m
#import "ZDH_ScrollView.h"
#define SCROLL_WIDTH CGRectGetWidth(_scrollView.frame)
#define SCROLL_HEIGHT CGRectGetHeight(_scrollView.frame)
#pragma mark - interface
@interface ZDH_ScrollView()<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
UIPageControl *_pageControl; //-->分頁
NSInteger imageNum; //-->圖片數(shù)量
NSTimer *_timer; //-->定時(shí)器
NSInteger _currentPage; //-->當(dāng)前顯示的頁數(shù)
NSTimeInterval _timeInterval;//-->時(shí)間間隔
}
@end
@implementation ZDH_ScrollView
#pragma mark - 初始化自定義方法
-(instancetype)initWithFrame:(CGRect)frame imageNames:(NSArray *)imageNames{
self = [super initWithFrame:frame];
if (self) {
[self commonInitWithImageNames:imageNames];
[self commonInitPageControl];
// 剪裁超出范圍的視圖
self.clipsToBounds = YES;
}
return self;
}
#pragma mark - 定時(shí)器
//根據(jù)指定的時(shí)間間隔觸發(fā)定時(shí)器實(shí)現(xiàn)自動(dòng)循環(huán)滾動(dòng)
-(void)scrollWithTimeInterval:(NSTimeInterval)timeInterval{
_timeInterval = timeInterval;
_timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
}
//重新設(shè)定定時(shí)器的激活時(shí)間
-(void)resetTimerFireDate{
NSDate *nDate= [NSDate dateWithTimeIntervalSinceNow:_timeInterval];
_timer.fireDate = nDate;
}
#pragma mark - 系列初始化方法都在這里
//初始化 ImageView 并添加到主視圖上
-(void)commonInitWithImageNames:(NSArray *)imageNames{
//獲取圖片的個(gè)數(shù)
imageNum = imageNames.count;
//初始化 滾動(dòng)視圖
[self commonInitScrollView];
//添加 UIimageView
for (int i=0; i<imageNum +2 ; i++) {
//最左側(cè)的視圖顯示最后一張圖片
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(i*CGRectGetWidth(self.frame), 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
//圖片添加規(guī)律為,最左側(cè)視圖上顯示最后一張圖,最右側(cè)視圖顯示第一張圖
imageV.image = [UIImage imageNamed:imageNames[i==0?(imageNum - 1):(i == (imageNum + 1)?0:(i-1))]];
[_scrollView addSubview:imageV];
}
//默認(rèn)顯示第二個(gè)視圖
[self showScrollViewWithPage:1];
}
// 初始化 UIScrollView 對(duì)象
- (void)commonInitScrollView
{
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
[self addSubview:_scrollView];
_scrollView.delegate = self;
// 設(shè)置UIScrollView 翻頁效果
_scrollView.pagingEnabled = YES;
// 設(shè)置內(nèi)容寬度 contentSize
// 為讓scrollView循環(huán)滾動(dòng)巡揍,在最左側(cè)添加一個(gè)視圖矩形赏廓,在最右側(cè)添加一個(gè)視圖矩形碑幅,所以imageNum+2
_scrollView.contentSize = CGSizeMake((imageNum+2)*CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
// 設(shè)置水平、垂直滑動(dòng)滾動(dòng)條不顯示
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
}
// 初始化 UIPageControl
- (void)commonInitPageControl
{
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-50, CGRectGetWidth(self.frame), 20)];
// 設(shè)置 UIPageControl 指示點(diǎn)的顏色
_pageControl.pageIndicatorTintColor = [UIColor whiteColor];
// 設(shè)置 UIPageControl 指示點(diǎn)的高亮顏色(當(dāng)前頁的顏色)
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
// 設(shè)置總頁數(shù)
_pageControl.numberOfPages = imageNum;
// 設(shè)置當(dāng)前默認(rèn)顯示的頁數(shù)
_pageControl.currentPage = 0;
// 添加目標(biāo)方法
[_pageControl addTarget:self action:@selector(pageControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_pageControl];
}
#pragma mark - 有關(guān)滾動(dòng)的一些設(shè)置在這里
// 根據(jù) UIPageControl 指定頁 顯示 UIScrollView 的視圖
- (void)pageControlValueChanged:(UIPageControl*)pageC
{
[self showScrollViewWithPage:pageC.currentPage+1];
}
// 自定方法庸论,用于根據(jù)當(dāng)前頁數(shù)顯示指定的 UIScrollView 頁面
- (void)showScrollViewWithPage:(NSUInteger)page
{
_currentPage = page;
// 預(yù)防超出總頁數(shù)
if (page >= imageNum+1) {
return;
}
// 根據(jù)當(dāng)前給定的 page 獲取相對(duì)應(yīng)的矩形區(qū)域(將要顯示的區(qū)域)
CGRect visibleRect = CGRectMake(page*SCROLL_WIDTH, 0, SCROLL_WIDTH, SCROLL_HEIGHT);
// 讓 scrollView 滑動(dòng)到指定要顯示的區(qū)域
[_scrollView scrollRectToVisible:visibleRect animated:YES];
}
// 自動(dòng)滾動(dòng)
- (void)autoScroll
{
// 通過設(shè)定 UIView 的動(dòng)畫效果實(shí)現(xiàn) offset移動(dòng)效果
[UIView animateWithDuration:.5 animations:^{
[_scrollView setContentOffset:CGPointMake(++_currentPage*SCROLL_WIDTH, _scrollView.contentOffset.y)];
}];
[self scrollViewDidEndDecelerating:_scrollView];
// 判斷當(dāng)前頁計(jì)算量大于理論總頁數(shù)時(shí),重新賦值
if (_currentPage > imageNum) {
_currentPage=1;
}
}
#pragma mark - UIScrollViewDelegate
// 當(dāng) UIScrollView 滾動(dòng)后激發(fā)該方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 手動(dòng)滾動(dòng)視圖后需要重新為定時(shí)器設(shè)定激活時(shí)間
[self resetTimerFireDate];
// 根據(jù)內(nèi)容 offset 的 x 值計(jì)算當(dāng)前視圖的頁數(shù)
NSUInteger page = scrollView.contentOffset.x/CGRectGetWidth(self.frame);
// 如果是最左側(cè)視圖(顯示最后一張圖片),scrollView 滾動(dòng)到倒數(shù)第二個(gè)視圖上(理論中的最后一張圖)
if (page==0) {
CGRect newRect = CGRectMake(imageNum*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
// 讓scrollView 滾動(dòng)到指定的區(qū)域
[_scrollView scrollRectToVisible:newRect animated:NO];
// 設(shè)置 UIPageControl 當(dāng)前頁
_pageControl.currentPage = imageNum - 1;
_currentPage = _pageControl.currentPage+1;
return;
}
// 如果是最右側(cè)視圖(顯示第一張圖片),scrollView 滾動(dòng)到第二個(gè)視圖上(理論中的第一張圖)
else if(page==imageNum+1){
CGRect newRect = CGRectMake(CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
[_scrollView scrollRectToVisible:newRect animated:NO];
// 設(shè)置 UIPageControl 當(dāng)前頁
_pageControl.currentPage = 0;
_currentPage = _pageControl.currentPage+1;
return;
}
// 設(shè)置 UIPageControl 當(dāng)前頁
_pageControl.currentPage = page - 1;
_currentPage = _pageControl.currentPage+1;
}
到這里就封裝好了,只需要在控制器里面調(diào)用就可以了
低能
"
導(dǎo)入頭文件
初始化賦值
加到視圖上
"
還不明白看Demo