首页 > 代码库 > 照片查看器

照片查看器

照片查看器

/**

 用纯代码开发的过程
 
 1.
确定界面元素,要有什么内容
 2.
用代码来搭建界面
 3.
编写代码
 */

@interfaceHMViewController ()

@property(nonatomic,strong) UILabel *noLabel;
@property(nonatomic,strong) UIImageView *iconImage;
@property(nonatomic,strong) UILabel *descLabel;
@property(nonatomic,strong) UIButton *leftButton;
@property(nonatomic,strong) UIButton *rightButton;

/**当前显示的照片索引*/
@property(nonatomic,assign) int index;
/**图片信息的数组*/
@property(nonatomic,strong) NSArray *imageList;

@property(nonatomic,strong) Person *person;
@end

@implementationHMViewController

/**
 
懒加载(延迟加载),通过getter实现
 
 
效果:让对象在最需要的时候才创建!
 */

- (
NSArray*)imageList
{
   
NSLog(@"读取图像信息");
   
if (_imageList== nil) {
       
NSString *path = [[NSBundlemainBundle]pathForResource:@"ImageList"ofType:@"plist"];
       
_imageList = [NSArrayarrayWithContentsOfFile:path];
    }
   
return _imageList;
}

#pragma mark -控件的懒加载
//getter方法中,不要再使用self.否则会重复调用getter方法,造成死循环
- (
UILabel*)noLabel
{
   
if (_noLabel== nil) {
       
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,20,self.view.bounds.size.width,40)];
       
       
_noLabel = label;
       
_noLabel.textAlignment  =NSTextAlignmentCenter;
        [
self.viewaddSubview:_noLabel];
    }
   
return _noLabel;
}

- (
UIImageView*)iconImage
{
   
if (_iconImage== nil) {
       
CGFloat imageW =200;
       
CGFloat imageH =200;
       
CGFloat imageX = (self.view.bounds.size.width- imageW) * 0.5;
       
CGFloat imageY =CGRectGetMaxY(self.noLabel.frame) + 20;
       
       
_iconImage = [[UIImageViewalloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
        [
self.viewaddSubview:_iconImage];
    }
   
return _iconImage;
}

- (
UILabel*)descLabel
{
   
if (_descLabel== nil) {
       
CGFloat descY =CGRectGetMaxY(self.iconImage.frame);
       
_descLabel = [[UILabelalloc]initWithFrame:CGRectMake(0, descY, self.view.bounds.size.width,100)];
       
_descLabel.textAlignment= NSTextAlignmentCenter;
       
       
// 需要Label具有足够的高度,不限制显示的行数
       
_descLabel.numberOfLines= 0;
        [
self.viewaddSubview:_descLabel];
    }
   
return _descLabel;
}

- (
UIButton*)leftButton
{
   
if (_leftButton== nil) {
       
_leftButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,40,40)];
       
CGFloat centerY =self.iconImage.center.y;
       
CGFloat centerX =self.iconImage.frame.origin.x* 0.5;
       
_leftButton.center= CGPointMake(centerX, centerY);
       
        [
_leftButtonsetBackgroundImage:[UIImageimageNamed:@"left_normal"]forState:UIControlStateNormal];
        [
_leftButtonsetBackgroundImage:[UIImageimageNamed:@"left_highlighted"]forState:UIControlStateHighlighted];
        [
self.viewaddSubview:_leftButton];
       
       
_leftButton.tag= -1;
       
        [
_leftButtonaddTarget:selfaction:@selector(clickButton:)forControlEvents:UIControlEventTouchUpInside];
    }
   
return _leftButton;
}

- (
UIButton*)rightButton
{
   
if (_rightButton== nil) {
       
_rightButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,40,40)];
       
CGFloat centerY =self.iconImage.center.y;
       
CGFloat centerX =self.iconImage.frame.origin.x* 0.5;
       
_rightButton.center= CGPointMake(self.view.bounds.size.width- centerX, centerY);
       
        [
_rightButtonsetBackgroundImage:[UIImageimageNamed:@"right_normal"]forState:UIControlStateNormal];
        [
_rightButtonsetBackgroundImage:[UIImageimageNamed:@"right_highlighted"]forState:UIControlStateHighlighted];
        [
self.viewaddSubview:_rightButton];
       
       
_rightButton.tag= 1;
       
        [
_rightButtonaddTarget:selfaction:@selector(clickButton:)forControlEvents:UIControlEventTouchUpInside];
    }
   
return _rightButton;
}

/**viewDidLoad创建界面*/
- (
void)viewDidLoad
{
    [
superviewDidLoad];
   
   
// 显示照片信息
    [
selfshowPhotoInfo];
}

/**
 
重构的目的:让相同的代码只出现一次
 */

- (
void)showPhotoInfo
{
   
// 设置序号
   
self.noLabel.text= [NSStringstringWithFormat:@"%d/%d",self.index+ 1, 5];

   
// 设置图像和描述
   
self.iconImage.image= [UIImageimageNamed:self.imageList[self.index][@"name"]];
   
self.descLabel.text= self.imageList[self.index][@"desc"];
   
   
self.rightButton.enabled= (self.index!= 4);
   
self.leftButton.enabled= (self.index!= 0);
}

//OC中,很多方法的第一个参数,都是触发该方法的对象!
- (
void)clickButton:(UIButton*)button
{
   
// 根据按钮调整当前显示图片的索引?
   
self.index+= button.tag;
   
    [
selfshowPhotoInfo];
}

注意:
1问题分析:每一次调用showPhotoInfo方法都会实例化一次数组
   
解决办法:将数组实例化方法移动至viewDidLoad方法
2问题分析:
     1)  viewDidLoad方法过于冗长
     2控件计算位置时,彼此依赖
     解决办法:
     利用控件的getter方法,实现控件的懒加载

3问题分析:图片信息与代码的耦合性还是太强
   
解决办法:采用plist的方式定义图片信息内容
   
提示:这是从网络加载数据的前奏,在程序开发过程中,应该尽量让数据内容与程序代码分离!

照片查看器