照片查看器

照片查看器

/**

  用纯代码开发的过程
 
 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 的方式定义图片信息内容
   
提示:这是从网络加载数据的前奏,在程序开发过程中,应该尽量让数据内容与程序代码分离!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值