本節(jié)學(xué)習(xí)內(nèi)容:
1.照片墻的基本概念
2.照片墻的實(shí)現(xiàn)原理
3.照片墻的實(shí)現(xiàn)方法
1.創(chuàng)建丙個(gè)視圖控器分別為VCRoot,VCImageShow 都繼承于UIViewController
【VCRoot,m】
#import"VCRoot.h"
#import"VCImageShow.h"
@interface VCRoot()
@end
@implementation VCRoot
-(void)viewDidLoad{
[super viewDidLoad];
self.title=@"照片墻";
self.navigationController.navgationBar.barTintColor=[UIColor purpleColor];
//使用導(dǎo)航欄不透明
self.navigationController.navigationBar.translucent=NO;
UIScrollView* sv=[[UIScrollView alloc]init];
//設(shè)置位置大小
sv.frame=CGRectMake(10,10,300,480);
//設(shè)置畫布大小
sv.contentSize=CGSizeMake(300,480*1.5);
sv.showsVerticalScrollIndicator=NO;
//打開(kāi)交互事件
sv.userInteractionEnabled=YES;
self.view.backgroundColor=[UIColor whiteColor];
for(int i=0;i<15;i++){
NSString* strName=[NSString stringWithFormat:"@17_%d.jpg",i+1];
UIImage* image=[UIImage imageNamed:strName];
UIImageView* iView=[[UIImageView alloc]initWithImage:image];
iView.grame=CGRectMake(3+(I%3)*100,(i/3)*140+10,90,130);
[sv addSubview:iView];
//開(kāi)啟交互模式
iView.userInteractionEnabled=YES;
//創(chuàng)建點(diǎn)擊手勢(shì)
UITapGestureRecognizer* tap=[[UITapGestureRecongnizer alloc]initWithTarget:self action:@selector(pressTap:)];
//單次點(diǎn)擊
tap.numberOftapsReuired=1
//單個(gè)手指
tap.numberOfTouchsRequired=1
[iView addGestureRecognizer:tap];
//圖像對(duì)像的tag值
iView.tag=101+i;
}
[self.view addSubview:sv];
}
方法三 tag值
-(void)pressTap:(UITapGesttureRecongnizer*)tap{
UIImageView* imageView=[UIImageView*)tap.view;
VCImageShow* imageShow=[[VCImageShow alloc]init];
imageShow.imageTag=imageView.tag;
[self.navigationController pushViewController:imageShow animated:YES];
}
/*方法二
-(void)pressTap:(UITapGestureRecognizer*)tap{
UIImageView* imageView=(UIImageView*)tap.view;
//創(chuàng)建視圖控制器
VCImageShow* imageShow=[[VCImageShow alloc]init];
//點(diǎn)擊的圖像視圖賦值
//imageShow.imageView=imageView;修改為
imageShow.image=imageView.image;
//將控制器推出
[self.navigationController pushViewController:imageShow animated:YES];
}
*/
【VCImageShow.h】
#import
@interface VCImageShow:UIViewController
//圖像視圖的tag
@property(nonatomic,assign)NSUIngeger imageTag;
//圖像對(duì)象
@property(nonatomic,retain)UITmage* image;
//圖像視圖對(duì)象
@property(noatomic,retain)UIImageView* imageView;
【VCImageShow.m】
#import"VCImageShow"
@imterface VCImageShow()
@end
@implementation VCImageShow
-(void)viewDidLoad{
[super viewDidLoad];
self.title=@"圖片展示";
_imageView=[[UIImageView alloc]init;
_imageView.frame=CGRectMake(0,0,320,480);
//方法二
//_imageView.image=_image;
//方法三
_imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"17_%lu.jpg",_imageTag_100]];
//一個(gè)視圖對(duì)象只能有一個(gè)根視圖,分我們把視圖添加到新的父視圖上時(shí)蚓炬,從原因的父視圖中給刪除掉
[self.view addSubview:_imageView];
}
【AppDelegate.m】
#import"AppDelegate.h"
#import"VCRooth"
@interface AppDelegate()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//導(dǎo)航控器框架結(jié)果
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UINavigationController* nav=[[UINavigationController alloc]]initWithRootViewController:[VCRoot alloc]init];
self.window.rootViewConroller=nav;
return YES;
}