黑马程序员技术交流社区
标题:
图片查看器
[打印本页]
作者:
任子杰
时间:
2015-11-6 01:06
标题:
图片查看器
屏幕快照 2015-11-06 上午1.06.19.png
(59.45 KB, 下载次数: 3)
下载附件
2015-11-6 01:06 上传
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel *nolabel;
@property(nonatomic,strong)UIImageView *img;
@property(nonatomic,strong)UIButton *leftbutton;
@property(nonatomic ,strong)UIButton *rightbutton;
@property(nonatomic,strong)UILabel *infolabel;
//记录当前照片索引
@property(nonatomic,assign)int index;
//定义数组存放字典
@property(nonatomic,strong)NSArray *array;
@end
@implementation ViewController
//懒加载 重写get方法 在程序最需要的时候创建对象
-(NSArray *)array{
NSLog(@"读取图像信息");
if (_array==nil) {
NSLog(@"实例化数组");
//设置数组的路径 NSBundle 程序编译安装过后的程序包 区分大小写
NSString *path=[[NSBundle mainBundle]pathForResource:@"imagelist" ofType:@"plist"];
NSLog(@"%@",path);
_array=[NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",_array);
}
return _array;
}
-(UILabel *)nolabel{
if (_nolabel==nil) {
//初始化控件
_nolabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 30, self.view.bounds.size.width, 40)];
//设置文字
//_nolabel.text=@"1/5";
//设置对齐方式
_nolabel.textAlignment=NSTextAlignmentCenter;
//加载到视图
[self.view addSubview:_nolabel];
}
return _nolabel;
}
-(UIImageView *)img{
if (_img==nil) {
//图片
CGFloat imgw=200;
CGFloat imgh=200;
//图片的x轴位置等于整个view的宽度减去图片的宽度除以2
CGFloat imgx=(self.view.bounds.size.width-imgw)*0.5;
//CGRectGetMaxY 求最大的y值
CGFloat imgy=CGRectGetMaxY(self.nolabel.frame)+20;
_img=[[UIImageView alloc]initWithFrame:CGRectMake(imgx, imgy, imgw, imgh)];
[self.view addSubview:_img];
}
return _img;
}
-(UILabel *)infolabel{
if (_infolabel==nil) {
//描述文字
CGFloat descY=CGRectGetMaxY(self.img.frame)+20;
_infolabel=[[UILabel alloc]initWithFrame:CGRectMake(0, descY, self.view.bounds.size.width, 100)];
//需要label有足够的高度才会换行 此处label高度为100
_infolabel.numberOfLines=0;
_infolabel.textAlignment=NSTextAlignmentCenter;
[_infolabel setTextColor:[UIColor yellowColor]];
[self.view addSubview:_infolabel];
}
return _infolabel;
}
-(UIButton *)leftbutton{
if (_leftbutton==nil) {
//左边按钮
_leftbutton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
CGFloat centenrX=(self.img.frame.origin.x)*0.5;
CGFloat centerY=self.img.center.y;
_leftbutton.center=CGPointMake(centenrX, centerY);
[_leftbutton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
[self.view addSubview:_leftbutton];
[_leftbutton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
_leftbutton.tag=-1;
}
return _leftbutton;
}
-(UIButton *)rightbutton{
if (_rightbutton==nil) {
//右边按钮
_rightbutton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
CGFloat centenrX=(self.img.frame.origin.x)*0.5;
CGFloat centerY=self.img.center.y;
_rightbutton.center=CGPointMake(self.view.bounds.size.width-centenrX, centerY);
[_rightbutton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
[self.view addSubview:_rightbutton];
//代码实现连线//@selector(clickButton:)此处谁点击就传入谁
[_rightbutton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
_rightbutton.tag=1;
}
return _rightbutton;
}
- (void)viewDidLoad {
//初始化父类
[super viewDidLoad];
[self showsPhotoinfo];
}
-(void)showsPhotoinfo{
//设置label动态显示内容
_nolabel.text=[NSString stringWithFormat:@"%d/%d",self.index+1,5];
//先根据index索引找出相应的字典再根据字典找相应内容
self.img.image=[UIImage imageNamed:self.array[self.index][@"name"]];
self.infolabel.text=self.array[self.index][@"desc"];
self.rightbutton.enabled = (self.index != 4);
self.leftbutton.enabled = (self.index != 0);
}
-(void)clickButton:(UIButton *)button{
self.index+=button.tag;
[self showsPhotoinfo];
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2