#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//去掉系统给ScrollView预留的20像素空间
[self setAutomaticallyAdjustsScrollViewInsets:NO];
//总的
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 20, 280, 450)];
scrollView.backgroundColor = [UIColor blueColor];
scrollView.tag = 1000;
scrollView.contentSize = CGSizeMake(1400, 0);
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
//第一个
UIScrollView *scrollView1 = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 20, 240, 450)];
scrollView1.contentSize = CGSizeMake(240, 0);
scrollView1.tag = 111;
UIImageView *image0 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 240, 350)];
image0.image = [UIImage imageNamed:@"g.jpg"];
[scrollView1 addSubview:image0];
[image0 release];
scrollView1.delegate = self;
scrollView1.minimumZoomScale = 0.5;
scrollView1.maximumZoomScale = 2;
[scrollView addSubview:scrollView1];
[scrollView1 release];
//第二个
UIScrollView *scrollView2 = [[UIScrollView alloc]initWithFrame:CGRectMake(300, 20, 240, 450)];
scrollView2.contentSize = CGSizeMake(280, 0);
scrollView2.tag = 222;
UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 240, 350)];
image1.image = [UIImage imageNamed:@"a.png"];
[scrollView2 addSubview:image1];
[image1 release];
scrollView2.delegate = self;
scrollView2.minimumZoomScale = 0.5;
scrollView2.maximumZoomScale = 2;
[scrollView addSubview:scrollView2];
[scrollView2 release];
//第三个
UIScrollView *scrollView3 = [[UIScrollView alloc]initWithFrame:CGRectMake(580, 20, 240, 450)];
scrollView3.contentSize = CGSizeMake(280, 0);
scrollView3.tag = 333;
UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 240, 350)];
image2.image = [UIImage imageNamed:@"h.jpg"];
[scrollView3 addSubview:image2];
[image2 release];
scrollView3.delegate = self;
scrollView3.minimumZoomScale = 0.5;
scrollView3.maximumZoomScale = 2;
[scrollView addSubview:scrollView3];
[scrollView3 release];
//第四个
UIScrollView *scrollView4 = [[UIScrollView alloc]initWithFrame:CGRectMake(860, 20, 240, 450)];
scrollView4.contentSize = CGSizeMake(280, 0);
scrollView4.tag = 444;
UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 240, 350)];
image3.image = [UIImage imageNamed:@"i.jpg"];
[scrollView4 addSubview:image3];
[image3 release];
scrollView4.delegate = self;
scrollView4.minimumZoomScale = 0.5;
scrollView4.maximumZoomScale = 2;
[scrollView addSubview:scrollView4];
[scrollView4 release];
//第五个
UIScrollView *scrollView5 = [[UIScrollView alloc]initWithFrame:CGRectMake(1140, 20, 240, 450)];
scrollView5.contentSize = CGSizeMake(280, 0);
scrollView5.tag = 555;
UIImageView *image4 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 240, 350)];
image4.image = [UIImage imageNamed:@"k.jpg"];
[scrollView5 addSubview:image4];
[image4 release];
scrollView5.delegate = self;
scrollView5.minimumZoomScale = 0.5;
scrollView5.maximumZoomScale = 2;
[scrollView addSubview:scrollView5];
[scrollView5 release];
scrollView.delegate = self;
[self.view addSubview:scrollView];
[scrollView release];
UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(110, 400, 100, 30)];
pageControl.numberOfPages = 5;
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
[pageControl addTarget:self action:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
[pageControl release];
self.page = pageControl;
}
- (void)pageControlAction:(UIPageControl *)pageControl
{
UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:1000];
scrollView.contentOffset = CGPointMake(280 * self.page.currentPage,0);
for (int i = 1; i < 6; i++) {
UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:111* i];
scrollView.zoomScale = 1;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
for (int i = 1; i < 6; i ++) {
UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:111* i];
scrollView.zoomScale = 1;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end