標籤:
知識準備:
UIbutton 和UIimageview的異同:
相同點:》 都能顯示圖片
不同點:》 UIButton預設情況就能監聽點擊事件,而UIImageView預設情況下不能
》 UIButton可以在不同狀態下顯示不同的圖片
》 UIButton既能顯示文字,又能顯示圖片
如何選擇:》 UIButton:需要顯示圖片,點擊圖片後需要做一些特定的操作
》 UIImageView:僅僅需要顯示圖片,點擊圖片後不需要做任何事情
NSArray和NSDictionary的使用:
當圖片內容非常多時,“根據index來設定內容”的代碼就不具備擴充性,要經常改動
為了改變現狀,可以考慮講圖片資料線儲存到一個數組中,數組中有序地放著很多字典,一個字典代表一張圖片資料,包含了圖片名、圖片描述 @property (strong, nonatomic) NSArray *images;
“懶載入”\”消極式載入”
由於只需要初始化一次圖片資料,因此放在get/set方法中初始化/調用
將屬性放在get方法中初始化的方式,稱為“懶載入”\”消極式載入”
什麼是Plist檔案
》 直接將資料直接寫在代碼裡面,不是一種合理的做法。如果資料經常改,就要經常翻開對應的代碼進行修改,造成代碼擴充性低
》 因此,可以考慮將經常變的資料放在檔案中進行儲存,程式啟動後從檔案中讀取最新的資料。如果要變動資料,直接修改資料檔案即可,不用修改代碼
》 一般可以使用屬性列表檔案儲存體NSArray或者NSDictionary之類的資料,這種屬性列表檔案的副檔名是plist,因此也稱為“Plist檔案”
功能分析
》點擊箭頭切換序號、圖片、描述
》如果是首張圖片,左邊箭頭不能點擊
》如果是尾張圖片,右邊箭頭不能點擊
步驟分析
》搭建UI介面
》監聽按鈕點擊
》切換序號、圖片、描述
程式實現
//// kViewController.m// 圖片瀏覽器//// Created by Kengsir on 15-1-8.// Copyright (c) 2015年 ___FULLUSERNAME___. All rights reserved.////#import "kViewController.h"#define kicon @"icon"#define kdesc @"desc"@interface kViewController ()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *noLabel;@property (weak, nonatomic) IBOutlet UILabel *descLabel;@property (weak, nonatomic) IBOutlet UIButton *leftbtn;@property (weak, nonatomic) IBOutlet UIButton *rightbtn;- (IBAction)right;- (IBAction)left;//一般對象用strong,控制項用weak@property (assign,nonatomic)int index;@property (strong,nonatomic) NSArray *array;@end@implementation kViewController- (void)viewDidLoad{ [super viewDidLoad]; [self changeData];}//消極式載入資料,將屬性放在get方法中初始化的方式,稱為懶載入-(NSArray *)array{ if(_array ==nil){//從未初始化過 //初始化資料 NSMutableDictionary *image1 = [NSMutableDictionary dictionary]; image1[kicon] = @"biaoqingdi"; image1[kdesc] = @"表情帝"; NSMutableDictionary *image2 = [NSMutableDictionary dictionary]; image2[kicon] = @"wangba"; image2[kdesc] = @"王八"; NSMutableDictionary *image3 = [NSMutableDictionary dictionary]; image3[kicon] = @"wangba"; image3[kdesc] = @"王八"; //快速建立數組,將字典放入數組 self.array = @[image1,image2,image3]; } return _array;}//程式的擴充性要好-(void)changeData{ //1. 改變資料 self.noLabel.text = [NSString stringWithFormat:@"%d/%d",self.index + 1 , self.array.count]; //2.根據數組index 取出 對應的字典資料 NSDictionary *imagedict = self.array[self.index]; NSLog(@"%@",imagedict); //3.設定圖片 //NSString *name = imagedict[kicon]; self.iconView.image = [UIImage imageNamed:imagedict[kicon]]; //4.設定描述 self.descLabel.text = imagedict[kdesc]; //改變按鈕判斷狀態// if(self.index == 0){// self.leftbtn.enabled = NO;// }else {// self.leftbtn.enabled = YES;// }// if(self.index == 1){// self.rightbtn.enabled = NO;// }else {// self.rightbtn.enabled = YES;// } self.leftbtn.enabled = (self.index == 0)?NO:YES; self.rightbtn.enabled = (self.index != self.array.count - 1);}- (IBAction)right{ self.index ++ ; [self changeData];}- (IBAction)left { self.index --; [self changeData]; }@end
效果:
最佳化:使用plist儲存資料
通過代碼來解析Plist檔案中的資料獲得Plist檔案的全路徑NSBundle *bundle = [NSBundle mainBundle];NSString *path = [bundle pathForResource:@"imageData" ofType:@"plist"];改寫本來寫死的初始化資料的get方法用來載入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;}
iOS基礎控制項之 圖片瀏覽器