iOS学习笔记-016.图片浏览器

图片浏览器


一、代码

1.数据类 ImageModel.m

//
//  ImageModel.m
//  03_UIView04_图片浏览器
//
//  Created by 杞文明 on 15/12/22.
//  Copyright © 2015年 杞文明. All rights reserved.
//

#import "ImageModel.h"
@interface ImageModel(){

    UIImage* _image;
}

@end

@implementation ImageModel

-(UIImage*)image{
    if(!_image){
        _image = [UIImage imageNamed:self.icon];
    }
    return _image;
}

-(instancetype)initWithDict:(NSDictionary *)dict{
    if(self=[super init]){
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+(instancetype)imageModelWithDict:(NSDictionary *)dict{
    return [[self alloc]initWithDict:dict];
}

+(NSArray*)imageModels{
    NSArray* array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"]];
    NSMutableArray* arrayM = [NSMutableArray array];
    for (NSDictionary* dict in array) {
        [arrayM addObject:[ImageModel imageModelWithDict:dict]];
    }
    return arrayM;
}

@end

2.ViewController.m

//
//  ViewController.m
//  03_UIView04_图片浏览器
//
//  Created by 杞文明 on 15/12/22.
//  Copyright © 2015年 杞文明. All rights reserved.
//
#import "ViewController.h"
#import "ImageModel.h"
@interface ViewController (){
    NSInteger _photoIndex;
}
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置显示的图片
    _photoIndex = 0;
    [self setShow:true];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
/**开关*/
- (IBAction)switchchange:(id)sender {
    if([_switchView isOn]){
        self.view.backgroundColor = [UIColor grayColor];
        [_nightLb setText:@"白天模式"];
    }else{
        self.view.backgroundColor = [UIColor whiteColor];
        [_nightLb setText:@"夜间模式"];
    }
}
/**计数器*/
- (IBAction)stepperChange:(id)sender {
    NSInteger nowIndex = _stepper.value-1;
    if(nowIndex!=_photoIndex && nowIndex>=0){
        _photoIndex = nowIndex;
        [self setShow:false];
    }
}
/**滑块*/
- (IBAction)sliderChange:(id)sender {
    NSInteger nowIndex = _slider.value-1;
    if(nowIndex!=_photoIndex&& nowIndex>=0){
        _photoIndex = nowIndex;
        [self setShow:true];
    }
}
/**设置显示*/
-(void)setShow:(BOOL)isStepper{
    //位置的显示
    [_indexLb setText:[NSString stringWithFormat:@"%ld/%ld",_photoIndex+1,[ImageModel imageModels].count]];
    ImageModel* model = (ImageModel*)[ImageModel imageModels][_photoIndex];
    //图片的显示
    [_imageView setImage: model.image];
    [_titleLb setText:model.title];
    if(isStepper){
        //计数器的显示
        [_stepper setValue:_photoIndex+1];
    }else{
         //滑块的显示
        [_slider setValue:_photoIndex+1];
    }  
}
@end

二、演示

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值