码迷,mamicode.com
首页 > 其他好文 > 详细

(纯代码)图片浏览器:

时间:2015-03-12 00:50:27      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

#define kImage @"image"
#define kDescribe @"describe"

@interface ViewController ()

@property (nonatomic, weak) UIImageView *proImageView;
@property (nonatomic, weak) UILabel *proLabImageNumber;
@property (nonatomic, weak) UILabel *proLabImageDescribe;
@property (nonatomic, weak) UIButton *proBtnLeft;
@property (nonatomic, weak) UIButton *proBtnRight;

@property (nonatomic, assign) int index;
@property (nonatomic, strong) NSArray *imageData;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /** 显示页面数 */
    UILabel *labImageNumber = [[UILabel alloc] initWithFrame:CGRectMake(175, 50, 30, 25)];
    labImageNumber.text = [NSString stringWithFormat:@"%d/%lu",self.index+1,self.imageData.count];
    labImageNumber.textColor = [UIColor greenColor];
    [self.view addSubview:labImageNumber];
    self.proLabImageNumber = labImageNumber;
    
    /** 图片浏览 */
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"biaoqingdi"]];
    imageView.frame = CGRectMake(80, 100, 200, 250);
    [self.view addSubview:imageView];
    self.proImageView = imageView;
    
    /** 左翻页 */
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLeft.frame = CGRectMake(20, 210, 50, 50);
    [btnLeft setImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [btnLeft setImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
    [btnLeft setImage:[UIImage imageNamed:@"left_disable"] forState:UIControlStateDisabled];
    // 设置按钮状态为禁止使用,参数enabled为NO
    btnLeft.enabled = NO;
    [self.view addSubview:btnLeft];
    [btnLeft setTag:1];
    [btnLeft addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.proBtnLeft = btnLeft;
    
    /** 右翻页 */
    UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRight.frame = CGRectMake(300, 210, 50, 50);
    [btnRight setImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [btnRight setImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
    [btnRight setImage:[UIImage imageNamed:@"right_disable"] forState:UIControlStateDisabled];
    [self.view addSubview:btnRight];
    [btnRight setTag:2];
    [btnRight addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.proBtnRight = btnRight;
    
    /** 图片描述 */
    UILabel *labImageDescribe = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 200, 50)];
    labImageDescribe.textColor = [UIColor redColor];
    labImageDescribe.text = @"瞧这熊孩子。。。";
    [self.view addSubview:labImageDescribe];
    self.proLabImageDescribe = labImageDescribe;
    
    
}

// 重写imageData的get方法
- (NSArray *)imageData
{

    if (_imageData == nil) { // 从未初始化
        
        // 初始化数据
        // File : 全路径
        // NSBundle : 一个NSBundle代表一个文件夹
        
        // 利用mainBundle就可以访问软件资源包中的任何资源
        NSBundle *bundel = [NSBundle mainBundle];
        NSString *path = [bundel pathForResource:@"ImageAndDescribe" ofType:@"plist"];
        _imageData = [NSArray arrayWithContentsOfFile:path];
        
    }
    
    return _imageData;
}

- (void) changeImageData
{
    // 改变按钮状态
    self.proBtnLeft.enabled = (self.index != 0 );
    self.proBtnRight.enabled = (self.index != self.imageData.count-1);
    
    // 改变labImageNumber控件文字
    self.proLabImageNumber.text = [NSString stringWithFormat:@"%d/%lu",self.index+1,(unsigned long)self.imageData.count];
    
    // 2.取出index对应的字典数据
    NSDictionary *imageDict = self.imageData[self.index];
    
    // 设置图片
    self.proImageView.image = [UIImage imageNamed:imageDict[kImage]];
    self.proLabImageDescribe.text = imageDict[kDescribe];
    
}


// 监听
- (void) btnClick: (UIButton *) btn
{
    switch (btn.tag) {
        case 1:
            self.index--;
            [self changeImageData];
            break;
            
        case 2:
            self.index++;
            [self changeImageData];
            break;
    }
}


@end

 

(纯代码)图片浏览器:

标签:

原文地址:http://www.cnblogs.com/pocket-mood/p/4331301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!