轮播图第三方代码

在iOS移动应用开发过程中,轮播图的使用非常频繁,网上现在流传着很多版本的轮播图第三方。但是作为第三方,进行修改的时候会遇到很多问题,所以自己写了一个轮播图的第三方,来适应自己的各种代码需求。

在重写初始化中实现外部图片名称或者网络URL的传入,同时也传入了UIPageControl的位置

//重写初始化
- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array withDistance:(CGFloat)distance
{
    self = [super initWithFrame:frame];
    if (self) {
        _array = [[NSMutableArray alloc] initWithArray:array];
        _distance = distance;
        
        [self addSubview:self.scrollView];
        [self.scrollView release];
        
        [self loadPicture];
        
//scrollView懒加载
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        //    设置代理人
        _scrollView.delegate = self;
        //    设置整页偏移
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
    }
    return _scrollView;
}

[self addSubview:self.pageControl]; [self.pageControl release]; } return self;}
 
  
加载scrollView
//scrollView懒加载
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        //    设置代理人
        _scrollView.delegate = self;
        //    设置整页偏移
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
    }
    return _scrollView;
}


 
  

通过for循环实现图片的加载

//加载图片
- (void)loadPicture {
    if (self.array.count) {
        [self.array insertObject:[self.array lastObject] atIndex:0];
        [self.array addObject:self.array[1]];
        for (int i = 0; i < self.array.count; i++) {
            if ([self.array[i] isKindOfClass:[NSString class]]) {
                UIImageView *imageViews = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
                imageViews.image = [UIImage imageNamed:self.array[i]];
                imageViews.contentMode = UIViewContentModeScaleAspectFill;
                imageViews.clipsToBounds = YES;
                [self.scrollView addSubview:imageViews];
                [imageViews release];
                
            }
        }
        _count = self.array.count;
        self.scrollView.contentSize = CGSizeMake(WIDTH * _count, 0);
    }
}

如果需要从网络请求数据,则使用SDImageView的方法

pageControl的方法

//pageControl懒加载
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT - _distance, WIDTH, 20)];
        _pageControl.numberOfPages = _count - 2;
        [_pageControl addTarget:self action:@selector(changePageByPageControl:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}

//pageControl方法
- (void)changePageByPageControl:(UIPageControl *)sender {
    self.scrollView.contentOffset = CGPointMake(WIDTH * (sender.currentPage + 1), 0);
}

实现轮播

//实现轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == WIDTH * (_count - 1)) {
        scrollView.contentOffset = CGPointMake(WIDTH, 0);
    } else if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(WIDTH * (_count - 2), 0);
    }
//    根据图片改currentPage
    self.pageControl.currentPage = self.scrollView.contentOffset.x / WIDTH - 1;
}

实现自动播放的功能

//自动播放
- (void)setTimer:(NSTimeInterval)timeinterval {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(timerCarousel) userInfo:nil repeats:YES];
    
    [timer fire];
}
//自动播放方法
- (void)timerCarousel {
    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0);
    if (self.scrollView.contentOffset.x > WIDTH * (_count - 2)) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    self.pageControl.currentPage = self.scrollView.contentOffset.x / WIDTH - 1;
}
自动播放的功能需要使用方法

- (void)setTimer:(NSTimeInterval)timeinterval;

来进行设置轮播时间



你可能感兴趣的:(轮播图第三方代码)