zoukankan      html  css  js  c++  java
  • (纯代码)图片浏览器:

    #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
  • 相关阅读:
    大型网站架构系列:负载均衡详解(1)
    转:构建高并发高可用的电商平台架构实践
    转:RBAC权限控制
    小型电商网站的架构
    中小型电子商务网站架构
    装饰器在类中的实现
    使用MySQLdb操作Mysql数据库
    unicode转中文以及str形态的unicode转中文
    了解Python内存管理机制,让你的程序飞起来
    多线程初级入门学习
  • 原文地址:https://www.cnblogs.com/pocket-mood/p/4331301.html
Copyright © 2011-2022 走看看