@implementation RootViewController
- (void)dealloc {
[_scrollView release];
[_pageControl release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
self.scrollView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.scrollView];
[_scrollView release];
self.scrollView.contentSize = CGSizeMake(WIDTH * 9, 0);
self.scrollView.pagingEnabled = YES;
self.scrollView.bounces = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
firstImageView.image = [UIImage imageNamed:@"h7.jpeg"];
[self.scrollView addSubview:firstImageView];
[firstImageView release];
firstImageView.userInteractionEnabled = YES;
for (NSInteger i = 1; i < 8; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i]];
[self.scrollView addSubview:imageView];
[imageView release];
imageView.userInteractionEnabled = YES;
}
UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8 * WIDTH, 0, WIDTH, HEIGHT)];
lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
[self.scrollView addSubview:lastImageView];
[lastImageView release];
lastImageView.userInteractionEnabled = YES;
self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
self.scrollView.delegate = self;
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 630, 375,50)];
self.pageControl.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.pageControl];
[_pageControl release];
self.pageControl.numberOfPages = 7;
self.pageControl.pageIndicatorTintColor = [UIColor redColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
[self.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
self.scrollView.minimumZoomScale = 0.1;
self.scrollView.maximumZoomScale = 3;
}
#pragma mark 这个方法用来关联scrollView的缩放功能
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView.subviews firstObject];
}
- (void)pageControlAction:(UIPageControl *)page {
[self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * WIDTH, 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (self.scrollView.contentOffset.x == 8 * WIDTH) {
self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
} else if (self.scrollView.contentOffset.x == 0) {
self.scrollView.contentOffset = CGPointMake(7 * WIDTH, 0);
}
self.pageControl.currentPage = scrollView.contentOffset.x / WIDTH - 1;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end