1. 程式人生 > >150行程式碼搞定輪播圖

150行程式碼搞定輪播圖

思路:底層scrollview contentsize為3個螢幕寬 新增三個圖片檢視0,1,2  初始便偏移到中間檢視1(中間一個圖片檢視,左右各一個)   左滑或者右滑結束 scrollview都滑回原來位置,且無動畫,重新整理的是圖片檢視圖片地址,也就是每滑動一下需要處理的是陣列元素的迴圈移動。通過重新整理圖片檢視的圖片地址達到圖片無線滾動的檢視效果,,同時新增計時器自動滾動,,滑動時計時器銷燬,,滑動結束計時器開啟(sdwebimage 載入圖片有快取機制,只要圖片地址沒變網路載入只走一次)

圖片個數無限制

借用SDCycleScrollView的圖片來用用

自動滾動:

手動滾動:

計時器停止和啟動

demo :連結:https://pan.baidu.com/s/1wjgSHoO4V_OkIHC37RQyMw  密碼:60ah

建立UIview為  ScrollImageView

#import <UIImageView+WebCache.h>

@interface ScrollImageView()<UIScrollViewDelegate>

/// 底部滾動檢視

@property (nonatomic, strong) UIScrollView *backScrollView;

/// 判斷滾動方向

@property (nonatomic, assign) CGFloat contentOffsetX;

/// 圖片地址陣列

@property (nonatomic, strong) NSMutableArray *imageArr;

/// 點選連結地址陣列

@property (nonatomic, strong) NSMutableArray *imageUrlArr;

@property (nonatomic, strong) NSMutableArray *imageUrlsaveArr;

@property (nonatomic, strong) NSMutableArray *imagesaveArr;

@property (nonatomic, strong) NSMutableArray *imageViewArr;

@property (nonatomic, strong) NSMutableArray *tapGestureArr;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ScrollImageView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        self.imageViewArr = [NSMutableArray array];

        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

        [self createView];

    }

    return self;

}

- (void)reloadScrollView:(NSMutableArray *)imageArr urlArr:(NSMutableArray *)urlArr{

    self.imageArr = [NSMutableArray array];

    self.imageUrlArr = [NSMutableArray array];

    if (imageArr.count>0) {

        if (imageArr.count == 1) {

            for (NSInteger i =0; i<3; i++) {

                [self.imageArr addObject:imageArr[0]];

                [self.imageUrlArr addObject:urlArr[0]];

            }

            [self.timer invalidate];

            self.timer = nil;

            self.backScrollView.scrollEnabled = NO;

        }else if (imageArr.count == 2){

            for (NSInteger i = 0; i<4; i++) {

                if (i<2) {

                    [self.imageArr addObject:imageArr[i]];

                    [self.imageUrlArr addObject:urlArr[i]];

                }else{

                    [self.imageArr addObject:imageArr[i-2]];

                    [self.imageUrlArr addObject:urlArr[i-2]];

                }

            }

        }else{

            /// 對圖片陣列和點選連結地址處理 使第一張圖片顯示在三個圖片z檢視的中間位置

            for (NSInteger i = 0; i<imageArr.count; i++) {

                if (i == 0) {

                    [self.imageArr addObject:imageArr[imageArr.count-1]];

                    [self.imageUrlArr addObject:urlArr[urlArr.count-1]];

                }else{

                    [self.imageArr addObject:imageArr[i-1]];

                    [self.imageUrlArr addObject:urlArr[i-1]];

                }

            }

        }

        /// 對圖片檢視陣列中的UIimageview設定圖片

        for (NSInteger i = 0; i<3; i++) {

            UIImageView *image = self.imageViewArr[i];

            [image sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]];

        }

    }else{

        [self.timer invalidate];

        self.timer = nil;

        self.backScrollView.scrollEnabled = NO;

    }

}

- (void)createView{

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

    self.backScrollView.contentSize = CGSizeMake(self.frame.size.width*3, 0);

    [self addSubview:self.backScrollView];

    self.backScrollView.delegate = self;

    self.backScrollView.pagingEnabled = YES;

    self.backScrollView.showsHorizontalScrollIndicator = NO;

    ///固定只有三個圖片檢視

    [self setScrollViewContentOffsetCenter];

    for (NSInteger i = 0; i<3; i++) {

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

        image.userInteractionEnabled = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

        [image addGestureRecognizer:tap];

        image.tag = i;

        [self.imageViewArr addObject:image];

        [self.backScrollView addSubview:image];

    }

}

//// 圖片的點選代理方法

- (void)tapAction:(UITapGestureRecognizer *)tap{

    [self.delegate scrollImageViewTapAction:self.imageUrlArr[1]];

}

/// 偏移到中心位置

- (void)setScrollViewContentOffsetCenter {

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

}

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

    int contentOffsetX = scrollView.contentOffset.x;

    /// 左滑

    if(contentOffsetX >= (2 * self.frame.size.width)) {

        [self.backScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];

        [self reloadImage:1];

    }

    /// 右劃

    if(contentOffsetX <= 0) {

        [self.backScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];

        [self reloadImage:0];

    }

}

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

    [self.timer invalidate];

    self.timer = nil;

}

///  手指已經離開螢幕

- ( void )scrollViewDidEndDragging:( UIScrollView *)scrollView willDecelerate:(BOOL )decelerate{

        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

}

- (void)reloadImage:(NSInteger)type{

    if (type == 0) {

        /// 右劃 圖片陣列像右移動 第0個圖變成最後一個 第一個變成第0個 第二個變成第1個。。。。最後一個變成第0個

        self.imagesaveArr = [NSMutableArray arrayWithArray:self.imageArr];

        self.imageUrlsaveArr = [NSMutableArray arrayWithArray:self.imageUrlArr];

        self.imageArr = [NSMutableArray array];

        self.imageUrlArr = [NSMutableArray array];

        for (NSInteger i = 0; i<self.imagesaveArr.count; i++) {

            if (i == 0) {

                [self.imageArr addObject:self.imagesaveArr[self.imagesaveArr.count - 1]];

                [self.imageUrlArr addObject:self.imageUrlsaveArr[self.imageUrlsaveArr.count - 1]];

            }else{

                [self.imageArr addObject:self.imagesaveArr[i-1]];

                [self.imageUrlArr addObject:self.imageUrlsaveArr[i-1]];

            }

        }

        for (NSInteger i = 0; i<self.imageViewArr.count; i++) {

            UIImageView *image = self.imageViewArr[i];

            [image sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]];

        }

    }else{

        /// 左滑 圖片陣列像左移動 第0個圖變成第一個 第一個變成第2個 第二個變成第3個。。.。最後一個變成第0個

        self.imagesaveArr = [NSMutableArray arrayWithArray:self.imageArr];

        self.imageUrlsaveArr = [NSMutableArray arrayWithArray:self.imageUrlArr];

        self.imageUrlArr = [NSMutableArray array];

        self.imageArr = [NSMutableArray array];

        for (NSInteger i = 0; i<self.imagesaveArr.count; i++) {

            if(i == self.imagesaveArr.count - 1){

                [self.imageArr addObject:self.imagesaveArr[0]];

                [self.imageUrlArr addObject:self.imageUrlsaveArr[0]];

            }else{

                [self.imageArr addObject:self.imagesaveArr[i+1]];

                [self.imageUrlArr addObject:self.imageUrlsaveArr[i+1]];

            }

        }

        for (NSInteger i = 0; i<self.imageViewArr.count; i++) {

            UIImageView *image = self.imageViewArr[i];

            [image sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]];

        }

    }

}

- (void)timeAction{

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

}

controller使用

ScrollImageView *view2 = [[ScrollImageView alloc] initWithFrame:CGRectMake(0, 300,[UIScreen mainScreen].bounds.size.width , 200)];

    [view2 reloadScrollView:[NSMutableArray arrayWithObjects:@"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",

                             @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",

                             @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg", nil] urlArr:[NSMutableArray arrayWithObjects:@"0----------",@"1-----------",@"2-----------",@"3-----------", nil]];

    view2.delegate = self;

    [self.view addSubview:view2];