// ViewController.h // 03-综合使用 #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
// ViewController.m // 03-综合使用 #import "ViewController.h" #import "XMGShop.h" #import "XMGShopView.h" @interface ViewController () /** 存放所有商品的整体 */ @property (weak, nonatomic) IBOutlet UIView *shopsView; /** HUD */ @property (weak, nonatomic) IBOutlet UILabel *hud; // 文档注释 /** 添加按钮 */ @property (weak, nonatomic) UIButton *addBtn; /** 删除按钮 */ @property (weak, nonatomic) UIButton *removeBtn; /** 全部商品数据 */ @property (strong, nonatomic) NSArray *shops; @end @implementation ViewController // 加载plist数据(比较大) // 懒加载:用到时再去加载,而且也只加载一次 - (NSArray *)shops { if (_shops == nil) { // 加载一个字典数组 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"]]; NSMutableArray *shopArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { XMGShop *shop = [XMGShop shopWithDict:dict]; [shopArray addObject:shop]; } _shops = shopArray; } return _shops; } - (void)viewDidLoad { [super viewDidLoad]; self.shopsView.clipsToBounds = YES; // 添加“添加按钮” self.addBtn = [self addButtonWithImage:@"add" highImage:@"add_highlighted" disableImage:@"add_disabled" frame:CGRectMake(30, 30, 50, 50) action:@selector(add)]; // 添加“删除按钮” self.removeBtn = [self addButtonWithImage:@"remove" highImage:@"remove_highlighted" disableImage:@"remove_disabled" frame:CGRectMake(270, 30, 50, 50) action:@selector(remove)]; self.removeBtn.enabled = NO; // 加载xib文件 // Test.xib --编译--> Test.nib // 方式1 // NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"Test" owner:nil options:nil]; // [self.view addSubview:objs[1]]; // 方式2 // 一个UINib对象就代表一个xib文件 // UINib *nib = [UINib nibWithNibName:@"Test" bundle:[NSBundle mainBundle]]; // 一般情况下,bundle参数传nil,默认就是mainBundle // UINib *nib = [UINib nibWithNibName:@"Test" bundle:nil]; // NSArray *objs = [nib instantiateWithOwner:nil options:nil]; // [self.view addSubview:[objs lastObject]]; } #pragma mark 添加按钮 - (UIButton *)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage frame:(CGRect)frame action:(SEL)action { // 创建按钮 UIButton *btn = [[UIButton alloc] init]; // 设置背景图片 [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted]; [btn setBackgroundImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled]; // 设置位置和尺寸 btn.frame = frame; // 监听按钮点击 [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; // 添加按钮 [self.view addSubview:btn]; return btn; } #pragma mark 添加 - (void)add { // 每一个商品的尺寸 CGFloat shopW = 80; CGFloat shopH = 90; // 一行的列数 int cols = 3; // 每一列之间的间距 CGFloat colMargin = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1); // 每一行之间的间距 CGFloat rowMargin = 10; // 商品的索引 NSUInteger index = self.shopsView.subviews.count; // 创建一个父控件(整体:存放图片和文字) XMGShopView *shopView = [XMGShopView shopView]; shopView.shop = self.shops[index]; // 商品的x值 NSUInteger col = index % cols; CGFloat shopX = col * (shopW + colMargin); // 商品的y值 NSUInteger row = index / cols; CGFloat shopY = row * (shopH + rowMargin); shopView.frame = CGRectMake(shopX, shopY, shopW, shopH); // 添加控件 [self.shopsView addSubview:shopView]; // 控制按钮的可用性 [self checkState]; } #pragma mark 删除 - (void)remove { [[self.shopsView.subviews lastObject] removeFromSuperview]; // 控制按钮的可用性 [self checkState]; } #pragma mark 检查状态:按钮状态 - (void)checkState { // 删除按钮什么时候可以点击:商品个数 > 0 self.removeBtn.enabled = (self.shopsView.subviews.count > 0); // 添加按钮什么时候可以点击:商品个数 < 总数 self.addBtn.enabled = (self.shopsView.subviews.count < self.shops.count); // 显示HUD NSString *text = nil; if (self.removeBtn.enabled == NO) { // 删光了 text = @"已经全部删除"; } else if (self.addBtn.enabled == NO) { // 加满了 text = @"已经添加满了"; } if (text == nil) return; self.hud.text = text; self.hud.alpha = 1.0; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.hud.alpha = 0.0; }); } @end
// XMGShopView.h // 03-综合使用 #import <UIKit/UIKit.h> @class XMGShop; @interface XMGShopView : UIButton /** 模型数据 */ @property (nonatomic, strong) XMGShop *shop; + (instancetype)shopView; + (instancetype)shopViewWithShop:(XMGShop *)shop; @end
// XMGShopView.m // 03-综合使用 #import "XMGShopView.h" #import "XMGShop.h" @interface XMGShopView() @end @implementation XMGShopView + (instancetype)shopView { return [self shopViewWithShop:nil]; } + (instancetype)shopViewWithShop:(XMGShop *)shop { XMGShopView *shopView = [[self alloc] init]; shopView.backgroundColor = [UIColor redColor]; shopView.shop = shop; return shopView; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.titleLabel.textAlignment = NSTextAlignmentCenter; self.titleLabel.font = [UIFont systemFontOfSize:11]; } return self; } - (void)setShop:(XMGShop *)shop { _shop = shop; // 设置子控件的数据 if (shop.icon) { [self setImage:[UIImage imageNamed:shop.icon] forState:UIControlStateNormal]; } [self setTitle:shop.name forState:UIControlStateNormal]; // 不要直接拿出按钮内部的子控件,来修改文字、图片属性 // self.titleLabel.text = shop.name; // self.imageView.image = [UIImage imageNamed:@"shop.icon"]; } //- (CGRect)imageRectForContentRect:(CGRect)contentRect //{ // return CGRectMake(0, 0, contentRect.size.width, contentRect.size.height); //} // //- (CGRect)titleRectForContentRect:(CGRect)contentRect //{ // return CGRectMake(0, 30, 70, 30); //} - (void)layoutSubviews { [super layoutSubviews]; CGFloat buttonW = self.frame.size.width; CGFloat buttonH = self.frame.size.height; CGFloat imageH = buttonW - 10; self.imageView.frame = CGRectMake(0, 0, buttonW, imageH); self.titleLabel.frame = CGRectMake(0, imageH, buttonW, buttonH - imageH); } @end
// XMGShop.h // 03-综合使用 #import <Foundation/Foundation.h> @interface XMGShop : NSObject /** 商品名称 */ @property (nonatomic, strong) NSString *name; /** 图标 */ @property (nonatomic, strong) NSString *icon; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)shopWithDict:(NSDictionary *)dict; @end
// XMGShop.m // 03-综合使用 #import "XMGShop.h" @implementation XMGShop - (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { // self.name = dict[@"name"]; // self.icon = dict[@"icon"]; [self setValuesForKeysWithDictionary:dict]; } return self; } + (instancetype)shopWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } @end