标签:icon back add msu 删除按钮 font sys form undle
九空格雏形--
每一行的列数,行间距,列间距
%决定了列数,/决定了行数。->来计算每个格子的x和y的位置;
1 #import "ViewController.h" 2 3 @interface ViewController () 4 @property (weak, nonatomic) IBOutlet UIView *shopsView; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 13 [self addButtonWithImage:@"add" highImage:@"add_highlighted" disableImage:@"add_disabled"
frame:CGRectMake(30, 30, 50, 50) action:@selector(add)]; 14 [self addButtonWithImage:@"remove" highImage:@"remove_highlighted" disableImage:@"remove_disabled"
frame:CGRectMake(270, 30, 50, 50) action:@selector(remove)]; 15 } 16 17 #pragma mark 添加按钮 18 - (void)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage frame:(CGRect)frame action:(SEL)action 19 { 20 // 创建按钮 21 UIButton *btn = [[UIButton alloc] init]; 22 // 设置背景图片 23 [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; 24 [btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted]; 25 [btn setBackgroundImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled]; 26 // 设置位置和尺寸 27 btn.frame = frame; 28 // 监听按钮点击 29 [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 30 31 // 添加按钮 32 [self.view addSubview:btn]; 33 } 34 35 #pragma mark 添加 36 - (void)add 37 { 38 // self.shopsView.clipsToBounds = YES; 39 40 // 每一个商品的尺寸 41 CGFloat shopW = 50; 42 CGFloat shopH = 70; 43 44 // 一行的列数 45 int cols = 4; 46 47 // 每一列之间的间距 48 CGFloat colMargin = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1); 49 // 每一行之间的间距 50 CGFloat rowMargin = 10; 51 52 // 创建一个父控件(整体:存放图片和文字) 53 UIView *shopView = [[UIView alloc] init]; 54 shopView.backgroundColor = [UIColor redColor]; 55 56 // 商品的索引 57 NSUInteger index = self.shopsView.subviews.count; 58 59 // 商品的x值 60 NSUInteger col = index % cols; 61 CGFloat shopX = col * (shopW + colMargin); 62 63 // 商品的y值 64 NSUInteger row = index / cols; 65 CGFloat shopY = row * (shopH + rowMargin); 66 67 shopView.frame = CGRectMake(shopX, shopY, shopW, shopH); 68 [self.shopsView addSubview:shopView]; 69 70 // 添加图片 71 UIImageView *iconView = [[UIImageView alloc] init]; 72 iconView.image = [UIImage imageNamed:@"danjianbao"]; 73 iconView.frame = CGRectMake(0, 0, shopW, shopW); 74 iconView.backgroundColor = [UIColor blueColor]; 75 [shopView addSubview:iconView]; 76 77 // 添加文字 78 UILabel *label = [[UILabel alloc] init]; 79 label.text = @"单肩包"; 80 label.frame = CGRectMake(0, shopW, shopW, shopH - shopW); 81 label.font = [UIFont systemFontOfSize:11]; 82 label.textAlignment = NSTextAlignmentCenter; 83 [shopView addSubview:label]; 84 } 85 86 #pragma mark 删除 87 - (void)remove 88 { 89 NSLog(@"删除。。。。"); 90 } 91 92 @end
启用活的数据(存有字典的数组)
1 #import "ViewController.h" 2 3 @interface ViewController () 4 /** 存放所有商品的整体 */ 5 @property (weak, nonatomic) IBOutlet UIView *shopsView; 6 // 文档注释 7 /** 添加按钮 */ 8 @property (weak, nonatomic) UIButton *addBtn; 9 /** 删除按钮 */ 10 @property (weak, nonatomic) UIButton *removeBtn; 11 12 /** 全部商品数据 */ 13 @property (strong, nonatomic) NSArray *shops; 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 // 添加“添加按钮” 23 self.addBtn = [self addButtonWithImage:@"add" highImage:@"add_highlighted" disableImage:@"add_disabled"
frame:CGRectMake(30, 30, 50, 50) action:@selector(add)]; 24 25 // 添加“删除按钮” 26 self.removeBtn = [self addButtonWithImage:@"remove" highImage:@"remove_highlighted" disableImage:@"remove_disabled"
frame:CGRectMake(270, 30, 50, 50) action:@selector(remove)]; 27 self.removeBtn.enabled = NO; 28 29 30 // 数据 31 self.shops = @[ 32 @{ 33 @"icon" : @"danjianbao", 34 @"name" : @"单肩包" 35 }, 36 @{ 37 @"icon" : @"liantiaobao", 38 @"name" : @"链条包" 39 }, 40 @{ 41 @"icon" : @"qianbao", 42 @"name" : @"钱包" 43 }, 44 @{ 45 @"name" : @"手提包", 46 @"icon" : @"shoutibao.png" 47 }, 48 @{ 49 @"name" : @"双肩包", 50 @"icon" : @"shuangjianbao.png" 51 }, 52 @{ 53 @"name" : @"斜挎包", 54 @"icon" : @"xiekuabao.png" 55 } 56 ]; 57 } 58 59 #pragma mark 添加按钮 60 - (UIButton *)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage
frame:(CGRect)frame action:(SEL)action 61 { 62 // 创建按钮 63 UIButton *btn = [[UIButton alloc] init]; 64 // 设置背景图片 65 [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; 66 [btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted]; 67 [btn setBackgroundImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled]; 68 // 设置位置和尺寸 69 btn.frame = frame; 70 // 监听按钮点击 71 [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 72 // 添加按钮 73 [self.view addSubview:btn]; 74 return btn; 75 } 76 77 #pragma mark 添加 78 - (void)add 79 { 80 // self.shopsView.clipsToBounds = YES; 81 82 // 每一个商品的尺寸 83 CGFloat shopW = 80; 84 CGFloat shopH = 90; 85 86 // 一行的列数 87 int cols = 3; 88 89 // 每一列之间的间距 90 CGFloat colMargin = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1); 91 // 每一行之间的间距 92 CGFloat rowMargin = 10; 93 94 // 创建一个父控件(整体:存放图片和文字) 95 UIView *shopView = [[UIView alloc] init]; 96 shopView.backgroundColor = [UIColor redColor]; 97 98 // 商品的索引 99 NSUInteger index = self.shopsView.subviews.count; 100 101 // 商品的x值 102 NSUInteger col = index % cols; 103 CGFloat shopX = col * (shopW + colMargin); 104 105 // 商品的y值 106 NSUInteger row = index / cols; 107 CGFloat shopY = row * (shopH + rowMargin); 108 109 shopView.frame = CGRectMake(shopX, shopY, shopW, shopH); 110 [self.shopsView addSubview:shopView]; 111 112 // 获得index位置对应的商品数据 113 NSDictionary *shop = self.shops[index]; 114 115 // 添加图片 116 UIImageView *iconView = [[UIImageView alloc] init]; 117 iconView.image = [UIImage imageNamed:shop[@"icon"]]; 118 iconView.frame = CGRectMake(0, 0, shopW, shopW); 119 iconView.backgroundColor = [UIColor blueColor]; 120 [shopView addSubview:iconView]; 121 122 // 添加文字 123 UILabel *label = [[UILabel alloc] init]; 124 label.text = shop[@"name"]; 125 label.frame = CGRectMake(0, shopW, shopW, shopH - shopW); 126 label.font = [UIFont systemFontOfSize:11]; 127 label.textAlignment = NSTextAlignmentCenter; 128 [shopView addSubview:label]; 129 130 // 控制按钮的可用性 131 [self checkState]; 132 133 // 设置“添加按钮”能不能点击 134 // if (self.shopsView.subviews.count == shops.count) { 135 // // 添加满了 136 // self.addBtn.enabled = NO; 137 // } 138 // 139 // self.removeBtn.enabled = YES; 140 } 141 142 #pragma mark 删除 143 - (void)remove 144 { 145 [[self.shopsView.subviews lastObject] removeFromSuperview]; 146 147 // 控制按钮的可用性 148 [self checkState]; 149 150 // 设置“删除按钮”能不能点击 151 // if (self.shopsView.subviews.count == 0) { 152 // // 全部被删掉了 153 // self.removeBtn.enabled = NO; 154 // } 155 // 156 // self.addBtn.enabled = YES; 157 } 158 159 #pragma mark 检查状态:按钮状态 160 - (void)checkState 161 { 162 // if (self.shopsView.subviews.count == 0) { 163 // self.removeBtn.enabled = NO; 164 // } else { 165 // self.removeBtn.enabled = YES; 166 // } 167 // 168 // 删除按钮什么时候可以点击:商品个数 > 0 169 self.removeBtn.enabled = (self.shopsView.subviews.count > 0); 170 171 // if (self.shopsView.subviews.count == self.shops.count) { 172 // self.addBtn.enabled = NO; 173 // } else { 174 // self.addBtn.enabled = YES; 175 // } 176 // 添加按钮什么时候可以点击:商品个数 < 总数 177 self.addBtn.enabled = (self.shopsView.subviews.count < self.shops.count); 178 } 179 180 @end
添加指示器,定时器方法
1 #import "ViewController.h" 2 3 @interface ViewController () 4 /** 存放所有商品的整体 */ 5 @property (weak, nonatomic) IBOutlet UIView *shopsView; 6 7 /** HUD */ 8 @property (weak, nonatomic) IBOutlet UILabel *hud; 9 10 // 文档注释 11 /** 添加按钮 */ 12 @property (weak, nonatomic) UIButton *addBtn; 13 /** 删除按钮 */ 14 @property (weak, nonatomic) UIButton *removeBtn; 15 16 /** 全部商品数据 */ 17 @property (strong, nonatomic) NSArray *shops; 18 @end 19 20 @implementation ViewController 21 22 - (void)viewDidLoad 23 { 24 [super viewDidLoad]; 25 26 // 添加“添加按钮” 27 self.addBtn = [self addButtonWithImage:@"add" highImage:@"add_highlighted" disableImage:@"add_disabled"
frame:CGRectMake(30, 30, 50, 50) action:@selector(add)]; 28 29 // 添加“删除按钮” 30 self.removeBtn = [self addButtonWithImage:@"remove" highImage:@"remove_highlighted" disableImage:@"remove_disabled"
frame:CGRectMake(270, 30, 50, 50) action:@selector(remove)]; 31 self.removeBtn.enabled = NO; 32 33 34 // 数据 35 self.shops = @[ 36 @{ 37 @"icon" : @"danjianbao", 38 @"name" : @"单肩包" 39 }, 40 @{ 41 @"icon" : @"liantiaobao", 42 @"name" : @"链条包" 43 }, 44 @{ 45 @"icon" : @"qianbao", 46 @"name" : @"钱包" 47 }, 48 @{ 49 @"name" : @"手提包", 50 @"icon" : @"shoutibao.png" 51 }, 52 @{ 53 @"name" : @"双肩包", 54 @"icon" : @"shuangjianbao.png" 55 }, 56 @{ 57 @"name" : @"斜挎包", 58 @"icon" : @"xiekuabao.png" 59 } 60 ]; 61 } 62 63 #pragma mark 添加按钮 64 - (UIButton *)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage
frame:(CGRect)frame action:(SEL)action 65 { 66 // 创建按钮 67 UIButton *btn = [[UIButton alloc] init]; 68 // 设置背景图片 69 [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; 70 [btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted]; 71 [btn setBackgroundImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled]; 72 // 设置位置和尺寸 73 btn.frame = frame; 74 // 监听按钮点击 75 [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 76 // 添加按钮 77 [self.view addSubview:btn]; 78 return btn; 79 } 80 81 #pragma mark 添加 82 - (void)add 83 { 84 // self.shopsView.clipsToBounds = YES; 85 86 // 每一个商品的尺寸 87 CGFloat shopW = 80; 88 CGFloat shopH = 90; 89 90 // 一行的列数 91 int cols = 3; 92 93 // 每一列之间的间距 94 CGFloat colMargin = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1); 95 // 每一行之间的间距 96 CGFloat rowMargin = 10; 97 98 // 创建一个父控件(整体:存放图片和文字) 99 UIView *shopView = [[UIView alloc] init]; 100 shopView.backgroundColor = [UIColor redColor]; 101 102 // 商品的索引 103 NSUInteger index = self.shopsView.subviews.count; 104 105 // 商品的x值 106 NSUInteger col = index % cols; 107 CGFloat shopX = col * (shopW + colMargin); 108 109 // 商品的y值 110 NSUInteger row = index / cols; 111 CGFloat shopY = row * (shopH + rowMargin); 112 113 shopView.frame = CGRectMake(shopX, shopY, shopW, shopH); 114 [self.shopsView addSubview:shopView]; 115 116 // 获得index位置对应的商品数据 117 NSDictionary *shop = self.shops[index]; 118 119 // 添加图片 120 UIImageView *iconView = [[UIImageView alloc] init]; 121 iconView.image = [UIImage imageNamed:shop[@"icon"]]; 122 iconView.frame = CGRectMake(0, 0, shopW, shopW); 123 iconView.backgroundColor = [UIColor blueColor]; 124 [shopView addSubview:iconView]; 125 126 // 添加文字 127 UILabel *label = [[UILabel alloc] init]; 128 label.text = shop[@"name"]; 129 label.frame = CGRectMake(0, shopW, shopW, shopH - shopW); 130 label.font = [UIFont systemFontOfSize:11]; 131 label.textAlignment = NSTextAlignmentCenter; 132 [shopView addSubview:label]; 133 134 // 控制按钮的可用性 135 [self checkState]; 136 } 137 138 #pragma mark 删除 139 - (void)remove 140 { 141 [[self.shopsView.subviews lastObject] removeFromSuperview]; 142 143 // 控制按钮的可用性 144 [self checkState]; 145 } 146 147 #pragma mark 检查状态:按钮状态 148 - (void)checkState 149 { 150 // 删除按钮什么时候可以点击:商品个数 > 0 151 self.removeBtn.enabled = (self.shopsView.subviews.count > 0); 152 // 添加按钮什么时候可以点击:商品个数 < 总数 153 self.addBtn.enabled = (self.shopsView.subviews.count < self.shops.count); 154 155 // 显示HUD 156 NSString *text = nil; 157 if (self.removeBtn.enabled == NO) { // 删光了 158 text = @"已经全部删除"; 159 } else if (self.addBtn.enabled == NO) { // 加满了 160 text = @"已经添加满了"; 161 } 162 if (text == nil) return; 163 164 self.hud.text = text; 165 self.hud.alpha = 1.0; 166 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 167 self.hud.alpha = 0.0; 168 }); 169 } 170 171 #pragma mark 隐藏HUD 172 //- (void)hideHUD 173 //{ 174 // self.hud.alpha = 0.0; 175 //} 177 // HUD 178 // 指示器 179 // 蒙板 180 // 遮盖 181 182 183 // 定时任务 184 // SEL:对方法的包装, 使用@selector(方法名)包装一个SEL数据 185 // 2.0s以后会自动调用self的hidHUD方法 186 // [self performSelector:@selector(hideHUD) withObject:nil afterDelay:1.5]; 187 // [self performSelector:@selector(hideHUD) withObject:nil afterDelay:1.5]; 188 // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 189 // self.hud.alpha = 0.0; 190 // }); 191 // [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideHUD) userInfo:nil repeats:NO]; 192 @end
加载plist数据
1 #import "ViewController.h" 2 3 @interface ViewController () 4 /** 存放所有商品的整体 */ 5 @property (weak, nonatomic) IBOutlet UIView *shopsView; 18 @end 19 20 @implementation ViewController 21 22 - (void)viewDidLoad 23 { 24 [super viewDidLoad]; 25 26 // 添加“添加按钮” 27 self.addBtn = /*****/ 29 // 添加“删除按钮” 30 self.removeBtn = /***/ 31 self.removeBtn.enabled = NO; 32 33 // 加载plist数据 34 35 // 一个NSBundle对象对应一个资源包(图片、音频、视频、plis等文件) 36 // NSBundle的作用:用来访问与之对应的资源包内部的文件,可以用来获得文件的全路径 37 // 项目中添加的资源都会被添加到主资源包中 38 // [NSBundle mainBundle]关联的就是项目的主资源包 39 NSBundle *bundle = [NSBundle mainBundle]; 40 41 // 利用mainBundle获得plist文件在主资源包中的全路径 42 NSString *file = [bundle pathForResource:@"shops" ofType:@"plist"]; 43 // NSString *file = [bundle pathForResource:@"shops.plist" ofType:nil]; 44 45 // 凡是参数名为File,传递的都是文件的全路径 46 self.shops = [NSArray arrayWithContentsOfFile:file]; 47 } 48 49 #pragma mark 添加按钮 50 - (UIButton *)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage
frame:(CGRect)frame action:(SEL)action 51 { 52 .... 65 } 66 67 #pragma mark 添加 68 - (void)add 69 { 70 .....122 } 123 124 #pragma mark 删除 125 - (void)remove 126 { 127 ...131 } 132 133 #pragma mark 检查状态:按钮状态 134 - (void)checkState 135 { 136 ...155 } 156 157 @end
加载plist数据(比较大)->懒加载
懒加载:用到时再去加载,而且也只加载一次
1 1 #import "ViewController.h" 2 2 3 3 @interface ViewController () 4 4 ... 18 @end 5 19 6 20 @implementation ViewController 7 21 8 22 // 加载plist数据(比较大) 9 23 // 懒加载:用到时再去加载,而且也只加载一次 10 24 - (NSArray *)shops //重写setter方法 11 25 { 12 26 if (_shops == nil) { 13 27 NSString *file = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"]; 14 28 self.shops = [NSArray arrayWithContentsOfFile:file]; 15 29 // _shops = [NSArray arrayWithContentsOfFile:file]; 16 30 // [self setShops:[NSArray arrayWithContentsOfFile:file]]; 17 31 } 18 32 return _shops; 19 33 }
标签:icon back add msu 删除按钮 font sys form undle
原文地址:http://www.cnblogs.com/laugh/p/6387037.html