参考了这个链接里的内容:http://www.cocoachina.com/ios/20150803/12879.html
@interface ViewController ()<UIScrollViewDelegate>
@property (strong,nonatomic)UIScrollView *scrollView;
@property (assign,nonatomic)CGSize size;
@property (strong,nonatomic)UIImageView *imageViewLef;
@property (strong,nonatomic)UIImageView *imageViewCenter;
@property (strong,nonatomic)UIImageView *imagViewRight;
@property (strong,nonatomic)NSMutableArray *arrayM;
@property (assign,nonatomic)CGFloat beginX;
@property (assign,nonatomic)NSInteger page;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.arrayM = [NSMutableArrayarray];
self.page =0;
[self setupPic];
[selfsetupScrollView];
}
/**
* 设置scrollView
*/
-(void)setupScrollView
{
CGRect rect = [UIScreenmainScreen].bounds;
self.size = rect.size;
self.scrollView = [[UIScrollViewalloc]initWithFrame:rect];
[self.scrollViewsetContentSize:CGSizeMake(self.size.width *3 , self.size.height)];
[self.scrollViewsetShowsHorizontalScrollIndicator:NO];
[self.scrollViewsetPagingEnabled:YES];
[self.scrollViewsetDelegate:self];
[self.scrollViewsetContentOffset:CGPointMake(self.size.width,0)];
[self.viewaddSubview:self.scrollView];
[selfsetupImageView];
[selfreloadImageView];
}
/**
* 设置scrollView中的三个imageView
*/
-(void)setupImageView
{
self.imageViewLef = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, self.size.width,self.size.height)];
[self.imageViewLefsetContentMode:UIViewContentModeScaleAspectFill];
[self.scrollViewaddSubview:self.imageViewLef];
self.imageViewCenter = [[UIImageViewalloc]initWithFrame:CGRectMake(self.size.width,0, self.size.width,self.size.height)];
[self.imageViewLefsetContentMode:UIViewContentModeScaleAspectFill];
[self.scrollViewaddSubview:self.imageViewCenter];
self.imagViewRight = [[UIImageViewalloc]initWithFrame:CGRectMake(self.size.width * 2, 0, self.size.width,self.size.height)];
[self.imageViewLefsetContentMode:UIViewContentModeScaleAspectFill];
[self.scrollViewaddSubview:self.imagViewRight];
[self.imageViewLefsetBackgroundColor:[UIColorredColor]];
[self.imageViewCentersetBackgroundColor:[UIColorblueColor]];
[self.imagViewRightsetBackgroundColor:[UIColorgrayColor]];
}
/**
* 设置图片
*/
-(void)setupPic
{
for (NSInteger i =1; i <= 4; i++) {
UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"new_feature_%ld",i]];
[self.arrayMaddObject:image];
}
}
/**
* 重新加载图片
*/
-(void)reloadImageView
{
if (self.page >=4 ) {
self.page =0;
}
if (self.page <0) {
self.page =3;
}
NSInteger pre = self.page - 1;
NSInteger now = self.page;
NSInteger next = self.page + 1;
if (pre < 0) {
pre = 3;
}
if (next >= 4) {
next = 0;
}
[self.imageViewLefsetImage:self.arrayM[pre]];
[self.imageViewCentersetImage:self.arrayM[now]];
[self.imagViewRightsetImage:self.arrayM[next]];
[self.scrollViewsetContentOffset:CGPointMake(self.size.width,0)];
self.beginX =self.size.width;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self operation:scrollView];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"x %f",scrollView.contentOffset.x);
NSLog(@"beginX %f",self.beginX);
/*
* 当快速拖动时,边上会出现白边,然后再有图片,
* 当快速左右拖动时,x会大于或者小于beginX,如果是正常拖动x是等于beginX
*/
CGFloat x = scrollView.contentOffset.x;
if (x > self.beginX || x <self.beginX) {
[self operation:scrollView];
}
}
/**
* 前后拖动时的处理
*/
-(void)operation:(UIScrollView *)scrollView
{
CGFloat x = scrollView.contentOffset.x;
if (x > self.beginX) {
NSLog(@"后");
self.page++;
}
if (x < self.beginX) {
NSLog(@"前");
self.page--;
}
self.beginX = x;
[selfreloadImageView];
}