这是一个图片浏览的程序,是传智公开课的一个demo
代码如下
// // ViewController.m // ImageBroswer // // Created by xin on 15-3-12. // Copyright (c) 2015年 Jackey. All rights reserved. // #import "ViewController.h" @interface ViewController () //序号标签 @property (nonatomic,strong) UILabel *noLabel; //图片 @property (nonatomic,strong) UIImageView *icon; //图片描述 @property (nonatomic,strong) UILabel *descriptionLabel; //左边按钮 @property (nonatomic,strong) UIButton *leftButton; //右边按钮 @property (nonatomic,strong) UIButton *rightButton; //image index @property (nonatomic,assign) int index;//不带星号 assign @property (nonatomic,strong) NSArray *array; //@property // 1 setter getter // 2 带下划线的成员变量 @end @implementation ViewController //array getter -(NSArray *)array{ //只有第一次调用getter的时候才会执行 //其他时候直接返回 if(_array==nil){ NSDictionary *dic1 = @{@"name":@"biaoqingdi",@"desc":@"表情"}; NSDictionary *dic2 = @{@"name":@"bingli",@"desc":@"病例"}; NSDictionary *dic3 = @{@"name":@"chiniupa",@"desc":@"吃牛扒"}; NSDictionary *dic4 = @{@"name":@"danteng",@"desc":@"蛋疼"}; NSDictionary *dic5 = @{@"name":@"wangba",@"desc":@"王八"}; _array = @[dic1,dic2,dic3,dic4,dic5]; } return _array; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //标签描述 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 320, 40)]; //label.text = @"1/5"; label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label]; self.noLabel = label; //图片 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 70, 200, 200)]; //imageView.image = [UIImage imageNamed:@"biaoqingdi"]; [self.view addSubview:imageView]; self.icon = imageView; //图片描述 UILabel *iconLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 300, 320, 80)]; //iconLabel.text = @"什么表情都弱爆了"; iconLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:iconLabel]; self.descriptionLabel = iconLabel; //left icon UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; leftBtn.center = CGPointMake(self.icon.frame.origin.x/2, self.icon.center.y); [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; [self.view addSubview:leftBtn]; [leftBtn addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside]; self.leftButton = leftBtn; //right btn UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; rightBtn.center = CGPointMake(self.view.frame.size.width- self.icon.frame.origin.x/2, self.icon.center.y); [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; [self.view addSubview:rightBtn]; [rightBtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside]; self.rightButton = rightBtn; [self changeImage]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** 改变图像数据 */ -(void)changeImage{ self.noLabel.text = [NSString stringWithFormat:@"%d/%d",self.index+1,5]; self.icon.image = [UIImage imageNamed:self.array[self.index][@"name"]]; self.descriptionLabel.text = self.array[self.index][@"desc"]; self.rightButton.enabled = (self.index !=4); self.leftButton.enabled = (self.index!=0); } /** 左边按钮 */ -(void)leftClick{ NSLog(@"left"); self.index--; [self changeImage]; } -(void)rightClick{ NSLog(@"right"); self.index++; [self changeImage]; } @end
优化代码,使用懒加载
// // ViewController.m // ImageBroswer // // Created by xin on 15-3-12. // Copyright (c) 2015年 Jackey. All rights reserved. // #import "ViewController.h" @interface ViewController () //序号标签 @property (nonatomic,strong) UILabel *noLabel; //图片 @property (nonatomic,strong) UIImageView *icon; //图片描述 @property (nonatomic,strong) UILabel *descriptionLabel; //左边按钮 @property (nonatomic,strong) UIButton *leftButton; //右边按钮 @property (nonatomic,strong) UIButton *rightButton; //image index @property (nonatomic,assign) int index;//不带星号 assign @property (nonatomic,strong) NSArray *array; //@property // 1 setter getter // 2 带下划线的成员变量 @end @implementation ViewController //array getter -(NSArray *)array{ //只有第一次调用getter的时候才会执行 //其他时候直接返回 if(_array==nil){ NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageData" ofType:@"plist"]; _array= [NSArray arrayWithContentsOfFile:path]; } return _array; } -(UILabel *)noLabel{ if(_noLabel==nil){ _noLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 320, 40)]; _noLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_noLabel]; } return _noLabel; } -(UIImageView *)icon{ if(_icon==nil){ _icon = [[UIImageView alloc]initWithFrame:CGRectMake(60, 70, 200, 200)]; [self.view addSubview:_icon]; } return _icon; } -(UILabel *)descriptionLabel{ if(_descriptionLabel==nil){ _descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 300, 320, 80)]; _descriptionLabel.numberOfLines = 0; _descriptionLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_descriptionLabel]; } return _descriptionLabel; } -(UIButton *)leftButton{ if(_leftButton==nil){ _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; _leftButton.center = CGPointMake(self.icon.frame.origin.x/2, self.icon.center.y); [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; [self.view addSubview:_leftButton]; [_leftButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside]; } return _leftButton; } -(UIButton *)rightButton{ if(!_rightButton){ _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; _rightButton.center = CGPointMake(self.view.frame.size.width- self.icon.frame.origin.x/2, self.icon.center.y); [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; [self.view addSubview:_rightButton]; [_rightButton addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside]; } return _rightButton; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self changeImage]; } /** 改变图像数据 */ -(void)changeImage{ [self.noLabel setText:[NSString stringWithFormat:@"%d/%d",self.index+1,5]]; [self.icon setImage:[UIImage imageNamed:self.array[self.index][@"name"]]]; [self.descriptionLabel setText:self.array[self.index][@"desc"]]; [self.rightButton setEnabled:(self.index!=4)]; [self.leftButton setEnabled:(self.index!=0)]; } /** 左边按钮 */ -(void)leftClick{ NSLog(@"left"); self.index--; [self changeImage]; } -(void)rightClick{ NSLog(@"right"); self.index++; [self changeImage]; } @end