- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self layoutScrollView];
[self layoutImageView];
[self layoutPageControl];
[self addTimer1];
}
#pragma mark - 布局scrollView
- (void)layoutScrollView{
self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.scrollView.contentSize = CGSizeMake(kScreenWidth * 7, kScreenHeight);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
[self.scrollView release];
}
#pragma mark - 布局图片
- (void)layoutImageView{
for (int i = 0; i < 7; i++) {
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * (i + 1), 20, kScreenWidth, kScreenHeight / 3)];
NSString *imageName = [NSString stringWithFormat:@"p%d", i + 1];
NSString *filePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];
imageV.image = [UIImage imageWithContentsOfFile:filePath];
[self.scrollView addSubview:imageV];
[imageV release];
}
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenHeight / 3)];
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"p6" ofType:@"jpg"];
imageView1.image = [UIImage imageWithContentsOfFile:filePath1];
[self.scrollView addSubview:imageView1];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * 7, 20, kScreenWidth, kScreenHeight / 3)];
NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"p1" ofType:@"jpg"];
imageView2.image = [UIImage imageWithContentsOfFile:filePath2];
[self.scrollView addSubview:imageView2];
}
#pragma mark - 布局pageControl
- (void)layoutPageControl{
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth - 120) / 2, 580, 120, 20)];
self.pageControl.pageIndicatorTintColor = [UIColor cyanColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
self.pageControl.numberOfPages = 6;
self.pageControl.currentPage = 0;
[self.pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.pageControl];
[self.pageControl release];
}
- (void)addTimer1{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTime) userInfo:nil repeats:YES];
}
- (void)handleTime{
[self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + 375, 0) animated:YES];
if (self.scrollView.contentOffset.x == kScreenWidth * 6) {
self.scrollView.contentOffset = CGPointZero;
self.pageControl.currentPage = 0;
}else if (self.scrollView.contentOffset.x == 0){
self.pageControl.currentPage = 6;
}else{
self.pageControl.currentPage = self.scrollView.contentOffset.x / kScreenWidth;
}
}
#pragma mark - UIScrollViewDelegate
//第一张图片向左拖拽会显示最后一张,最后一张向右拖拽会到第一张
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (self.scrollView.contentOffset.x == kScreenWidth * 7) {
self.pageControl.currentPage = 0;
self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
}else if (self.scrollView.contentOffset.x == 0){
self.pageControl.currentPage = 6;
self.scrollView.contentOffset = CGPointMake(kScreenWidth * 6, 0);
}else{
self.pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth - 1;
}
}
- (void)handlePageControl:(UIPageControl *)pageControl{
[self.scrollView setContentOffset:CGPointMake(pageControl.currentPage * kScreenWidth, 0) animated:YES];
}