@interface ViewController : UIViewController<UIScrollViewDelegate>
@property (nonatomic ,strong) NSMutableArray *array;
@property (nonatomic ,strong) UIImageView *imageView1;
@property (nonatomic ,strong) UIImageView *imgeView2;
@property (nonatomic ,strong) UIImageView *imgeView3;
@property (nonatomic ,strong) UIScrollView *myscroll;
@property (nonatomic ,strong) UIPageControl *mypage;
@property (nonatomic ,assign) int currentPage;
@end
#import "ViewController.h"
#define HEIGHT 200
#define WIDTH 300
//#define WIDTH1
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]];
self.array = [[NSMutableArray alloc ] init];
for (int i = 0; i<6; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
[self.array addObject:image];
}
self.myscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH/4 , HEIGHT/3, WIDTH, HEIGHT)];
self.myscroll.backgroundColor=[UIColor grayColor];
self.myscroll.contentSize = CGSizeMake(WIDTH*3, 0);
[self.view addSubview:self.myscroll];
self.myscroll.delegate = self;
//隐藏滚动条
self.myscroll.showsHorizontalScrollIndicator = NO;
//设置分页
self.myscroll.pagingEnabled = YES;
self.mypage = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH-140, HEIGHT+40, 120, 30)];
self.mypage.backgroundColor = [UIColor clearColor];
//页数可以写成数组的个数
self.mypage.numberOfPages = self.array.count;
self.mypage.currentPage = 0;
self.mypage.currentPageIndicatorTintColor = [UIColor greenColor];
self.mypage.pageIndicatorTintColor = [UIColor blueColor];
[self.view addSubview:self.mypage];
self.imageView1 = [[UIImageView alloc] init];
self.imgeView2 = [[UIImageView alloc] init];
self.imgeView3 = [[UIImageView alloc] init];
self.currentPage = 0;
[self reloadImage];
}
-(void)reloadImage{
//第一种情况,当前页在第一张图片的时候
if (self.currentPage == 0)
{
self.imageView1.image = [self.array lastObject];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array objectAtIndex:self.currentPage+1];
}
//当前页在最后一张图片的时候
else if (self.currentPage == self.array.count-1)
{ self.imageView1.image = [self.array objectAtIndex:self.currentPage-1 ];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array firstObject];
}
else
{ self.imageView1.image = [self.array objectAtIndex:self.currentPage-1];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array objectAtIndex:self.currentPage+1];
}
self.imageView1.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
self.imgeView2.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
self.imgeView3.frame = CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT);
[self.myscroll addSubview:self.imageView1];
[self.myscroll addSubview:self.imgeView2];
[self.myscroll addSubview:self.imgeView3];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
float x = self.myscroll.contentOffset.x;
//向左滑动
if (x<=0)
{ if(self.currentPage == 0)
{
self.currentPage = (int)self.array.count - 1;
}
else
{
self.currentPage--;
}
}
//向右滑动
if(x>=WIDTH*2)
{ if(self.currentPage ==self.array.count-1)
{
self.currentPage = 0;
}
else
{
self.currentPage++;
}
}
self.mypage.currentPage = self.currentPage;
[self reloadImage];
}