博客> 无限轮播封装
无限轮播封装
2017-09-21 06:56 评论:0 阅读:290 飞翔信念
无限轮播封装

因为项目中用到了无限轮播,正好有个之前有个同事项目中用到了,拿来分享下,方便调用,封装到了一个类里面直接调用就可以了

1.h文件

#import <UIKit>
@protocol myviewDelegate;
@interface myview : UIView
@property(weak,nonatomic)id<myviewDelegate>delegate;
-(instancetype)initWithFrame:(CGRect)frame;
-(void) setupWithArray:(NSArray *)array;
-(void) setupWithLocalArray:(NSArray *)array;
+(instancetype)initWithFrame:(CGRect)frame withArray:(NSArray*) array hasTimer:(BOOL) hastimer interval:(NSUInteger) inter;
+(instancetype)initWithFrame:(CGRect)frame hasTimer:(BOOL) hastimer interval:(NSUInteger) inter placeHolder:(UIImage*) image;
@end

@protocol myviewDelegate <NSObject>

- (void) carouselTouch:(myview*)carousel atIndex:(NSUInteger)index;

@end

2.m文件

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

@interface myview()<UIScrollViewDelegate>
@property BOOL hasTimer;
@property (assign, nonatomic) NSUInteger interval;

@property (strong, nonatomic) UIImage *placeHolder;
@property (strong, nonatomic) NSArray * imageArray;
@property (strong, nonatomic) UIScrollView *wheelScrollView;
@property (strong, nonatomic) UIPageControl *wheelPageControl;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) NSUInteger currentImageIndex;
@property (strong, nonatomic) UIImageView *image1;
@property (strong, nonatomic) UIImageView *image2;
@property (strong, nonatomic) UIImageView *image3;
@property (assign, nonatomic) NSUInteger imageNum;
@property (strong, nonatomic) UIImageView *mask;
@property (assign, nonatomic) BOOL isLocal;
@end

@implementation myview
+(instancetype)initWithFrame:(CGRect)frame withArray:(NSArray*) array hasTimer:(BOOL)hastimer interval:(NSUInteger)inter{
    myview * carousel = [[myview alloc] initWithFrame:frame];
    carousel.hasTimer = hastimer;
    carousel.interval = inter;
    [carousel setupWithArray:array];
    return carousel;
}

+(instancetype)initWithFrame:(CGRect)frame hasTimer:(BOOL)hastimer interval:(NSUInteger)inter placeHolder:(UIImage *)image{

    myview * carousel = [[myview alloc] initWithFrame:frame];
    carousel.placeHolder = image;
    carousel.hasTimer = hastimer;
    carousel.interval = inter;
    carousel.mask.image = image;
    return carousel;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.mask = [[UIImageView alloc] initWithFrame:frame];
        [self addSubview:self.wheelScrollView];
        [self addSubview:self.wheelPageControl];
        [self addSubview:self.mask];
        self.wheelScrollView.scrollEnabled = NO;
    }
    return self;
}

-(void) setupWithArray:(NSArray *)array{
    self.wheelScrollView.scrollEnabled = YES;
    self.mask.hidden = YES;
    self.imageArray = array;
    self.imageNum = self.imageArray.count;
    self.currentImageIndex = 0;

    if (self.imageNum == 1) {
        self.wheelPageControl.hidden = YES;
        self.wheelScrollView.scrollEnabled = NO;
    }

    [self setup];
}

-(void) setupWithLocalArray:(NSArray *)array{
    self.isLocal = YES;
    self.wheelScrollView.scrollEnabled = YES;
    self.mask.hidden = YES;
    self.imageArray = array;
    self.imageNum = self.imageArray.count;
    self.currentImageIndex = 0;

    if (self.imageNum == 1) {
        self.wheelPageControl.hidden = YES;
        self.wheelScrollView.scrollEnabled = NO;
    }

    [self setup];
}

-(void) setup{
    self.wheelPageControl.pageIndicatorTintColor = [UIColor whiteColor];
    self.wheelPageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    CGPoint p = CGPointMake(self.frame.size.width * 0.5, 0.8 * self.frame.size.height);
    self.wheelPageControl.center = p;
    if (self.hasTimer) {
        [self setupTimer];
    }
    [self updateImage];
}

- (void) updateImage{
    self.imageNum = (int)self.imageArray.count;
    self.wheelPageControl.numberOfPages = self.imageNum;
    [self updateScrollImage];
}

- (void) updateWheel{
    CGPoint offset = self.wheelScrollView.contentOffset;
    offset.x += self.wheelScrollView.frame.size.width;
    [self.wheelScrollView setContentOffset:offset animated:YES];
}

-(void) updateScrollImage{
    int left;
    int right;
    int page = self.wheelScrollView.contentOffset.x / self.wheelScrollView.frame.size.width;
    if (page == 0) {
        self.currentImageIndex = (self.currentImageIndex + self.imageNum - 1) % self.imageNum;
    }else if(page == 2){
        self.currentImageIndex = (self.currentImageIndex + 1) % self.imageNum;
    }

    left = (int)(self.currentImageIndex + self.imageNum -1) % self.imageNum;
    right = (int)(self.currentImageIndex + 1) % self.imageNum;
    if (self.isLocal) {
        self.image1.image = [UIImage imageNamed:self.imageArray[left]];
        self.image2.image = [UIImage imageNamed:self.imageArray[self.self.currentImageIndex]];
        self.image3.image = [UIImage imageNamed:self.imageArray[right]];
    }else{
        [self.image1  setImageWithURL:[NSURL URLWithString:self.imageArray[left]]  placeholderImage:self.placeHolder];
        [self.image2 setImageWithURL:[NSURL URLWithString:self.imageArray[self.currentImageIndex]] placeholderImage:self.placeHolder];
        [self.image3 setImageWithURL:[NSURL URLWithString:self.imageArray[right]] placeholderImage:self.placeHolder];
    }

    self.wheelPageControl.currentPage = self.currentImageIndex;
    [self.wheelScrollView setContentOffset:CGPointMake(self.wheelScrollView.frame.size.width, 0) animated:NO];
}

-(void) setupTimer{
    if (self.interval == 0) {
        self.interval = 3;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(updateWheel) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

-(void) touch{
    if ([self.delegate respondsToSelector:@selector(carouselTouch:atIndex:)]) {
        [self.delegate carouselTouch:self atIndex:self.currentImageIndex];
    }
}

-(void) destroy{
    [self.timer invalidate];
}

#pragma mark - UIScrollView
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self updateScrollImage];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self updateScrollImage];
}

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

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self setupTimer];
}
//添加点击事件
#pragma mark - Getter
-(UIScrollView *)wheelScrollView{
    if (!_wheelScrollView) {
        _wheelScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _wheelScrollView.backgroundColor = [UIColor clearColor];
        _wheelScrollView.pagingEnabled = YES;
        _wheelScrollView.delegate = self;
        _wheelScrollView.showsHorizontalScrollIndicator = NO;
        _wheelScrollView.showsVerticalScrollIndicator = NO;

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

        _image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _image2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        _image3 = [[UIImageView alloc] initWithFrame:CGRectMake(2*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        _image2.image = self.placeHolder;

        for (UIImageView * img in @[_image1,_image2,_image3]) {
            [_wheelScrollView addSubview:img];
        }

        [_wheelScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        _wheelScrollView.scrollEnabled = YES;
        _wheelScrollView.contentSize = CGSizeMake(3*self.frame.size.width, self.frame.size.height);
    }
    return _wheelScrollView;
}

-(UIPageControl *)wheelPageControl{
    if (!_wheelPageControl) {
        _wheelPageControl = [[UIPageControl alloc] init];
        [_wheelPageControl setBackgroundColor:[UIColor clearColor]];
        _wheelPageControl.currentPage = 0;
        _wheelPageControl.numberOfPages = self.imageNum;
    }
    return _wheelPageControl;
}

@end

3.调用

(1)在要调用的类里面实现代理myviewDelegate然后创建一个全局的 myview *headview

headView=[myview initWithFrame:CGRectMake(0, 64, DeviceWidth, 188) hasTimer:YES interval:2 placeHolder:[UIImage imageNamed:@""]];
    headView.delegate=self;
    [self.view addSubview:headView];
    NSArray * imgArray=[[NSArray alloc]initWithObjects:@"http://pic1.nipic.com/2008-12-25/2008122510134038_2.jpg",@"http://pic42.nipic.com/20140607/18358876_140318247306_2.jpg",@"http://pic1.nipic.com/2008-12-25/2008122510134038_2.jpg", nil];
    [headView setupWithArray:imgArray];

(2)实现点击图片的代理方法

#pragma mark myview代理方法
- (void) carouselTouch:(myview*)carousel atIndex:(NSUInteger)index
{
    NSLog(@"第%lu张图片点击了",(unsigned long)index);
}
收藏
0
sina weixin mail 回到顶部