#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