在简易相册的基础上, 利用导航控制器, 进行完善, 第一个页面是照片的缩略图, 点击缩略图, 则能够看到大图
//1.创建类HomeViewController和PhotoViewController,
在AppDelegate.m中创建导航控制器, 并将HomeViewController作为它的根视图控制器
//创建视图控制器HomeViewController的对象, 并对其进行初始化
HomeViewController *homeVC = [[HomeViewController alloc]init];
//创建导航控制器, 并将homeVC作为它的根视图控制器
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:homeVC];
[homeVCrelease];
//将naVC设为window的根视图控制器
self.window.rootViewController = naVC;
[naVCrelease];
//2.在HomeViewController.m中实现缩略图页面
#import "HomeViewController.h"
#import "PhotoViewController.h"
@interface HomeViewController () {
UIScrollView*homeScrollView;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[superviewDidLoad];
//设置导航栏标题
self.navigationItem.title = @"相册";
CGSize size= [UIScreen mainScreen].bounds.size;
//创建滚动视图homeScrollView
homeScrollView = [[UIScrollView alloc] initWithFrame:[UIScreenmainScreen].bounds];
homeScrollView.contentSize = CGSizeMake(size.width, size.height * 6- 570);
[self.viewaddSubview:homeScrollView];
//创建一个数组, 用来保存照片名字
NSArray*nameArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8",@"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17",@"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26",@"27", @"28", @"29", @"30", @"31", @"32"......@"50"]
NSInteger n= 0;
//通过for循环, 将照片缩略图添加到滚动视图上
for(NSInteger i = 0; i < 17; i++) {
for (NSInteger j = 0; j < 3; j++) {
//创建图片视图
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:nameArray[n++]]];
//设置图片视图的tag值
imageView.tag = 100 + i * 3 + j;
imageView.userInteractionEnabled = YES;
imageView.frame = CGRectMake(size.width / 3 * j, size.height / 3.3* i, size.width / 3, size.height / 3.3);
//创建轻拍手势, 并关联方法, 来实现点击缩略图进入大图的效果
UITapGestureRecognizer *tapPhoto = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[imageView addGestureRecognizer:tapPhoto];
[tapPhoto release];
[homeScrollView addSubview:imageView];
[imageView release];
//当for循环将照片全部添加后, break停止循环
if (i == 16 && j == 1) {
break;
}
}
}
}
//tap手势的方法, 实现缩略图视图和大图试图之间的切换
- (void)tap:(UITapGestureRecognizer *)sender {
PhotoViewController *photoVC = [[PhotoViewController alloc]init];
photoVC.number = sender.view.tag;
[self.navigationController pushViewController:photoVC animated:YES];
[photoVCrelease];
}
@end
//3.在PhotoViewController.m中实现大图页面
#import "PhotoViewController.h"
@interface PhotoViewController () {
UIScrollView*bigScrollView;
NSIntegerflag;
}
@end
@implementation PhotoViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CGSize size= [UIScreen mainScreen].bounds.size;
flag =0;
NSArray*nameArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8",@"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17",@"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26",@"27", @"28", @"29", @"30", @"31", @"32"...@"50"]
bigScrollView = [[UIScrollView alloc] initWithFrame:[UIScreenmainScreen].bounds];
bigScrollView.contentSize = CGSizeMake(size.width *nameArray.count, size.height);
bigScrollView.pagingEnabled = YES;
bigScrollView.delegate = self;
[self.viewaddSubview:bigScrollView];
[bigScrollView release];
for(NSInteger i = 0; i < nameArray.count; i++) {
UIScrollView *smallScorollView = [[UIScrollView alloc]initWithFrame:CGRectMake(size.width * i, 0, size.width,size.height)];
smallScorollView.minimumZoomScale = 0.5;
smallScorollView.maximumZoomScale = 2;
smallScorollView.delegate = self;
[bigScrollView addSubview:smallScorollView];
[smallScorollView release];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:nameArray[i]]];
imageView.frame = CGRectMake(0, 0, size.width, size.height);
imageView.tag = 100;
[smallScorollView addSubview:imageView];
[imageView release];
}
bigScrollView.contentOffset = CGPointMake(size.width * (self.number- 100), 0);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if(scrollView != bigScrollView) {
return [scrollView viewWithTag:100];
} else{
return nil;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if(scrollView == bigScrollView) {
NSInteger currentPage = bigScrollView.contentOffset.x / 375;
if (currentPage != flag) {
UIScrollView *smallScrollView = [bigScrollView.subviewsobjectAtIndex:flag];
smallScrollView.zoomScale = 1.0;
flag = currentPage;
}
}
}
@end
网友评论