UI基础-基础控件-0324-浏览图片案例最终版与注意事项

来源:互联网 发布:网络纠纷案件怎么解决 编辑:程序博客网 时间:2024/06/06 00:48
normal(普通状态)
默认情况
对应的枚举常量:UIControlStateNormal


highlighted(高亮状态)
按钮被按下去的时候(手指还未松开)
对应的枚举常量:UIControlStateHighlighted


disabled(失效状态,不可用状态)
如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击

对应的枚举常量:UIControlStateDisabled


--------------------------------------------------------------------------------------------------------- 

NSArray和NSDictionary的使用

当图片内容非常多时,“根据index来设置内容”的代码就不具备扩展性,要经常改动


为了改变现状,可以考虑讲图片数据线保存到一个数组中,数组中有序地放着很多字典,一个字典代表一张图片数据,包含了图片名、图片描述
@property (strong, nonatomic) NSArray *images;


由于只需要初始化一次图片数据,因此放在get方法中初始化


将属性放在get方法中初始化的方式,称为“懒加载”\”延迟加载”


--------------------------------------------------------------------------------------------------------- 

什么是Plist文件

直接将数据直接写在代码里面,不是一种合理的做法。如果数据经常改,就要经常翻开对应的代码进行修改,造成代码扩展性低


因此,可以考虑将经常变的数据放在文件中进行存储,程序启动后从文件中读取最新的数据。如果要变动数据,直接修改数据文件即可,不用修改代码


一般可以使用属性列表文件存储NSArray或者NSDictionary之类的数据,这种属性列表文件的扩展名是plist,因此也成为“Plist文件”


--------------------------------------------------------------------------------------------------------- 


解析Plist文件

接下来通过代码来解析Plist文件中的数据
获得Plist文件的全路径
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"imageData" ofType:@"plist"];


加载plist文件
_images = [NSArray arrayWithContentsOfFile:path];


- (NSArray *)images
{
    if (_images == nil) {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path = [bundle pathForResource:@"imageData" ofType:@"plist"];
        _images = [NSArray arrayWithContentsOfFile:path];
    }
    return _images;
}


--------------------------------------------------------------------------------------------------------- 

1.NSBundle
1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹
2> 利用mainBundle就可以访问软件资源包中的任何资源
3> 模拟器应用程序的安装路径
/Users/aplle/资源库/Application Support/iPhone Simulator/7.1/Applications

--------------------------------------------------------------------------------------------------------- 

UIImageView的contentMode:.contendMode:适配

--------------------------------------------------------------------------------------------------------- 


//  06-图片浏览器最终版////  Created by apple on 14-3-24.//  Copyright (c) 2014年 itcast. All rights reserved.//// strong : 一般对象// weak : UI控件#define MJIconKey @"icon"#define MJDescKey @"desc"#import "MJViewController.h"@interface MJViewController ()- (IBAction)previous;- (IBAction)next;@property (weak, nonatomic) IBOutlet UIButton *previousBtn;@property (weak, nonatomic) IBOutlet UIButton *nextBtn;@property (weak, nonatomic) IBOutlet UIImageView *ico@property (weak, nonatomic) IBOutlet UILabel *noLabel;nView;@property (weak, nonatomic) IBOutlet UILabel *descLabel;// 记录当前显示的是第几张图片@property (nonatomic, assign) int index;// 图片数据集合@property (nonatomic, strong) NSArray *imageData;@end@implementation MJViewController- (void)viewDidLoad{    [super viewDidLoad];        // 默认显示index为0对应的数据    [self changeData];}- (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;}#pragma mark 改变数据- (void)changeData{    // 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[MJIconKey]];        // 4.设置描述    self.descLabel.text = imageDict[MJDescKey];        // 5.改变按钮状态    self.previousBtn.enabled = (self.index != 0);    self.nextBtn.enabled = (self.index != self.imageData.count - 1);}#pragma mark 上一张//在这个方法中需要改变noLab、descLab和imageData,所以 使用tag太麻烦,直接使用属性
- (IBAction)previous {    // 1.减小索引    self.index--;        // 2.改变数据    [self changeData];}#pragma mark 下一张- (IBAction)next {    // 1.增加索引    self.index++;        // 2.根据索引显示对应的内容    [self changeData];}@end



0 0
原创粉丝点击