iOS: 無限迴圈輪播圖簡單封裝

來源:互聯網
上載者:User

標籤:

輪播圖思路:

1.首先要有一個ScrollView和兩張圖片,把圖片放到ScrollView上。

2.要想滾動ScrollView的寬度需設定屏寬的2倍。

3.迴圈滾動,當滾動到最後一張圖片時,採用位移的方法,將位移量歸零,回到第一張圖片。

4.將第二張的圖片上的所有值賦給第一張圖片。

5.設定讓它自己滾動,需添加定時器。

需要的第三方資料庫:SDWebImage

 

m.檔案內:

#imporst "ScrollView.h"

@interface ScrollView ()<UIScrollViewDelegate>

//滾動視圖

@property (nonatomic,strong) UIScrollView *scrollView;

//圖片

@property (nonatomic,strong) UIImageView *leftImageView;

//圖片

@property (nonatomic,strong) UIImageView *rightImageView;

//指示小圓點

@property (nonatomic,strong) UIPageControl *pageControl;

//下標

@property (nonatomic,assign) NSInteger index;

//定時器

@property (nonatomic,strong) NSTimer *timer;

//接收外面傳進來的值

@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation ScrollView

//滾動視圖的懶載入

- (UIScrollView *)scrollView{

    if (!_scrollView) {

        //初始化滾動視圖

        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

        //設定代理

        _scrollView.delegate = self;

        //取消反彈效果

        _scrollView.bounces = NO;

        _scrollView.bouncesZoom = NO;

        //設定整頁翻滾

        _scrollView.pagingEnabled = YES;

        //滾動範圍

        _scrollView.contentSize = CGSizeMake(self.frame.size.width*2, 0);

        //捲軸的關閉

        _scrollView.showsHorizontalScrollIndicator = NO;

    }

    return _scrollView;

}

//左邊圖片懶載入

- (UIImageView *)leftImageView{

    if (!_leftImageView) {

        //初始化圖片框架

        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

        //添加圖片(寫這步需要引入第三方資料庫)

        [_leftImageView sd_setImageWithURL:[NSURL URLWithString: self.dataArray[0]]];

    }

    return _leftImageView;

}

//右邊圖片懶載入

- (UIImageView *)rightImageView{

    if (!_rightImageView) {

        //初始化圖片框架

        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.leftImageView.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];

        //添加圖片(寫這步需要引入第三方資料庫)

        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.index]]];

    }

    return _rightImageView;

}

//小圓點指示標懶載入

- (UIPageControl *)pageControl{

    if (!_pageControl) {

        //初始化小圓點

        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, self.scrollView.frame.size.height-50, self.frame.size.width-100, 50)];

        //    設定頁數

        _pageControl.numberOfPages = self.dataArray.count;

        //    設定圓點指標的小圓點顏色

        _pageControl.pageIndicatorTintColor = [UIColor redColor];

        //    設定當前小圓點的顏色

        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

    }

      return _pageControl;

}

//定時器懶載入

- (NSTimer *)timer{

    if (!_timer) {

        //定時時間及事件

        _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

    }

    return _timer;

}

//重寫父類

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray{

    if ([super initWithFrame:frame]) {

        //第一張圖片

        self.index = 1;

        //將外面的值引入

        self.dataArray = dataArray;

        [self creatScrollViewWithFrame:frame];

    }

    return self;

}

//建立滾動視圖

- (void)creatScrollViewWithFrame:(CGRect)frame  {

    [self.scrollView addSubview:self.leftImageView];

    [self.scrollView addSubview:self.rightImageView];

    [self addSubview:self.scrollView];

    [self addSubview:self.pageControl];

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self.timer fire];

    });

}

//代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

     //如果滾動視圖的位移量為螢幕寬

    if (scrollView.contentOffset.x == self.frame.size.width) {

        //返回0

        self.scrollView.contentOffset = CGPointZero;

        //當前小圓點為下標

        self.pageControl.currentPage = self.index;

        //下標加+

        self.index++;

        //數組的下標設定為index

        if (self.index == self.dataArray.count) {

            //從0開始

            self.index = 0;

        }

        //圖片之間傳值

    self.leftImageView.image = self.rightImageView.image;

    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.index]]];

    }

}

 //定時器事件

- (void)timerAction{

     [self.scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];

}

 h.檔案內:

#import <UIKit/UIKit.h>

@interface ScrollView : UIView

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray;

@end

 

iOS: 無限迴圈輪播圖簡單封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.