#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface ViewController : UIViewController<UIScrollViewDelegate>
@property (strong,nonatomic) UIScrollView *myScorolV;
@property (strong,nonatomic) UIPageControl *pageC;
@property (strong,nonatomic) NSMutableArray *arrPage;
@property (strong,nonatomic) UIImageView *threeImage;
@property (strong,nonatomic) UIImageView *secondImage;
@property (strong,nonatomic) UIImageView *firstImage;
@property (assign,nonatomic) int currentPage;
@end
//滚动视图创建
self.myScorolV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
self.myScorolV.contentSize = CGSizeMake(WIDTH * 3, 0);
self.myScorolV.pagingEnabled = YES;
self.myScorolV.showsHorizontalScrollIndicator = NO;
self.myScorolV.delegate = self;
[self.view addSubview:self.myScorolV];
//分页视图
self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 5 * 2, HEIGHT - 80, WIDTH / 5, 40)];
self.pageC.backgroundColor = [UIColor clearColor];
self.pageC.currentPage = 0;
self.pageC.numberOfPages = 5;
//指定页码颜色
self.pageC.currentPageIndicatorTintColor = [UIColor redColor];
self.pageC.pageIndicatorTintColor = [UIColor greenColor];
[self.view addSubview:self.pageC];
self.arrPage = [NSMutableArray arrayWithCapacity:10];
for (int i = 1; i < 6; i++)
{
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
[self.arrPage addObject:image];
}
self.firstImage = [[UIImageView alloc] init];
self.secondImage = [[UIImageView alloc] init];
self.threeImage = [[UIImageView alloc] init];
self.currentPage = 0;
[self relodeImage];
}
-(void)relodeImage
{
if (self.currentPage == 0)
{
self.firstImage.image = [self.arrPage lastObject];
self.secondImage.image = [self.arrPage objectAtIndex:self.currentPage];
self.threeImage.image = [self.arrPage objectAtIndex:self.currentPage + 1];
}
else if (self.currentPage == self.arrPage.count - 1)
{
self.firstImage.image = [self.arrPage objectAtIndex:self.currentPage - 1];
self.secondImage.image = [self.arrPage objectAtIndex:self.currentPage];
self.threeImage.image = [self.arrPage objectAtIndex:0];
}
else
{
self.firstImage.image = [self.arrPage objectAtIndex:self.currentPage - 1];
self.secondImage.image = [self.arrPage objectAtIndex:self.currentPage];
self.threeImage.image = [self.arrPage objectAtIndex:self.currentPage + 1];
}
self.firstImage.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
self.secondImage.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
self.threeImage.frame = CGRectMake(WIDTH * 2, 0, WIDTH, HEIGHT);
[self.myScorolV addSubview:self.firstImage];
[self.myScorolV addSubview:self.secondImage];
[self.myScorolV addSubview:self.threeImage];
self.myScorolV.contentOffset = CGPointMake(WIDTH, 0);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int number = (int)self.myScorolV.contentOffset.x;
if (number < 0 || number == 0)
{
if (self.currentPage == 0)
{
self.currentPage = (int)(self.arrPage.count - 1);
}
else
{
self.currentPage--;
}
}
else if (number > WIDTH * 2 || number == WIDTH * 2)
{
if (self.currentPage == (int)self.arrPage.count - 1)
{
self.currentPage = 0;
}
else
{
self.currentPage++;
}
}
self.pageC.currentPage = self.currentPage;
[self relodeImage];
}