图片浏览器
一、代码
1.数据类 ImageModel.m
#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
#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
二、演示
![这里写图片描述](https://img-blog.csdn.net/20170117111540037?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWl3ZW5taW5nc2hpd28=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)