通过UIScrollView和UIPageControl结合制作相册
1、创建UIScrollView视图:
UIScrollView *aScrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
aScrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:aScrollView];
aScrollView.contentSize = CGSizeMake(CGRectGetWidth(aScrollView.bounds)*4, CGRectGetHeight(aScrollView.bounds));
aScrollView.pagingEnabled = YES;
aScrollView.delegate = self;
2、在aScrollView视图上添加4个scrollView(暂定有4张相片),给每一个scrollView添加一张图片:
NSArray *images = @[@"001.jpg",@"002.jpg",@"003.jpg",@"004.jpg"];
for (int i = 0; i < 4; i++) {
UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(CGRectGetWidth(aScrollView.bounds)*i, 0, CGRectGetWidth(aScrollView.bounds), CGRectGetHeight(aScrollView.bounds))] autorelease];
[aScrollView addSubview:scrollView];
scrollView.minimumZoomScale = 0.3;
scrollView.maximumZoomScale = 3;
scrollView.delegate = self;
scrollView.tag = 200 + i;
UIImage *image = [UIImage imageNamed:images[i]];
CGFloat width = CGRectGetWidth(aScrollView.bounds);
CGFloat height = width * image.size.height / image.size.width;
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)] autorelease];
[scrollView addSubview:imageView];
imageView.image = image;
imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, CGRectGetHeight(self.view.bounds) / 2);
imageView.tag =100 + i;
}
3、创建UIPageControl控件:
UIPageControl *pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-80, CGRectGetWidth(self.view.bounds), 50)] autorelease];
[self.view addSubview:pageControl];
pageControl.numberOfPages = 4;
pageControl.currentPage = 0;
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
[pageControl addTarget:self action:@selector(handlePageAction:) forControlEvents:UIControlEventValueChanged];
4、实现UIScrollView视图和UIPageControl控件联动的方法:
- (void)handlePageAction:(UIPageControl *)sender{
CGPoint offset = CGPointZero;
offset.x = sender.currentPage * CGRectGetWidth(self.view.bounds);
for (id object in self.view.subviews) {
if ([object isKindOfClass:[UIScrollView class]]) {
[(UIScrollView *)object setContentOffset:offset animated:YES];
UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:200+sender.tag];
scrollView.zoomScale = 1;
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x / CGRectGetWidth(scrollView.bounds);
UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:300];
UIScrollView *aScrollView = (UIScrollView *)[self.view viewWithTag:200+pageControl.currentPage];
aScrollView.zoomScale = 1;
for (id object in self.view.subviews) {
if ([object isKindOfClass:[UIPageControl class]]) {
[(UIPageControl *)object setCurrentPage:index];
}
}
}
5、实现相片的缩放功能:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
for (int i = 0; i < 4; i++) {
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100+i];
CGFloat content_width = scrollView.contentSize.width;
CGFloat content_height = scrollView.contentSize.height;
CGFloat width = CGRectGetWidth(scrollView.bounds);
CGFloat height = CGRectGetHeight(scrollView.bounds);
CGFloat delta_x = width > content_width ? (width - content_width) / 2 : 0;
CGFloat delta_y = height > content_height ? (height - content_height)/2 : 0;
imageView.center = CGPointMake(content_width / 2 + delta_x, content_height / 2 + delta_y);
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView viewWithTag:scrollView.tag - 100];
}