IOS開發學習筆記八 圖片瀏覽器
阿新 • • 發佈:2018-11-13
首先是效果圖:demo下載
- 把圖片素材放入Assets.xcassets
- 建立plist檔案(本質是xml檔案)
- 介面的搭建,按照頁面佈局拖拽新增控制元件
- ViewController具體程式碼
#import "ViewController.h"
@interface ViewController ()
// 自己寫一個索引, 來控制當前顯示的是第幾張圖片
// 這個屬性一開始沒有賦值就是0
@property (nonatomic, strong) NSArray *pic;
@property (nonatomic, assign) int index;
//當前序號
@property (weak, nonatomic) IBOutlet UILabel *labIndex;
//圖片素材
@property (weak, nonatomic) IBOutlet UIImageView *ivPic;
//標題
@property (weak, nonatomic) IBOutlet UILabel *labTxt;
//上一張瀏覽按鈕
@property (weak, nonatomic) IBOutlet UIButton *btnPreview;
//下一張瀏覽按鈕
@property (weak , nonatomic) IBOutlet UIButton *btnNext;
- (IBAction)preview:(UIButton *)sender;
- (IBAction)next:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnPreviewNew;
@property (weak, nonatomic) IBOutlet UIButton *btnNextNew;
- (IBAction)previewNew:(id)sender;
- (IBAction)nextNew:(id )sender;
@end
@implementation ViewController
// 重寫pic屬性的get方法
//------------- 懶載入資料 -----------------
- (NSArray *)pic
{
if (_pic == nil) {
// 寫程式碼載入pic.plist檔案中的資料到_pic
// 1. 獲取pic.plist檔案的路徑
// 獲取pic.plist檔案的路徑賦值給path變數
// [NSBundle mainBundle]表示獲取這個app安裝到手機上時的根目錄
// 然後在app的安裝的根目錄下搜尋pic.plist檔案的路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"pic.plist" ofType:nil];
// let imgPath:String = Bundle.main.path(pathForResource: "a.jpg", ofType: "")!;
// 讀取檔案
NSArray *array = [NSArray arrayWithContentsOfFile:path];
_pic = array;
}
return _pic;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.btnPreview.enabled = false;
self.btnPreviewNew.enabled = false;
// 3. 把獲取到的資料設定給介面上的控制元件
self.labIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index + 1, self.pic.count];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)preview:(UIButton *)sender {
// 1. 讓索引++
self.index--;
// 2. 從陣列中獲取當前這張圖片的資料
NSDictionary *dict = self.pic[self.index];
// 3. 把獲取到的資料設定給介面上的控制元件
self.labIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index + 1, self.pic.count];
// 通過image屬性來設定圖片框裡面的圖片
self.ivPic.image = [UIImage imageNamed:dict[@"icon"]];
// 設定這張圖片的標題
self.labTxt.text = dict[@"title"];
//上一張按鈕可以點選
self.btnNext.enabled = true;
self.btnNextNew.enabled = true;
if(self.index == 0){
//下一張按鈕不可以點選
self.btnPreview.enabled = false;
self.btnPreviewNew.enabled = false;
}
}
- (IBAction)next:(UIButton *)sender {
// 1. 讓索引++
self.index++;
// 2. 從陣列中獲取當前這張圖片的資料
NSDictionary *dict = self.pic[self.index];
// 3. 把獲取到的資料設定給介面上的控制元件
self.labIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index + 1, self.pic.count];
// 通過image屬性來設定圖片框裡面的圖片
self.ivPic.image = [UIImage imageNamed:dict[@"icon"]];
// 設定這張圖片的標題
self.labTxt.text = dict[@"title"];
//上一張按鈕可以點選
self.btnPreview.enabled = true;
self.btnPreviewNew.enabled = true;
if(self.index == self.pic.count-1){
//下一張按鈕不可以點選
self.btnNext.enabled = false;
self.btnNextNew.enabled = false;
}
}
- (IBAction)previewNew:(id)sender {
self.index--;
[self setData];
}
- (IBAction)nextNew:(id)sender {
self.index++;
[self setData];
}
// 設定控制元件資料
- (void)setData
{
// 2. 從陣列中獲取當前這張圖片的資料
NSDictionary *dict = self.pic[self.index];
// 3. 把獲取到的資料設定給介面上的控制元件
self.labIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index + 1, self.pic.count];
// 通過image屬性來設定圖片框裡面的圖片
self.ivPic.image = [UIImage imageNamed:dict[@"icon"]];
// 設定這張圖片的標題
self.labTxt.text = dict[@"title"];
// 控制上一張與下一張按鈕是否可用
self.btnPreview.enabled = (self.index != 0);
self.btnPreviewNew.enabled = (self.index != 0);
self.btnNext.enabled = (self.index != (self.pic.count - 1));
self.btnNextNew.enabled = (self.index != (self.pic.count - 1));
}
@end