首页 > 其他 > 详细

源码-03-封装

时间:2017-02-10 20:19:13      阅读:152      评论:0      收藏:0      [点我收藏+]

封装

  1 #import "ViewController.h"
  2 #import "XMGShop.h"
  3 
  4 @interface ViewController ()
  5 /** 存放所有商品的整体 */
  6 @property (weak, nonatomic) IBOutlet UIView *shopsView;
  7 
  8 /** HUD */
  9 @property (weak, nonatomic) IBOutlet UILabel *hud;
 10 
 11 // 文档注释
 12 /** 添加按钮 */
 13 @property (weak, nonatomic) UIButton *addBtn;
 14 /** 删除按钮 */
 15 @property (weak, nonatomic) UIButton *removeBtn;
 16 
 17 /** 全部商品数据 */
 18 @property (strong, nonatomic) NSArray *shops;
 19 @end
 20 
 21 @implementation ViewController
 22 
 23 // 加载plist数据(比较大)
 24 // 懒加载:用到时再去加载,而且也只加载一次
 25 - (NSArray *)shops
 26 {
 27     if (_shops == nil) {
 28         // 加载一个字典数组
 29         NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"]];
 30         
 31         NSMutableArray *shopArray = [NSMutableArray array];
 32         for (NSDictionary *dict in dictArray) {
 33             XMGShop *shop = [XMGShop shopWithDict:dict];
 34             [shopArray addObject:shop];
 35         }
 36         _shops = shopArray;
 37     }
 38     return _shops;
 39 }
 40 
 41 - (void)viewDidLoad
 42 {
 43     [super viewDidLoad];
 44     
 45     // 添加“添加按钮”
 46     self.addBtn = 。。。 47     
 48     // 添加“删除按钮”
 49     self.removeBtn = 。。。 50     self.removeBtn.enabled = NO;
 51 }
 52 
 53 #pragma mark 添加按钮
 54 - (UIButton *)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage 
                  frame:(CGRect)frame action:(SEL)action
55 { 56 // 创建按钮 57 UIButton *btn = [[UIButton alloc] init]; 58 // 设置背景图片 59 [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; 60 [btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted]; 61 [btn setBackgroundImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled]; 62 // 设置位置和尺寸 63 btn.frame = frame; 64 // 监听按钮点击 65 [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 66 // 添加按钮 67 [self.view addSubview:btn]; 68 return btn; 69 } 70 71 #pragma mark 添加 72 - (void)add 73 { 74 75 CGFloat shopW = 80; // 每一个商品的尺寸 76 CGFloat shopH = 90; 77 78 int cols = 3; // 一行的列数 79 80 CGFloat colMargin = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1); // 每一列之间的间距 81 CGFloat rowMargin = 10; // 每一行之间的间距 82 83 UIView *shopView = [[UIView alloc] init]; // 创建一个父控件(整体:存放图片和文字) 84 shopView.backgroundColor = [UIColor redColor]; 85 86 NSUInteger index = self.shopsView.subviews.count; // 商品的索引 87 88 NSUInteger col = index % cols; // 商品的x值 89 CGFloat shopX = col * (shopW + colMargin); 90 91 NSUInteger row = index / cols; // 商品的y值 92 CGFloat shopY = row * (shopH + rowMargin); 93 94 shopView.frame = CGRectMake(shopX, shopY, shopW, shopH); 95 [self.shopsView addSubview:shopView]; 96 97 // 获得index位置对应的商品数据 98 // NSDictionary *dict = self.shops[index]; 99 // Shop *shop = [Shop shopWithDict:dict]; 100 101 XMGShop *shop = self.shops[index]; 102 103 // 添加图片 104 UIImageView *iconView = [[UIImageView alloc] init]; 105 iconView.image = [UIImage imageNamed:shop.icon]; 106 iconView.frame = CGRectMake(0, 0, shopW, shopW); 107 iconView.backgroundColor = [UIColor blueColor]; 108 [shopView addSubview:iconView]; 109 110 // 添加文字 111 UILabel *label = [[UILabel alloc] init]; 112 label.text = shop.name; 113 label.frame = CGRectMake(0, shopW, shopW, shopH - shopW); 114 label.font = [UIFont systemFontOfSize:11]; 115 label.textAlignment = NSTextAlignmentCenter; 116 [shopView addSubview:label]; 117 118 // 控制按钮的可用性 119 [self checkState]; 120 } 121 122 #pragma mark 删除 123 - (void)remove 124 { 125 [[self.shopsView.subviews lastObject] removeFromSuperview]; 126 127 // 控制按钮的可用性 128 [self checkState]; 129 } 130 131 #pragma mark 检查状态:按钮状态 132 - (void)checkState 133 { 134 // 删除按钮什么时候可以点击:商品个数 > 0 135 self.removeBtn.enabled = (self.shopsView.subviews.count > 0); 136 // 添加按钮什么时候可以点击:商品个数 < 总数 137 self.addBtn.enabled = (self.shopsView.subviews.count < self.shops.count); 138 139 // 显示HUD 140 NSString *text = nil; 141 if (self.removeBtn.enabled == NO) { // 删光了 142 text = @"已经全部删除"; 143 } else if (self.addBtn.enabled == NO) { // 加满了 144 text = @"已经添加满了"; 145 } 146 if (text == nil) return; 147 148 self.hud.text = text; 149 self.hud.alpha = 1.0; 150 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 151 self.hud.alpha = 0.0; 152 }); 153 } 154 155 @end

 

 shops.h

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface XMGShop : NSObject
 4 /** 商品名称 */
 5 @property (nonatomic, strong) NSString *name;
 6 /** 图标 */
 7 @property (nonatomic, strong) NSString *icon;
 8 
 9 - (instancetype)initWithDict:(NSDictionary *)dict;
10 + (instancetype)shopWithDict:(NSDictionary *)dict;
11 @end

shops.m

 1 #import "XMGShop.h"
 2 
 3 @implementation XMGShop
 4 - (instancetype)initWithDict:(NSDictionary *)dict
 5 {
 6     if (self = [super init]) {
 7         self.name = dict[@"name"];
 8         self.icon = dict[@"icon"];
 9     }
10     return self;
11 }
12 
13 + (instancetype)shopWithDict:(NSDictionary *)dict
14 {
15     return [[self alloc] initWithDict:dict];
16 }
17 @end

 

源码-03-封装

原文:http://www.cnblogs.com/laugh/p/6387518.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!