首頁先上效果圖

1.gif
主要功能:
- 實(shí)現(xiàn)自動(dòng)輪播與手動(dòng)切換模式
- 設(shè)置自動(dòng)輪播時(shí)間
- 支持點(diǎn)擊事件
1.創(chuàng)建3個(gè)View,分別用來顯示前一張照片、當(dāng)前照片、后一張照片
if (_currentPage==0) {
_firstView = [_imageArray lastObject];
_middleView = [_imageArray objectAtIndex:_currentPage];
_lastView = [_imageArray objectAtIndex:_currentPage+1];
}
else if (_currentPage == _imageArray.count-1)
{
_firstView = [_imageArray objectAtIndex:_currentPage-1];
_middleView = [_imageArray objectAtIndex:_currentPage];
_lastView = [_imageArray firstObject];
}
else
{
_firstView = [_imageArray objectAtIndex:_currentPage-1];
_middleView = [_imageArray objectAtIndex:_currentPage];
_lastView = [_imageArray objectAtIndex:_currentPage+1];
}
//設(shè)置三個(gè)view的frame,加到scrollview上
_firstView.frame = CGRectMake(0, 0, kViewWidth, KViewHeight);
_middleView.frame = CGRectMake(kViewWidth, 0, kViewWidth, KViewHeight);
_lastView.frame = CGRectMake(kViewWidth*2, 0, kViewWidth, KViewHeight);
[_scrollView addSubview:_firstView];
[_scrollView addSubview:_middleView];
[_scrollView addSubview:_lastView];
//顯示中間的那一張圖片,也就是CurrentPage
_scrollView.contentOffset = CGPointMake(kViewWidth, 0);
2.實(shí)現(xiàn)ScorollView的代理方法,在停止拖拽的時(shí)候調(diào)用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//手動(dòng)滑動(dòng)時(shí)候暫停自動(dòng)替換
[_autoScrollTimer invalidate];
_autoScrollTimer = nil;
_autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
//得到當(dāng)前頁數(shù)
float x = _scrollView.contentOffset.x /kViewWidth;
//往前翻
if (x <= 0) {
if (_currentPage-1 < 0) {
_currentPage = _imageArray.count-1;
}else{
_currentPage --;
}
}
//往后翻
if (x >= 2) {
if (_currentPage == _imageArray.count-1) {
_currentPage = 0;
}else{
_currentPage ++;
}
}
self.isDragging = YES;
[self reloadData];
}
3.實(shí)現(xiàn)定時(shí)器自動(dòng)滾動(dòng)
-(void)setIsAutoScroll:(BOOL)isAutoScroll
{
_isAutoScroll = isAutoScroll;
if (isAutoScroll) {
if (!_autoScrollTimer) {
_autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
}
}else {
if (_autoScrollTimer.isValid) {
[_autoScrollTimer invalidate];
_autoScrollTimer = nil;
}
}
4.定時(shí)器執(zhí)行的自動(dòng)切換的方法
-(void)autoShowNextImage
{
if (_currentPage == _imageArray.count-1) {
_currentPage = 0;
}else{
_currentPage ++;
}
[self reloadData]; //更新數(shù)據(jù)、重新展示
}
5.動(dòng)畫優(yōu)化
設(shè)立一個(gè)標(biāo)志位isDragging,默認(rèn)不拖拽的情況下isDragging為NO,執(zhí)行切換動(dòng)畫。
在調(diào)用scrollViewDidEndDecelerating:(UIScrollView *)scrollView后設(shè)置isDragging為YES,不執(zhí)行動(dòng)畫。
if (!self.isDragging) {
_scrollView.contentOffset = CGPointMake(0, 0);
[UIView animateWithDuration:0.5 animations:^{
_scrollView.contentOffset = CGPointMake(kViewWidth, 0);
} completion:^(BOOL finished) {
}];
}else
{
_scrollView.contentOffset = CGPointMake(kViewWidth, 0);
self.isDragging = NO;
}
6.代理的實(shí)現(xiàn),監(jiān)聽點(diǎn)擊事件,回傳CurrentPage
_tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
_tapGesture.numberOfTapsRequired = 1;
_tapGesture.numberOfTouchesRequired = 1;
[_scrollView addGestureRecognizer:_tapGesture];
-(void)handleTap:(UITapGestureRecognizer*)sender
{
if ([self.delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
[self.delegate didClickPage:self atIndex:_currentPage];
}
}
7.最后附上Demo的github地址,歡迎大家Star
github項(xiàng)目地址