你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開辟中完成一個簡略的圖片閱讀器的實例講授

iOS開辟中完成一個簡略的圖片閱讀器的實例講授

編輯:IOS開發綜合

1、法式完成請求

1.請求

201611392120159.png (663×410)

2. 界面剖析

(1) 須要讀取或修正屬性的控件須要設置屬性

  • 序號標簽
  • 圖片
  • 圖片描寫
  • 右邊按鈕
  • 左邊按鈕

(2) 須要監聽呼應事宜的對象,須要添加監聽辦法

  • 右邊按鈕
  • 左邊按鈕

2、完成根本功效的法式

//
//  YYViewController.m
//  03-圖片閱讀器初步
//
//  Created by apple on 14-5-21.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

#define POTOIMGW    200
#define POTOIMGH    300
#define POTOIMGX    60
#define  POTOIMGY    50


@interface YYViewController ()

//變量聲明!
@property(nonatomic,strong)UILabel *firstlab;
@property(nonatomic,strong)UILabel *lastlab;
@property(nonatomic,strong)UIImageView *icon;
@property(nonatomic,strong)UIButton *leftbtn;
@property(nonatomic,strong)UIButton *rightbtn;

-(void)change;
@property(nonatomic ,assign)int i;
@end

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.i=0;
    //創立一個用來顯示序號的lable控件
    UILabel *headlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
   
  // [headlab setText:@"1/5"];
    [headlab setTextAlignment:NSTextAlignmentCenter];
    [headlab setTextColor:[UIColor blackColor]];
   
    [self.view addSubview:headlab];
    self.firstlab=headlab;
   
   
   
    //創立一個裝載圖片的控件
    UIImageView *potoimg=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)];
   
    UIImage *image=[UIImage imageNamed:@"biaoqingdi"];
    potoimg.image=image;
   
    [self.view addSubview:potoimg];
    self.icon=potoimg;
   
   
   
    //創立最下邊的描寫圖片的lable控件
    UILabel *desclab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)];
   // [desclab setText:@"臉色弱爆了!"];
    [desclab setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:desclab];
    self.lastlab=desclab;
   
   
    //創立兩個偏向鍵按鈕
    //設置為自界說類型
    //1.應用類創立對象
    UIButton *leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];
   
    //2.設置對象的屬性(不要忘卻設置坐標)
    leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40);
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
   
    //3.提交對象到視圖
    [self.view addSubview:leftbtn];
   
    self.leftbtn=leftbtn;
    [leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside];
   
   
    UIButton *rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];
   
    rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40);
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
   
    [self.view addSubview:rightbtn];
   
    self.rightbtn=rightbtn;
    [rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside];
   
    //這是一個初始化辦法,挪用change可以完成初始化的任務
    [self change];
}

-(void)change
{
    [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]];
    switch (self.i) {
        case 0:
            self.lastlab.text=@"甚麼臉色都弱爆了!";
            self.icon.image=[UIImage imageNamed:@"biaoqingdi"];
            break;
        case 1:
            self.lastlab.text=@"病例";
            self.icon.image=[UIImage imageNamed:@"bingli"];
            break;
        case 2:
           self.lastlab.text=@"王八";
            self.icon.image=[UIImage imageNamed:@"wangba"];
            break;
        case 3:
           self.lastlab.text=@"吃牛扒";
            self.icon.image=[UIImage imageNamed:@"chiniupa"];
            break;
        case 4:
             self.lastlab.text=@"蛋疼!";
            self.icon.image=[UIImage imageNamed:@"danteng"];
            break;
    }
    //掌握按鈕的點擊,假如為5則右鍵掉效,假如為1,則左鍵掉效
    self.leftbtn.enabled=(self.i!=0);
    self.rightbtn.enabled=(self.i!=4);

}

//向右按鍵
-(void)rightclick:(UIButton *)btn
{
    self.i++;
    [self change];
    //NSLog(@"點我了");
}
-(void)leftclick:(UIButton *)btn
{
    self.i--;
    [self change];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3、法式優化

//
//  YYViewController.m
//  03-圖片閱讀器初步
//
//  Created by apple on 14-5-21.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

#define POTOIMGW    200
#define POTOIMGH    300
#define POTOIMGX    60
#define  POTOIMGY    50


@interface YYViewController ()

//變量聲明!
@property(nonatomic,strong)UILabel *firstlab;
@property(nonatomic,strong)UILabel *lastlab;
@property(nonatomic,strong)UIImageView *icon;
@property(nonatomic,strong)UIButton *leftbtn;
@property(nonatomic,strong)UIButton *rightbtn;

@property(nonatomic,strong)NSArray *array;

-(void)change;
@property(nonatomic ,assign)int i;
@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.i=0;
    //創立一個用來顯示序號的lable控件
    UILabel *headlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
   
  // [headlab setText:@"1/5"];
    [headlab setTextAlignment:NSTextAlignmentCenter];
    [headlab setTextColor:[UIColor blackColor]];
   
    [self.view addSubview:headlab];
    self.firstlab=headlab;
   
   
   
    //創立一個裝載圖片的控件
    UIImageView *potoimg=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)];
   
    UIImage *image=[UIImage imageNamed:@"biaoqingdi"];
    potoimg.image=image;
   
    [self.view addSubview:potoimg];
    self.icon=potoimg;
   
   
   
    //創立最下邊的描寫圖片的lable控件
    UILabel *desclab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)];
   // [desclab setText:@"臉色弱爆了!"];
    [desclab setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:desclab];
    self.lastlab=desclab;
   
   
   
    //創立兩個偏向鍵按鈕
    //設置為自界說類型
    //1.應用類創立對象
    UIButton *leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];
   
    //2.設置對象的屬性(不要忘卻設置坐標)
    leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40);
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
   
    //3.提交對象到視圖
    [self.view addSubview:leftbtn];
   
    self.leftbtn=leftbtn;
    [leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside];
   
   
    UIButton *rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];
   
    rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40);
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
   
    [self.view addSubview:rightbtn];
   
    self.rightbtn=rightbtn;
    [rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside];
    //放在這裡的話,只會創立一次,然則這個部門和[self change];部門有很嚴厲的次序請求,其實不人道化,可以斟酌應用懶加載特征
//    NSDictionary *dict1=@{@"name": @"biaoqingdi",@"desc":@"甚麼臉色都弱爆了!"};
//    NSDictionary *dict2=@{@"name": @"bingli",@"desc":@"病例"};
//    NSDictionary *dict3=@{@"name": @"wangba",@"desc":@"烏龜"};
//    NSDictionary *dict4=@{@"name": @"chiniupa",@"desc":@"吃牛扒"};
//    NSDictionary *dict5=@{@"name": @"danteng",@"desc":@"蛋疼"};
//   
//    self.array=@[dict1,dict2,dict3,dict4,dict5];
    //這是一個初始化辦法,挪用change可以完成初始化的任務
    [self change];
}

-(void)change
{
    //每次挪用都須要創立?有無甚麼處理方法?
//    NSDictionary *dict1=@{@"name": @"biaoqingdi",@"desc":@"甚麼臉色都弱爆了!"};
//    NSDictionary *dict2=@{@"name": @"bingli",@"desc":@"病例"};
//    NSDictionary *dict3=@{@"name": @"wangba",@"desc":@"烏龜"};
//    NSDictionary *dict4=@{@"name": @"chiniupa",@"desc":@"吃牛扒"};
//    NSDictionary *dict5=@{@"name": @"danteng",@"desc":@"蛋疼"};
//   
//    NSArray *array=@[dict1,dict2,dict3,dict4,dict5];
   
   
    //設置照片
    //先依據self.i掏出數組中的元素,再掏出元素(字典)中鍵值對應的值
//    self.icon.image=[UIImage imageNamed:array[self.i][@"name"]];
//    self.lastlab.text=array[self.i][@"desc"];
   // NSLog(@"%@",array[self.i][@"desc"]);
   
    self.icon.image=[UIImage imageNamed:self.array[self.i][@"name"]];
    self.lastlab.text=self.array[self.i][@"desc"];
   
    [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]];
   
//    switch (self.i) {
//        case 0:
//            self.lastlab.text=@"甚麼臉色都弱爆了!";
//            self.icon.image=[UIImage imageNamed:@"biaoqingdi"];
//            break;
//        case 1:
//            self.lastlab.text=@"病例";
//            self.icon.image=[UIImage imageNamed:@"bingli"];
//            break;
//        case 2:
//           self.lastlab.text=@"王八";
//            self.icon.image=[UIImage imageNamed:@"wangba"];
//            break;
//        case 3:
//           self.lastlab.text=@"吃牛扒";
//            self.icon.image=[UIImage imageNamed:@"chiniupa"];
//            break;
//        case 4:
//             self.lastlab.text=@"蛋疼!";
//            self.icon.image=[UIImage imageNamed:@"danteng"];
//            break;
//    }
    //掌握按鈕的點擊,假如為5則右鍵掉效,假如為1,則左鍵掉效
    self.leftbtn.enabled=(self.i!=0);
    self.rightbtn.enabled=(self.i!=4);

}

//array的get辦法
-(NSArray *)array
{
    NSLog(@"須要獲得數組");
    //只實例化一次
    if (_array==nil) {
        NSLog(@"實例化數組");
        NSDictionary *dict1=@{@"name": @"biaoqingdi",@"desc":@"甚麼臉色都弱爆了!"};
        NSDictionary *dict2=@{@"name": @"bingli",@"desc":@"病例"};
        NSDictionary *dict3=@{@"name": @"wangba",@"desc":@"烏龜"};
        NSDictionary *dict4=@{@"name": @"chiniupa",@"desc":@"吃牛扒"};
        NSDictionary *dict5=@{@"name": @"danteng",@"desc":@"蛋疼"};
        _array=@[dict1,dict2,dict3,dict4,dict5];
    }
//    NSDictionary *dict1=@{@"name": @"biaoqingdi",@"desc":@"甚麼臉色都弱爆了!"};
//    NSDictionary *dict2=@{@"name": @"bingli",@"desc":@"病例"};
//    NSDictionary *dict3=@{@"name": @"wangba",@"desc":@"烏龜"};
//    NSDictionary *dict4=@{@"name": @"chiniupa",@"desc":@"吃牛扒"};
//    NSDictionary *dict5=@{@"name": @"danteng",@"desc":@"蛋疼"};
   
   // _array=@[dict1,dict2,dict3,dict4,dict5];
    return _array;
}

//向右按鍵
-(void)rightclick:(UIButton *)btn
{
    self.i++;
    [self change];
}

//向左按鍵
-(void)leftclick:(UIButton *)btn
{
    self.i--;
    [self change];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

解釋:

1> 界說控件屬性,留意:屬性必需是strong的,示例代碼以下:

@property (nonatomic, strong) UIImageView *icon;

2> 在屬性的getter辦法中完成懶加載,示例代碼以下:

- (UIImageView *)icon

{

    if (!_icon) {

        // 盤算地位參數

        CGFloat imageW = 200;

        CGFloat imageX = (320 - imageW) / 2;

        CGFloat imageH = 200;

        CGFloat imageY = 80;

        // 實例化圖象視圖

        _icon = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];

        // 將圖象視圖添加到主視圖

        [self.view addSubview:_icon];

    }

    return _icon;

}

4、應用plist文件

(1)應用Plist文件的目標:將數據與代碼分別

(2)加載辦法:

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

_imageList = [NSArray arrayWithContentsOfFile:path];

提醒:平日在辦法中湧現File字眼,平日須要傳遞文件的全途徑作為參數

(3)代碼示例

//
//  YYViewController.m
//  03-圖片閱讀器初步
//
//  Created by apple on 14-5-21.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

#define POTOIMGW    200
#define POTOIMGH    300
#define POTOIMGX    60
#define  POTOIMGY    50


@interface YYViewController ()

//變量聲明!
@property(nonatomic,strong)UILabel *firstlab;
@property(nonatomic,strong)UILabel *lastlab;
@property(nonatomic,strong)UIImageView *icon;
@property(nonatomic,strong)UIButton *leftbtn;
@property(nonatomic,strong)UIButton *rightbtn;

@property(nonatomic,strong)NSArray *array;

-(void)change;
@property(nonatomic ,assign)int i;
@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.i=0;
    //創立一個用來顯示序號的lable控件
    UILabel *headlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
   
  // [headlab setText:@"1/5"];
    [headlab setTextAlignment:NSTextAlignmentCenter];
    [headlab setTextColor:[UIColor blackColor]];
   
    [self.view addSubview:headlab];
    self.firstlab=headlab;
   
   
   
    //創立一個裝載圖片的控件
    UIImageView *potoimg=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)];
   
    UIImage *image=[UIImage imageNamed:@"biaoqingdi"];
    potoimg.image=image;
   
    [self.view addSubview:potoimg];
    self.icon=potoimg;
   
    //創立最下邊的描寫圖片的lable控件
    UILabel *desclab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)];
   // [desclab setText:@"臉色弱爆了!"];
    [desclab setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:desclab];
    self.lastlab=desclab;
   
   
    //創立兩個偏向鍵按鈕
    //設置為自界說類型
    //1.應用類創立對象
    UIButton *leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];
   
    //2.設置對象的屬性(不要忘卻設置坐標)
    leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40);
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
   
    //3.提交對象到視圖
    [self.view addSubview:leftbtn];
   
    self.leftbtn=leftbtn;
    [leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside];
   
   
    UIButton *rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];
   
    rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40);
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
   
    [self.view addSubview:rightbtn];
   
    self.rightbtn=rightbtn;
    [rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside];
    [self change];
}

-(void)change
{
    self.icon.image=[UIImage imageNamed:self.array[self.i][@"name"]];
    self.lastlab.text=self.array[self.i][@"desc"];
   
    [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]];
   
    self.leftbtn.enabled=(self.i!=0);
    self.rightbtn.enabled=(self.i!=4);

}

//array的get辦法
-(NSArray *)array
{
    NSLog(@"須要獲得數組");
    //只實例化一次
    if (_array==nil) {
    
        NSString *path=[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
        //數組的數據從文件獲得
       // _array=[NSArray arrayWithContentsOfFile:path];
        _array=[[NSArray alloc]initWithContentsOfFile:path];
        //打印檢查包的地位
        NSLog(@"%@",path);
       
       NSLog(@"實例化數組");
    }

    return _array;
}

//向右按鍵
-(void)rightclick:(UIButton *)btn
{
    self.i++;
    [self change];
}

//向左按鍵
-(void)leftclick:(UIButton *)btn
{
    self.i--;
    [self change];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

(4)plist文件

201611392221000.png (868×338)

(5)完成後果

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615472215.png (640×960)

5、彌補

開辟思緒:

1.完成根本功效

2.斟酌機能

(1)(初始化操作,可以直接挪用change停止)

(2)由於要掌握序號和圖片兩個變量,所以斟酌應用字典取代失落switch

(3)每次點擊,字典都須要創立一次,效力地下,可以斟酌創立的這部門拿到初始化辦法中去,如許就只須要創立一次就ok了。

(4)斟酌缺陷(對代碼的次序請求極端嚴厲)

(5)懶加載(須要的時刻才加載,那末甚麼時刻是須要的時刻,及挪用get辦法的時刻)

(6)每次都來一下?效力低下—》只要第一次挪用get辦法時為空,此時實例化並樹立數組,其他時刻直接前往成員變量(僅僅履行一次)

留意點:

1.辦法的挪用客棧(次序)。

2.應用plist:讓數據的操作加倍靈巧,把數據弄到裡面去,消除耦合性,讓耦合性不要太強。現實上是一個XmlRss/ target=_blank class=infotextkey>Xml,是蘋果界說的一種特別格局的XmlRss/ target=_blank class=infotextkey>Xml。

3.bundle-包(只讀)

【iOS開辟中完成一個簡略的圖片閱讀器的實例講授】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved