類似淘寶首頁輪播圖的實(shí)現(xiàn)

首頁先上效果圖


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)目地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,832評論 4 61
  • 今天好累呀,把廚房門框上和上面的壁紙貼了,還是不喜歡廚房的燈
    蒲公英花開閱讀 133評論 0 0
  • 社會(huì)是片汪洋大海,把很多曾經(jīng)相連的人們隔成了孤島。
    CristPenn閱讀 133評論 0 1
  • 我認(rèn)識一個(gè)叫阿飛的人。 每次見到阿飛都穿顏色很淺很淺的牛仔褲,配著一件干凈又溫暖的男士純棉上衣。一米七多,瘦瘦的,...
    涵丹尼閱讀 549評論 0 2

友情鏈接更多精彩內(nèi)容