麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

IOS開發(fā)筆記-基礎(chǔ)UI(6)照片瀏覽器(控件的懶加載和使用Plist文件將數(shù)據(jù)與代碼分離)

2019-11-14 19:29:55
字體:
供稿:網(wǎng)友

使用UIImageView、UILabel、UIButton實(shí)現(xiàn)一個綜合小案例

功能分析

(1)點(diǎn)擊箭頭切換序號、圖片、描述
(2)如果是首張圖片,左邊箭頭不能點(diǎn)擊
(3)如果是尾張圖片,右邊箭頭不能點(diǎn)擊

步驟分析

(1)搭建UI界面
(2)監(jiān)聽按鈕點(diǎn)擊

切換序號、圖片、描述

 

1. 界面分析

1> 需要讀取或修改的屬性的控件

// 序號標(biāo)簽

// 圖片

// 圖片描述

// 左邊按鈕

// 右邊按鈕

2> 需要監(jiān)聽響應(yīng)事件的對象,需要添加監(jiān)聽方法

// 左邊按鈕

// 右邊按鈕

uiimage 是圖片,不是控件,他的父類為NSObject,UIImageView是加載圖片的控件,父類為UIView

完全的代碼編寫界面(復(fù)習(xí)回憶)

#import "ViewController.h"@interface ViewController ()//序號標(biāo)簽@PRoperty (nonatomic, strong) UILabel *noLabel;//圖片@property (nonatomic, strong) UIImageView *icon;//圖片描述@property (nonatomic, strong) UILabel *descLabel;//左邊按鈕@property (nonatomic, strong) UIButton *leftButton;//右邊按鈕@property (nonatomic, strong) UIButton *rightButton;@end@implementation ViewController//初始化工作//viewDidLoad是視圖加載完成后調(diào)用的方法,通常在此方法中執(zhí)行視圖控制器的初始化工作- (void)viewDidLoad {    [super viewDidLoad];    //實(shí)例化控件    //1、序號標(biāo)簽的編寫    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 40)];    label.text = @"1/5";    //居中對齊    label.textAlignment = NSTextAlignmentCenter;    [self.view addSubview:label];    //記錄改變    self.noLabel = label;        //2、圖片控件    CGFloat imageW = 200;    CGFloat imageH = 200;    CGFloat imageX = (320 - imageW) / 2;    CGFloat imageY = 80;    //實(shí)例化一個圖像視圖    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];    //實(shí)例化一個圖像    UIImage *image = [UIImage imageNamed:@"biaoqingdi"];    //把圖片顯示到imageView    imageView.image = image;    [self.view addSubview:imageView];    //記錄下改變    self.icon = imageView;        //3、圖片描述 label 控件    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 300, 80)];    label1.text = @"發(fā)發(fā)發(fā)";    //居中對齊    label1.textAlignment = NSTextAlignmentCenter;    [self.view addSubview:label1];    //記錄改變    self.descLabel = label1;        //4、左邊的按鈕    UIButton *leftBtn = [[UIButton alloc] init];    //設(shè)置按鈕的背景圖    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];    //設(shè)置按鈕的大小    leftBtn.frame = CGRectMake(0, 0, 40, 40);    //設(shè)置按鈕的位置    leftBtn.center = CGPointMake(self.icon.frame.origin.x / 2, self.icon.center.y);        [self.view addSubview:leftBtn];    self.leftButton = leftBtn;        //5、右邊的按鈕    UIButton *rightBtn = [[UIButton alloc] init];    //設(shè)置按鈕的背景圖    [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];    [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];    //設(shè)置按鈕的大小    rightBtn.frame = CGRectMake(0, 0, 40, 40);    //設(shè)置按鈕的位置    rightBtn.center = CGPointMake(self.view.frame.size.width - self.icon.frame.origin.x / 2, self.icon.center.y);        [self.view addSubview:rightBtn];    self.leftButton = rightBtn;}@end

完整的代碼如下:

#import "ViewController.h"@interface ViewController ()//序號標(biāo)簽@property (nonatomic, strong) UILabel *noLabel;//圖片@property (nonatomic, strong) UIImageView *icon;//圖片描述@property (nonatomic, strong) UILabel *descLabel;//左邊按鈕@property (nonatomic, strong) UIButton *leftButton;//右邊按鈕@property (nonatomic, strong) UIButton *rightButton;//圖片索引,index默認(rèn)是0@property (nonatomic, assign) int index;/**設(shè)置一個圖像的數(shù)組*///新的注釋,可以顯式中文@property (nonatomic, strong) NSArray *imageList;/* @property  自動為我們生成 set,get 方法的聲明和實(shí)現(xiàn) 帶下劃線的成員變量 */@end@implementation ViewController//控件懶加載//不需要每次都在 viewdidload 里實(shí)例化數(shù)組,只要在需要的時候?qū)嵗纯?/span>//重寫 get 方法- (NSArray *)imageList{    //只有第一次調(diào)用imageList 的 getter 方法的時候,如果為空,那么再實(shí)例化并建立數(shù)組,其他時候,直接返回成員變量    if (_imageList == nil) {        //使用字典        NSDictionary *dict1 = @{@"name" : @"biaoqingdi", @"desc" : @"表情"};        NSDictionary *dict2 = @{@"name" : @"bingli", @"desc" : @"病歷"};        NSDictionary *dict3 = @{@"name" : @"chiniupa", @"desc" : @"吃牛扒"};        NSDictionary *dict4 = @{@"name" : @"danteng", @"desc" : @"蛋疼"};        NSDictionary *dict5 = @{@"name" : @"wangba", @"desc" : @"王八"};                self.imageList = @[dict1, dict2, dict3, dict4, dict5];    }        return _imageList;}//初始化工作//viewDidLoad是視圖加載完成后調(diào)用的方法,通常在此方法中執(zhí)行視圖控制器的初始化工作- (void)viewDidLoad {    [super viewDidLoad];    //實(shí)例化控件    //1、序號標(biāo)簽的編寫    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 40)];  //  label.text = @"1/5";    //居中對齊    label.textAlignment = NSTextAlignmentCenter;    [self.view addSubview:label];    //記錄改變    self.noLabel = label;        //2、圖片控件    CGFloat imageW = 200;    CGFloat imageH = 200;    CGFloat imageX = (320 - imageW) / 2;    CGFloat imageY = 80;    //實(shí)例化一個圖像視圖    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];    //實(shí)例化一個圖像   // UIImage *image = [UIImage imageNamed:@"biaoqingdi"];    //把圖片顯示到imageView   // imageView.image = image;    //把圖像增加到 view    [self.view addSubview:imageView];    //記錄下改變    self.icon = imageView;        //3、圖片描述 label 控件    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 300, 80)];   // label1.text = @"發(fā)發(fā)發(fā)";    //居中對齊    label1.textAlignment = NSTextAlignmentCenter;    [self.view addSubview:label1];    //記錄改變    self.descLabel = label1;        //4、左邊的按鈕    UIButton *leftBtn = [[UIButton alloc] init];    //設(shè)置按鈕的背景圖    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];    //設(shè)置按鈕的大小    leftBtn.frame = CGRectMake(0, 0, 40, 40);    //設(shè)置按鈕的位置    leftBtn.center = CGPointMake(self.icon.frame.origin.x / 2, self.icon.center.y);    [self.view addSubview:leftBtn];    //設(shè)置監(jiān)聽    [leftBtn addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];    self.leftButton = leftBtn;        //5、右邊的按鈕    UIButton *rightBtn = [[UIButton alloc] init];    //設(shè)置按鈕的背景圖    [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];    [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];    //設(shè)置按鈕的大小    rightBtn.frame = CGRectMake(0, 0, 40, 40);    //設(shè)置按鈕的位置    rightBtn.center = CGPointMake(self.view.frame.size.width - self.icon.frame.origin.x / 2, self.icon.center.y);        [self.view addSubview:rightBtn];        //設(shè)置監(jiān)聽    [rightBtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];    self.rightButton = rightBtn;        [self change];}- (void)change{    //更具 self.index 來顯示序號標(biāo)簽,圖形,,描述    self.noLabel.text = [NSString stringWithFormat:@"%d / %d", self.index + 1, 5];    self.icon.image = [UIImage imageNamed:self.imageList[self.index][@"name"]];    self.descLabel.text = self.imageList[self.index][@"desc"];        self.leftButton.enabled = (self.index != 0);    self.rightButton.enabled = (self.index != 4);}//left- (void)leftClick{    self.index--;    [self change];}//right- (void)rightClick{    self.index++;    [self change];}@end

 

小結(jié):

/**設(shè)置一個圖像的數(shù)組*/

這是 xcode 的新的注釋,鼠標(biāo)浮動時,可以顯式出中文注釋。

 

手碼懶加載創(chuàng)建控件的步驟

1> 定義控件屬性,注意:屬性必須是strong的,如下:

@property (nonatomic, strong) UIImageView *icon;

 

2> 在屬性的getter方法中實(shí)現(xiàn)懶加載。

 

使用懶加載的好處

1> 不必將創(chuàng)建對象的代碼全部寫在viewDidLoad方法中,代碼的可讀性更強(qiáng)

2> 每個控件的getter方法中分別負(fù)責(zé)各自的實(shí)例化處理,代碼彼此之間的獨(dú)立性強(qiáng),松耦合

 

按鈕的狀態(tài)

normal(普通狀態(tài))
默認(rèn)情況
對應(yīng)的枚舉常量:UIControlStateNormal
 
highlighted(高亮狀態(tài))
按鈕被按下去的時候(手指還未松開)
對應(yīng)的枚舉常量:UIControlStateHighlighted
 
disabled(失效狀態(tài),不可用狀態(tài))
如果enabled屬性為NO,就是處于disable狀態(tài),代表按鈕不可以被點(diǎn)擊
對應(yīng)的枚舉常量:UIControlStateDisabled
 

使用Plist文件重構(gòu)本項目代碼:

目的:將數(shù)據(jù)與代碼分離(類似 java 里的 xml 文件寫數(shù)據(jù),數(shù)據(jù)和代碼分離)

之前的代碼,尤其是字典那部分,還是處理的不好,顯得太耦合。需要把數(shù)據(jù)和代碼分離,這里學(xué)習(xí)屬性列表文件,property list

新建file

本地文件,也可以網(wǎng)絡(luò)上解析 xml 文件

 

這樣,只需要修改對應(yīng)的 xml 文件即可,不用再打開代碼,修改代碼

//控件懶加載//不需要每次都在 viewdidload 里實(shí)例化數(shù)組,只要在需要的時候?qū)嵗纯?/span>- (NSArray *)imageList{    //只有第一次調(diào)用imageList 的 getter 方法的時候,如果為空,那么再實(shí)例化并建立數(shù)組,其他時候,直接返回成員變量    if (_imageList == nil) {        //bundle 包的概念  只讀        NSString *path = [[NSBundle mainBundle] pathForResource:@"imageDate" ofType:@".plist"];        NSLog(@"%@", path);        //File 表示從完整路徑查找文件        _imageList = [NSArray arrayWithContentsOfFile:path];    }        return _imageList;}

小結(jié):

1、將數(shù)據(jù)與代碼分離,Plist 文件的加載方法:

直接將數(shù)據(jù)直接寫在代碼里面,不是一種合理的做法。如果數(shù)據(jù)經(jīng)常改,就要經(jīng)常翻開對應(yīng)的代碼進(jìn)行修改,造成代碼擴(kuò)展性低,因此,可以考慮將經(jīng)常變的數(shù)據(jù)放在文件中進(jìn)行存儲,程序啟動后從文件中讀取最新的數(shù)據(jù)。如果要變動數(shù)據(jù),直接修改數(shù)據(jù)文件即可,不用修改代碼。
一般可以使用屬性列表文件存儲NSArray或者NSDictionary之類的數(shù)據(jù),這種屬性列表文件的擴(kuò)展名是plist,因此也成為“Plist文件”

NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageData" ofType:@"plist"];

_imageList = [NSArray arrayWithContentsOfFile:path];

提示:通常在方法中出現(xiàn)File字眼,通常需要傳遞文件的全路徑作為參數(shù),如下全路徑:

/Users/dashuai/Library/Developer/CoreSimulator/Devices/83C611C9-DE98-4D02-BC64-D31C0403766E/data/Containers/Bundle/application/E04713CF-A9D4-49D1-A934-B4093BCE5B3C/圖片瀏覽.app/imageDate.plist

 

2、要想讓UILabel自動換行,設(shè)置Lines為0即可。

 

3、UIButton和UIImageView

相同點(diǎn)
都能顯示圖片
 
不同點(diǎn)
UIButton默認(rèn)情況就能監(jiān)聽點(diǎn)擊事件,而UIImageView默認(rèn)情況下不能
UIButton可以在不同狀態(tài)下顯示不同的圖片
UIButton既能顯示文字,又能顯示圖片
 
如何選擇
UIButton:需要顯示圖片,點(diǎn)擊圖片后需要做一些特定的操作
UIImageView:僅僅需要顯示圖片,點(diǎn)擊圖片后不需要做任何事情
 
NSArray和NSDictionary的使用
當(dāng)圖片內(nèi)容非常多時,“根據(jù)index來設(shè)置內(nèi)容”的代碼就不具備擴(kuò)展性,要經(jīng)常改動,為了改變現(xiàn)狀,可以考慮將圖片數(shù)據(jù)保存到一個數(shù)組中,數(shù)組中有序地放著很多字典,一個字典代表一張圖片數(shù)據(jù),包含了圖片名、圖片描述

@property (strong, nonatomic) NSArray *images;

由于只需要初始化一次圖片數(shù)據(jù),因此放在get方法中初始化,將屬性放在get方法中初始化的方式,稱為“懶加載”/”延遲加載”

 

/**設(shè)置一個圖像的數(shù)組*/

這是 xcode 的新的注釋,鼠標(biāo)浮動時,可以顯式出中文注釋。

 

手碼懶加載創(chuàng)建控件的步驟

1> 定義控件屬性,注意:屬性必須是strong的,如下:

@property (nonatomic, strong) UIImageView *icon;

 

2> 在屬性的getter方法中實(shí)現(xiàn)懶加載。

 

使用懶加載的好處:

1> 不必將創(chuàng)建對象的代碼全部寫在viewDidLoad方法中,代碼的可讀性更強(qiáng)

2> 每個控件的getter方法中分別負(fù)責(zé)各自的實(shí)例化處理,代碼彼此之間的獨(dú)立性強(qiáng),松耦合

 

按鈕的狀態(tài)

normal(普通狀態(tài))
默認(rèn)情況
對應(yīng)的枚舉常量:UIControlStateNormal
 
highlighted(高亮狀態(tài))
按鈕被按下去的時候(手指還未松開)
對應(yīng)的枚舉常量:UIControlStateHighlighted
 
disabled(失效狀態(tài),不可用狀態(tài))
如果enabled屬性為NO,就是處于disable狀態(tài),代表按鈕不可以被點(diǎn)擊
對應(yīng)的枚舉常量:UIControlStateDisabled 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久福利小视频 | 久久99国产精品免费网站 | 久久国产精品二国产精品 | 成人毛片免费播放 | 国产一级在线观看视频 | 欧美一级高清免费 | 免费三级大片 | 黄色大片免费网站 | 国产成人精品免费视频大全办公室 | 毛片免费观看视频 | 女18一级大黄毛片免费女人 | 国产成人小视频在线观看 | 久久久一区二区三区精品 | 日本aaaa片毛片免费观看视频 | 日韩在线播放一区二区 | 桥本有菜免费av一区二区三区 | 久久久日韩av免费观看下载 | 成人一级在线 | 国产99一区二区 | 在线成人看片 | 免费看操片 | 国产99免费 | 国产精品99久久久久久久vr | 日日草天天干 | 大逼逼影院 | 国语自产免费精品视频在 | 久久99精品久久 | 2019中文字幕在线播放 | 黄色网址免费在线播放 | 日本欧美一区二区三区视频麻豆 | 国产精品v片在线观看不卡 成人一区二区三区在线 | 一级毛片电影网 | hdhdhd79xxxxх | 成年人在线视频 | 免费亚洲视频在线观看 | 视屏一区 | 在线91视频 | 日本免费aaa观看 | xxxx18韩国护士hd老师 | 久久99在线 | 久草在线播放视频 |