无限轮播

作者: __夏至未至 | 来源:发表于2016-05-13 09:35 被阅读422次

    最近在准备毕业论文和毕业设计的事情,也是比较忙,许久未写博客了。之前一直想写一个自己的无限轮播,于是乎写出来和大家分享一下。

    思路

    总体思路很简单,重写了个scrollview。里面设置三个imageview,显示的是中间那个imageView的图像。当滚动结束后,把当前显示的imageView的image转给中间的imageView,并且设置好两边的imageView
    设置了两个style,一个是给string,本地图片,一个是传url,结合SDwebImage,显示网络图片。

    typedef NS_ENUM(NSInteger, LpScrollViewStyle){
    LpSCrollVIewUrlStyle ,
    LpSCrollVIewStringStyle};
    

    初始化三个imageView,设置好位置和大小(用懒加载的方式)。

    - (UIImageView *)leftImageView{
    if (_leftImageView == nil) {
        _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, IMAGEHEIGHT)];
    }
    return _leftImageView;
    }
    - (UIImageView *)midImageView{
    if (_midImageView == nil) {
        _midImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, IMAGEHEIGHT)];
    }
    return _midImageView;
    }
    - (UIImageView *)rightImageView{
    if (_rightImageView == nil) {
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH * 2, 0, SCREENWIDTH, IMAGEHEIGHT)];
    }
    return _rightImageView;
    }
    

    根据传入的数组初始化每个imageView的image。
    - (void)set_imageWithString{
    NSInteger count = self.imageArray.count;
    if (count == 1) {
    self.midImageView.image = [UIImage imageNamed:self.imageArray[0]];
    self.leftImageView.image = [UIImage imageNamed:self.imageArray[0]];
    self.rightImageView.image = [UIImage imageNamed:self.imageArray[0]];
    }else if(count == 2){
    self.midImageView.image = [UIImage imageNamed:self.imageArray[0]];
    self.leftImageView.image = [UIImage imageNamed:self.imageArray[1]];
    self.rightImageView.image = [UIImage imageNamed:self.imageArray[1]];
    }else{
    self.midImageView.image = [UIImage imageNamed:self.imageArray[0]];
    self.leftImageView.image = [UIImage imageNamed:self.imageArray[count - 1]];
    self.rightImageView.image = [UIImage imageNamed:self.imageArray[1]];
    }
    }
    - (void)set_imageWithUrl{
    NSInteger count = self.imageArray.count;
    if (count == 1) {
    [self.midImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]] placeholderImage:[UIImage imageNamed:@""]];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]] placeholderImage:[UIImage imageNamed:@""]];
    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]] placeholderImage:[UIImage imageNamed:@""]];
    }else if(count == 2){
    [self.midImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]] placeholderImage:[UIImage imageNamed:@""]];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[1]] placeholderImage:[UIImage imageNamed:@""]];
    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[1]] placeholderImage:[UIImage imageNamed:@""]];
    }else{
    [self.midImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]] placeholderImage:[UIImage imageNamed:@""]];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[count - 1]] placeholderImage:[UIImage imageNamed:@""]];
    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[1]] placeholderImage:[UIImage imageNamed:@""]];
    }

    }
    

    设置好定时器,用于计时作用,由于有时候人为的拖动需要重置定时器,所以设置了isTimeUp布尔类型的成员变量对他进行控制。
    - (void)set_timer{
    if (self.imageArray.count != 1) {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:changeImageTime target:self selector:@selector(imageViewMoved) userInfo:nil repeats:YES];
    isTimeUp = NO;
    }
    }

    - (void)imageViewMoved{
    [self setContentOffset:CGPointMake(SCREENWIDTH * 2, 0) animated:YES];
    isTimeUp = YES;
    [NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(scrollViewDidEndDecelerating:) userInfo:nil repeats:NO];
    }
    

    定时器方法调用的是UIScrollViewDelegate里的方法,当滑动结束后,会自动去更新imageView显示的内容,并且把scrollviewcontentoffset设置到中间的imageView

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.contentOffset.x == SCREENWIDTH * 2) {
        if (index == self.imageArray.count - 1) {
            index = 0;
        }else{
            index = index + 1;
        }
    }else{
        if (index == 0) {
            index = self.imageArray.count - 1;
        }else{
            index = index - 1;
        }
        }
    
    self.pageControl.currentPage = index % 3;
    
    if (self.lpStyle == LpSCrollVIewStringStyle) {
        [self string_Decelerating];
    }else{
        [self url_decelerating];
    }
    
    self.contentOffset = CGPointMake(SCREENWIDTH, 0);
    
    if (!isTimeUp) {
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:changeImageTime]];
    }
    isTimeUp = NO;
    }
    
    - (void)string_Decelerating{
    NSInteger count = self.imageArray.count;
    if (index == 0) {
        _leftImageView.image = [UIImage imageNamed:_imageArray[(count - 1)%count]];
        _midImageView.image = [UIImage imageNamed:_imageArray[(index )%count]];
        _rightImageView.image = [UIImage imageNamed:_imageArray[(index + 1)%count]];
    }else{
        _leftImageView.image = [UIImage imageNamed:_imageArray[(index - 1)%count]];
        _midImageView.image = [UIImage imageNamed:_imageArray[(index )%count]];
        _rightImageView.image = [UIImage imageNamed:_imageArray[(index + 1)%count]];
    }
    }
    
    - (void)url_decelerating{
    NSInteger count = self.imageArray.count;
    if (index == 0) {
        [_leftImageView sd_setImageWithURL:self.imageArray[(count-1)%count]];
        [_midImageView sd_setImageWithURL:self.imageArray[index%count]];
        [_rightImageView sd_setImageWithURL:self.imageArray[(index+1)%count]];
    }else{
        [_leftImageView sd_setImageWithURL:self.imageArray[(index-1)%count]];
        [_midImageView sd_setImageWithURL:self.imageArray[index%count]];
        [_rightImageView sd_setImageWithURL:self.imageArray[(index+1)%count]];
    }
    }
    

    结束语

    非常感谢各位的观看,欢迎交流,附上demo地址:https://github.com/ioscick/LpScrollview-Demo

    相关文章

      网友评论

      本文标题:无限轮播

      本文链接:https://www.haomeiwen.com/subject/bugprttx.html