相片查看器

照片查看器

照片查看器

/**

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

@interface HMViewController ()

@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

@implementation HMViewController

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

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

#pragma mark - 控件的懒加载
// getter方法中,不要再使用self. 否则会重复调用getter方法,造成死循环
- (
UILabel *)noLabel
{
   
if (_noLabel == nil) {
       
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 40)];
       
       
_noLabel = label;
       
_noLabel.textAlignment  = NSTextAlignmentCenter;
        [
self.view addSubview:_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 = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
        [
self.view addSubview:_iconImage];
    }
   
return _iconImage;
}

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

- (
UIButton *)leftButton
{
   
if (_leftButton == nil) {
       
_leftButton = [[UIButton alloc] 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);
       
        [
_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
        [
_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
        [
self.view addSubview:_leftButton];
       
       
_leftButton.tag = -1;
       
        [
_leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
   
return _leftButton;
}

- (
UIButton *)rightButton
{
   
if (_rightButton == nil) {
       
_rightButton = [[UIButton alloc] 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);
       
        [
_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
        [
_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
        [
self.view addSubview:_rightButton];
       
       
_rightButton.tag = 1;
       
        [
_rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
   
return _rightButton;
}

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

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

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

   
// 设置图像和描述
   
self.iconImage.image = [UIImage imageNamed: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;
   
    [
self showPhotoInfo];
}

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

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