无限循环的图片轮播器

#import "ViewController.h"

#define scrollW self.myScrollow.bounds.size.width

#define scrollH self.myScrollow.bounds.size.height


@interface ViewController ()<UIScrollViewDelegate>


@property (weak, nonatomic) IBOutlet UIScrollView *myScrollow;


@property (nonatomic, weak) UIImageView *leftImageV;

@property (nonatomic, weak) UIImageView *currentImageV;

@property (nonatomic, weak) UIImageView *rightImageV;

@property(nonatomic, strong)NSMutableArray *images;

@property (nonatomic, strong)NSTimer *timer;


@property(nonatomic, assign)int leftIndex;

@property(nonatomic, assign)int rightIndex;

@property(nonatomic, assign)int curIndex;

@property(nonatomic, assign)int imageCount;


@end


@implementation ViewController

// 加载图片

- (NSMutableArray *)images

{

    if (_images == nil) {

       

        _images = [NSMutableArray array];

        

        for (int i = 0; i<8; i++) {

            NSString *str = [NSString stringWithFormat:@"%d",i+1];

        

        UIImage *image = [UIImage imageNamed:str];


        [_images addObject:image];

          

    }

    }

      self.imageCount = (int)_images.count;

        return _images;

}


- (void)viewDidLoad {

    [super viewDidLoad];

// 设置scrollView

    [self setScrollow];

// 添加子控件

    [self setUp];

    

    int curIndex = 0;

    self.curIndex = curIndex;

//  设置scrollView默认显示的内容

    self.leftImageV.image = self.images[self.imageCount - 1];

    self.currentImageV.image = self.images[self.curIndex];

    self.rightImageV.image = self.images[self.curIndex + 1];

// 添加定时器

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


}

- (void)timeDoThing

{

//    self.curIndex++;

    NSLog(@"%s",__func__);

    [self scrollViewDidEndDecelerating:self.myScrollow];

}

// 设置scrollView的属性

- (void)setScrollow

{

    self.myScrollow.contentSize = CGSizeMake(scrollW * 3, scrollH);

    self.myScrollow.backgroundColor = [UIColor blueColor];

  

    //  分页

    self.myScrollow.pagingEnabled = YES;

    //  隐藏滚动条

    self.myScrollow.showsHorizontalScrollIndicator = NO;

    self.myScrollow.showsVerticalScrollIndicator = NO;

    //  取消弹簧效果

    self.myScrollow.bounces = NO;

    

    self.myScrollow.delegate = self;

    //  程序已启动展现在用户眼前的是curV

    [self.myScrollow setContentOffset:CGPointMake(scrollW, 0)];


}

// 添加子控件

- (void)setUp

{

    //  添加imageView

    UIImageView *leftV = [[UIImageView alloc] init];

    self.leftImageV = leftV;

    leftV.backgroundColor = [UIColor redColor];

    leftV.frame = CGRectMake(0, 0, scrollW, scrollH);

    [self.myScrollow addSubview:leftV];

    

    UIImageView *curV  = [[UIImageView alloc] init];

    curV.frame = CGRectMake(scrollW * 1, 0, scrollW, scrollH);

    curV.backgroundColor = [UIColor greenColor];

    [self.myScrollow addSubview:curV];

    self.currentImageV= curV;

    

    UIImageView *rightV  = [[UIImageView alloc] init];

    rightV.frame = CGRectMake(scrollW *2, 0, scrollW, scrollH);

    rightV.backgroundColor = [UIColor blueColor];

    [self.myScrollow addSubview:rightV];

    self.rightImageV = rightV;


}

static int num = 0;

// UIscrollow完全停止滚动时调用

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    

    //  设置图片

    if (self.myScrollow.contentOffset.x > scrollW) {//

        self.curIndex = (self.curIndex + 1)%self.imageCount;

        if (num >=7) {

            num = 0;

        }

        num++;

        NSLog(@" = %d",num);

    }else //

    {

        if (num <= 0) {

            num = 7;

        }

        num--;

        NSLog(@" = %d == %d",num ,self.curIndex);

        self.curIndex = (self.curIndex + self.imageCount - 1)%self.imageCount;

    }

    

    self.leftIndex = (self.curIndex + self.imageCount - 1)%self.imageCount;

    self.rightIndex = (self.curIndex + 1)%self.imageCount;

    

    self.currentImageV.image = self.images[self.curIndex];

    self.leftImageV.image = self.images[self.leftIndex];

    self.rightImageV.image = self.images[self.rightIndex];

    

/**********************/

    //  复位

    [self.myScrollow setContentOffset:CGPointMake(scrollW, 0)];

    

}

// 用户开始拖拽时停止定时器

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [self.timer invalidate];

}

// 用户结束拖拽时开启定时器

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

{

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

}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值