【成长记录】iOS小程序图片浏览器

做一个基本的图片浏览器,效果如下


需求实现

1 左右进行切换

2 数字显示第几张

3 下方有描述

4 第一张图片左图标不能使用,第五张图片右图标不能使用


代码初步实现

#import "ViewController.h"

@interface ViewController ()
//按钮方法
-(IBAction)previous;
-(IBAction)next;
//设置按钮,导航,描述等属性
@property(nonatomic,weak) IBOutlet UIButton *previousBtn;//前一张
@property(nonatomic,weak) IBOutlet UIButton *nextBtn;//后一张
@property(nonatomic,weak) IBOutlet UILabel *noLabel;//数字导航
@property(nonatomic,weak) IBOutlet UIImageView *iconView;//图片view
@property(nonatomic,weak) IBOutlet UILabel *descLabel;//描述
//需要记录显示的账片序数
@property(nonatomic,assign) int index;
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.index = 1;
    [self previous];
}
#pragma mark 改变数据
-(void)changeDate{
    //1,改变数据
    switch (self.index) {
        case 0:
            self.noLabel.text = @"1/5";
            self.iconView.image = [UIImage imageNamed:@"biaoqingdi"];
            self.descLabel.text = @"在他面前,其他神马表情都弱爆了!";
            break;
            
        case 1:
            self.noLabel.text = @"2/5";
            self.iconView.image = [UIImage imageNamed:@"wangba"];
            self.descLabel.text = @"哥们为什么选八号呢";
            break;


        case 2:
            self.noLabel.text = @"3/5";
            self.iconView.image = [UIImage imageNamed:@"bingli"];
            self.descLabel.text = @"这也忒狠了";
            break;
            
        case 3:
            self.noLabel.text = @"4/5";
            self.iconView.image = [UIImage imageNamed:@"chiniupa"];
            self.descLabel.text = @"这小姑娘吃个牛排比杀牛还费劲啊";
            break;

        case 4:
            self.noLabel.text = @"5/5";
            self.iconView.image = [UIImage imageNamed:@"danteng"];
            self.descLabel.text = @"亲,你能改下你的网名么?哈哈";
        break;
    }
    //2,改变按钮的状态
        //如果index = 0,上箭头不可用  indeex = 4,下箭头不可用
    self.previousBtn.enabled = (self.index != 0);
    self.nextBtn.enabled = (self.index !=4);
}
-(IBAction)previous{
    //1缩小索引
    self.index--;
    //2,改变数据
    [self changeDate];
}
-(IBAction)next{
    //1缩小索引
    self.index++;
    //2,改变数据
    [self changeDate];

}

@end
改变按钮的状态时用到了按钮的一个属性叫做enable,按住command进去看一下,发现是个BOOL类型,所以直接进行判断



这是一个非常死板的代码,因为如果想要再添加图片只能机械的在方法changData中进行复制粘贴,所以做出以下改进

#define LHSIconKey @"icon"
#define LHSDescKey @"desc"
#import "ViewController.h"

@interface ViewController ()
//按钮方法
-(IBAction)previous;
-(IBAction)next;
//设置按钮,导航,描述等属性
@property(nonatomic,weak) IBOutlet UIButton *previousBtn;//前一张
@property(nonatomic,weak) IBOutlet UIButton *nextBtn;//后一张
@property(nonatomic,weak) IBOutlet UILabel *noLabel;//数字导航
@property(nonatomic,weak) IBOutlet UIImageView *iconView;//图片view
@property(nonatomic,weak) IBOutlet UILabel *descLabel;//描述
//需要记录显示的账片序数
@property(nonatomic,assign) int index;
//strong:用于一般的对象
//weak:用于UI控件
@property(nonatomic,strong) NSArray *imageData;
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.index = 1;
    [self changeDate];
    
}

#pragma mark 设置数组数据
-(NSArray *)imageData{
    if (_imageData == nil) {//未被初始化
        //初始化数据
       
        NSMutableDictionary *image1 = [NSMutableDictionary dictionary];
        image1[LHSIconKey] = @"biaoqingdi";
        image1[LHSDescKey] = @"在他面前,其他神马表情都弱爆了!";
        
        NSMutableDictionary *image2 = [NSMutableDictionary dictionary];
        image2[LHSIconKey] = @"wangba";
        image2[LHSDescKey] = @"哥们为什么选八号呢";
        
        NSMutableDictionary *image3 = [NSMutableDictionary dictionary];
        image3[LHSIconKey] = @"bingli";
        image3[LHSDescKey] = @"这也忒狠了";
        
        NSMutableDictionary *image4 = [NSMutableDictionary dictionary];
        image4[LHSIconKey] = @"chiniupa";
        image4[LHSDescKey] = @"chiniupa";
        
        NSMutableDictionary *image5 = [NSMutableDictionary dictionary];
        image5[LHSIconKey] = @"danteng";
        image5[LHSDescKey] = @"亲,你能改下你的网名么?哈哈";
        _imageData = @[image1, image2, image3, image4, image5];
    }
    return _imageData;
}


#pragma mark 改变数据
-(void)changeDate{
    //1,改变数据
    self.noLabel.text = [NSString stringWithFormat:@"%d/%d",self.index + 1,self.imageData.count];
    //2,取出字典中index对应的字典数据
    NSDictionary *imageDict = self.imageData[self.index];
    //3.设置图片
    self.iconView.image = [UIImage imageNamed:imageDict[LHSIconKey]];
    //4.设置描述
    self.descLabel.text = imageDict[LHSDescKey];
    //5.改变按钮状态
    self.previousBtn.enabled = (self.index != 0);
    self.nextBtn.enabled = (self.index != self.imageData.count -1);
}
-(IBAction)previous{
    //1缩小索引
    self.index--;
    //2,改变数据
    [self changeDate];
}
-(IBAction)next{
    //1缩小索引
    self.index++;
    //2,改变数据
    [self changeDate];

}

@end

这算是一个扩展性比较好的代码了,我们可以将数组字典放在.plist文件中,直接在文件中读取,这也算是小小的数据库应用

- (NSArray *)imageData
{
    if (_imageData == nil) { // 从未初始化
        // 初始化数据
        // File : 全路径
        // NSBundle : 一个NSBundle代表一个文件夹

        // 利用mainBundle就可以访问软件资源包中的任何资源
        NSBundle *bundle = [NSBundle mainBundle];
        
        //  获得imageData.plist的全路径
        NSString *path = [bundle pathForResource:@"imageData" ofType:@"plist"];
        
        _imageData = [NSArray arrayWithContentsOfFile:path];
    }
    return _imageData;
}



















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值