一. 概述数组
1 #import "ViewController.h" 2 3 //用于查找字典防止拼写错误 4 #define HGImgKey @"name" 5 #define HGImgDes @"desc" 6 7 @interface ViewController () 8 /** 图片序号 */ 9 @property (weak, nonatomic) IBOutlet UILabel *iconNum; 10 /** 图片描述 */ 11 @property (weak, nonatomic) IBOutlet UILabel *iconDesc; 12 /** 图片数据 */ 13 @property (weak, nonatomic) IBOutlet UIImageView *iconView; 14 /** 上一张图片按钮 用于设置enable状态 */ 15 @property (weak, nonatomic) IBOutlet UIButton *prevBtn; 16 /** 下一张图片按钮 用于设置enable状态 */ 17 @property (weak, nonatomic) IBOutlet UIButton *nextBtn; 18 /** 设置按钮 弹出的设置框 */ 19 @property (weak, nonatomic) IBOutlet UIView *setView; 20 21 /** 记录当前图片索引即序号 */ 22 @property (nonatomic,assign) int iconIndex; 23 24 /** 图片数据结合 让控制器拥有数据并使用懒加载方式加载数据 */ 25 @property (nonatomic,strong)NSArray* imageData; 26 /** 设置按钮监听事件 */ 27 - (IBAction)setting:(UIButton *)sender; 28 /** 缩放监听事件 */ 29 - (IBAction)changeSize:(UISlider *)sender; 30 /** 模式选择开关监听事件 */ 31 - (IBAction)changeMode:(UISwitch *)sender; 32 33 /** previous 按钮监听事件 */ 34 - (IBAction)previous:(UIButton *)sender; 35 /** next 按钮监听事件 */ 36 - (IBAction)next:(UIButton *)sender; 37 38 @end
2)懒加载的方式建立并初始化数据浏览器
1 /** 2 * 懒加载的实现方式,重写get方法,用到时才建立并初始化数据 3 * 4 * @return _imageData 完成初始化的数据 5 */ 6 - (NSArray *)imageData1 7 { 8 // 判空只需在第一次使用时加载 9 if(_imageData == nil) 10 { 11 //建立字典 12 // 先来一个空的 13 NSMutableDictionary *img1 = [NSMutableDictionary dictionary]; 14 // 以后使用添加的方法添加key和val 15 img1[HGImgKey] = @"iq"; 16 img1[HGImgDes] = @"你是在挑战小偷的智商吗"; 17 18 // 先来一个空的 19 NSMutableDictionary *img2 = [NSMutableDictionary dictionary]; 20 // 以后使用添加的方法添加key和val 21 img2[HGImgKey] = @"caifang"; 22 img2[HGImgDes] = @"哪家电视台这么坑爹"; 23 24 // 先来一个空的 25 NSMutableDictionary *img3 = [NSMutableDictionary dictionary]; 26 // 以后使用添加的方法添加key和val 27 img3[HGImgKey] = @"niupai"; 28 img3[HGImgDes] = @"吃个牛排堪比杀牛"; 29 30 // 先来一个空的 31 NSMutableDictionary *img4 = [NSMutableDictionary dictionary]; 32 // 以后使用添加的方法添加key和val 33 img4[HGImgKey] = @"biaoqingdi"; 34 img4[HGImgDes] = @"在他面前,什么表情都弱爆了"; 35 36 // 先来一个空的 37 NSMutableDictionary *img5 = [NSMutableDictionary dictionary]; 38 // 以后使用添加的方法添加key和val 39 img5[HGImgKey] = @"can"; 40 img5[HGImgDes] = @"一个字惨!"; 41 42 // 先来一个空的 43 NSMutableDictionary *img6 = [NSMutableDictionary dictionary]; 44 // 以后使用添加的方法添加key和val 45 img6[HGImgKey] = @"sb"; 46 img6[HGImgDes] = @"非要跟本身过不去"; 47 48 /* 将字典加入数组 */ 49 // 快速建立数组的方法 50 _imageData = @[img1,img2,img3,img4,img5,img6]; 51 52 } 53 return _imageData; 54 }
2.前、后按钮的监听事件:因为两个按钮均须要改变显示数据,故将公共的代码部分提出到changeData方法中,只在各自的处理中操做index便可ide
1 - (IBAction)previous:(UIButton *)sender 2 { 3 // 序号减一 4 _iconIndex--; 5 // 改变显示的数据并设置按钮状态 6 [self changeData]; 7 } 8 9 - (IBAction)next:(UIButton *)sender 10 { 11 // 序号加1 12 _iconIndex++; 13 [self changeData]; 14 }
changeData方法实现:动画
1 - (void)changeData 2 { 3 // 从字典中取出当前索引对应的数据 4 NSDictionary *dict = self.imageData[self.iconIndex]; 5 6 /* 设置序号 */ 7 NSString *num = [NSString stringWithFormat:@"%d/%d",_iconIndex + 1,(int)self.imageData.count]; 8 _iconNum.text = num; 9 10 /* 设置数据 */ 11 _iconView.image = [UIImage imageNamed:dict[HGImgKey]]; 12 13 /* 设置描述 */ 14 _iconDesc.text = dict[HGImgDes]; 15 16 // 设置按钮状态 17 _prevBtn.enabled = _iconIndex > 0; 18 // 不可写死 19 //_nextBtn.enabled = _iconIndex < 5; 20 _nextBtn.enabled = _iconIndex < self.imageData.count -1; 21 }
3.设置按钮的监听事件atom
1 - (IBAction)setting:(UIButton *)sender { 2 3 NSLog(@"seting"); 4 5 // 添加动画 6 [UIView animateWithDuration:1.0 animations:^{ 7 CGRect fram = _setView.frame; 8 if (fram.origin.y == self.view.frame.size.height) 9 { 10 fram.origin.y -= _setView.bounds.size.height; 11 } 12 else 13 { 14 fram.origin.y += _setView.bounds.size.height; 15 } 16 _setView.frame = fram; 17 }]; 18 }
4.图片的缩放:缩放使用的是UISlider控件值设为0-1默认为最大,只能比初始状态小。spa
1 - (IBAction)changeSize:(UISlider *)sender { 2 3 NSLog(@"changeSize"); 4 // 缩放图片 5 // 获取当前的value 6 CGFloat value = sender.value; 7 //改变图片的transform属性 8 _iconView.transform = CGAffineTransformMakeScale(value, value); 9 }
1 @property(nonatomic) NSInteger numberOfLines;
1. UIlabel控件显示文字默认是左对齐,要想设置的居中,在storybord中的设置见上图中alignment选项,对应的代码为:code
@property(nonatomic) NSTextAlignment textAlignment; // default is NSTextAlignmentLeft
可设置为以下三个值:orm
NSTextAlignmentLeft = 0, // 左对齐 NSTextAlignmentCenter = 1, // 居中 NSTextAlignmentRight = 2, // 右对齐