简单实现 scrollview无限轮播的封装

来源:互联网 发布:软件开发技术联盟 编辑:程序博客网 时间:2024/05/17 08:38

.h中

#import <UIKit/UIKit.h>

@class CarouselScrollView;

@protocol CarouseScrollViewDelegate <NSObject>

//轮播图被点击时触发的代理方法,index为点击的图片下标
-(void)carouseFiguteDidCarouse:(CarouselScrollView *)carouseFigure withIndex:(NSUInteger)index;

@end

@interface CarouselScrollView : UIView

//本地图片
+(instancetype)carouselScrollViewWithFrame:(CGRect)frame localPhotosArray:(NSArray *)photosArray;

//网络图片 ,默认使用SDWebImage工具, 请自行下载
+(instancetype)carouselScrollViewWithFrame:(CGRect)frame URLPhotosArray:(NSArray *)URLForPhotosArray;

//使用网络图片并使用代理
+(instancetype)carouselScrollViewWithFrame:(CGRect)frame URLPhotosArray:(NSArray *)URLForPhotosArray delegate:(id<CarouseScrollViewDelegate>)delegate;

//图片自动切换间隔时间, 默认设置为 2s
@property(assign ,nonatomic)NSTimeInterval duration;




@end


.m中

#import "CarouselScrollView.h"
#import "UIImageView+WebCache.h"

typedef NS_ENUM(NSInteger , CarouseImagesDataStyle){

    CarouseImagesDataInLocal,//本地图片标记
    CarouseImagesDataInURL   //URL图片标记
};

@interface CarouselScrollView ()<UIScrollViewDelegate>

@property(strong ,nonatomic)UIScrollView *scroll;
@property(strong ,nonatomic)UIPageControl *pageControl;

//存放图片数组
@property(strong ,nonatomic)NSArray *photosArray;
//存放图片数组的网址
@property(strong ,nonatomic)NSArray *URLForPhotosArray;
@property(weak ,nonatomic)id<CarouseScrollViewDelegate>delegate;

//前一个视图,当前视图,下一个视图
@property(strong ,nonatomic)UIImageView *lastImgView;
@property(strong ,nonatomic)UIImageView *currentImgView;
@property(strong ,nonatomic)UIImageView *nextImgView;

//图片来源
@property(nonatomic)CarouseImagesDataStyle carouseImagesStyle;

@property(strong ,nonatomic)NSTimer *timer;

//kCount = array.count,图片数组个数
@property(assign ,nonatomic)NSInteger kCount;

//显示nextImgView的下标 默认从 1开始
@property(assign,nonatomic )NSInteger photoPage;

@property(strong , nonatomic)UILabel *label;

@end

@implementation CarouselScrollView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _duration = 2;
        _timer = [NSTimer new];
    }
    return self;
}

+(instancetype)carouselScrollViewWithFrame:(CGRect)frame localPhotosArray:(NSArray *)photosArray{
    CarouselScrollView *carouseScroll =[[CarouselScrollView alloc]initWithFrame:frame];
    carouseScroll.photosArray = photosArray;
    return carouseScroll;
}

+(instancetype)carouselScrollViewWithFrame:(CGRect)frame URLPhotosArray:(NSArray *)URLForPhotosArray{
    CarouselScrollView *carouseScroll = [[CarouselScrollView alloc]initWithFrame:frame];
    carouseScroll.URLForPhotosArray = URLForPhotosArray;
    return carouseScroll;
}

+(instancetype)carouselScrollViewWithFrame:(CGRect)frame URLPhotosArray:(NSArray *)URLForPhotosArray delegate:(id<CarouseScrollViewDelegate>)delegate{
    CarouselScrollView *carouseScroll = [[CarouselScrollView alloc]initWithFrame:frame];
    carouseScroll.delegate = delegate;
    carouseScroll.URLForPhotosArray = URLForPhotosArray;
    return carouseScroll;
}

-(void)setDuration:(NSTimeInterval)duration{
    [self.timer invalidate];
    self.timer = nil;
    _duration = duration;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration target:self selector:@selector(timerAction) userInfo:self repeats:YES];
}

-(void)setPhotosArray:(NSArray *)photosArray{
//    timer停止
    [self.timer invalidate];
    self.timer = nil;
    if (_photosArray != photosArray) {
        _photosArray = nil;
        _photosArray = photosArray;
    }
    //标记图片来源
    self.carouseImagesStyle = CarouseImagesDataInLocal;
    //获取数组个数
    self.kCount = _photosArray.count;
    [self drawMyView];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration target:self selector:@selector(timerAction) userInfo:self repeats:YES];
}

-(void)setURLForPhotosArray:(NSArray *)URLForPhotosArray{
    
    [self.timer invalidate];
    self.timer = nil;
    if (_URLForPhotosArray != URLForPhotosArray) {
        _URLForPhotosArray = nil;
        _URLForPhotosArray = URLForPhotosArray;
    }
    //标记图片来源
    self.carouseImagesStyle = CarouseImagesDataInURL;
    self.kCount = _URLForPhotosArray.count;
    [self drawMyView];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration target:self selector:@selector(timerAction) userInfo:self repeats:YES];
}

#define  kWidth  self.bounds.size.width
#define  kHeight self.bounds.size.height
-(void)drawMyView{
    [self addSubview:self.scroll];
    [self addSubview:self.pageControl];
    self.photoPage = 1;
    
#pragma mark --- 自己可以添加东西
//        self.label = [[UILabel alloc]initWithFrame:CGRectMake(50, 500,kWidth-100, 30)];
//        self.label.backgroundColor = [UIColor redColor];
//        [self addSubview:self.label];
}
-(UIScrollView *)scroll{
    if (_scroll == nil) {
        _scroll = [[UIScrollView alloc]initWithFrame:self.bounds];
        _scroll.pagingEnabled = YES;
        _scroll.bounces = NO;
        _scroll.showsHorizontalScrollIndicator = NO;
        _scroll.showsVerticalScrollIndicator = NO;
        _scroll.contentSize = CGSizeMake(kWidth * 3, kHeight);
        //显示中间的图片
        _scroll.contentOffset = CGPointMake(kWidth, 0);
        _scroll.delegate = self;

        [_scroll addSubview:self.lastImgView];
        [_scroll addSubview:self.currentImgView];
        [_scroll addSubview:self.nextImgView];
    }
    return _scroll;
}
-(UIPageControl *)pageControl{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, kWidth, 30)];
        _pageControl.center = CGPointMake(kWidth/2.0, kHeight - 30/2.0);
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.numberOfPages = self.kCount;
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}
-(UIImageView *)lastImgView{
    if (_lastImgView == nil) {
        _lastImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        [self setImageView:_lastImgView withSubscript:(_kCount-1)];
    }
    return _lastImgView;
}
-(UIImageView *)currentImgView{
    
    if (_currentImgView == nil) {
        _currentImgView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeight)];
        [self setImageView:_currentImgView withSubscript:0];
        if (_delegate != nil) {
            //        给图片添加手势,
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapActionInImageView:)];
            //打开用户交互
            [_currentImgView addGestureRecognizer:tap];
            _currentImgView.userInteractionEnabled = YES;
        }
    }
    return _currentImgView;
}
-(UIImageView *)nextImgView{
    if (_nextImgView == nil) {
        _nextImgView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth*2, 0,kWidth , kHeight)];
        [self setImageView:_nextImgView withSubscript:1];
    }
    return _nextImgView;
}

//根据下标设置imgView的image
-(void)setImageView:(UIImageView *)imgView withSubscript:(NSInteger)subcript{
    if (self.carouseImagesStyle == CarouseImagesDataInLocal) {
        imgView.image = self.photosArray[subcript];
    } else{
        //网络图片设置, 如果要使用占位图请自行修改 使用方法二
        [imgView sd_setImageWithURL:[NSURL URLWithString:self.URLForPhotosArray[subcript]]];
        
        //方法二:
//        [imgView sd_setImageWithURL:[NSURL URLWithString:self.URLForPhotosArray[subcript]] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        
    }
}

#pragma mark --scroll代理事件
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //到第一个图片时
    if (scrollView.contentOffset.x <= 0) {
        _nextImgView.image = _currentImgView.image;
        _currentImgView.image = _lastImgView.image;
        scrollView.contentOffset = CGPointMake(kWidth, 0);
        _lastImgView.image = nil;
        if (_photoPage == 0) {
            _photoPage = _kCount-1 ;
        } else{
            _photoPage--;
        }
        [self setImageView:_lastImgView withSubscript:_photoPage];
    }
    //最后一个图片时
    if (scrollView.contentOffset.x  >= kWidth*2) {
        _lastImgView.image = _currentImgView.image;
        _currentImgView.image = _nextImgView.image;
        scrollView.contentOffset = CGPointMake(kWidth, 0);
        _nextImgView.image = nil;
        if (_photoPage == _kCount-1 ) {
            _photoPage = 0;
        } else{
            _photoPage++;
        }
        [self setImageView:_nextImgView withSubscript:_photoPage];
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //矫正pageCotrol的位置
    _pageControl.currentPage = _photoPage-1;
    if (_photoPage - 1 <0) {
        _pageControl.currentPage = _kCount-1;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration target:self selector:@selector(timerAction) userInfo:self repeats:YES];
}

#pragma mark -- timer事件
-(void)timerAction{

    [_scroll setContentOffset:CGPointMake(2*kWidth, 0) animated:YES];
    _pageControl.currentPage = _photoPage;
    
}
#pragma mark -- 手势点击事件
-(void)handleTapActionInImageView:(UITapGestureRecognizer *)tap{
    if (_delegate && [_delegate respondsToSelector:@selector(carouseFiguteDidCarouse:withIndex:)]) {
        if (_photoPage == 0) {
            [_delegate carouseFiguteDidCarouse:self withIndex:_kCount-1];
        }else{
            [_delegate carouseFiguteDidCarouse:self withIndex:_photoPage-1];
        }
    }
}


@end





0 0
原创粉丝点击