#import "ViewController.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 - (void)viewDidLoad { [super viewDidLoad]; // 添加「添加按鈕」 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; // 加載plist數據 // 一個NSBundle對象對應一個資源包(圖片、音頻、視頻、plis等文件) // NSBundle的做用:用來訪問與之對應的資源包內部的文件,能夠用來得到文件的全路徑 // 項目中添加的資源都會被添加到主資源包中 // [NSBundle mainBundle]關聯的就是項目的主資源包 NSBundle *bundle = [NSBundle mainBundle]; // 利用mainBundle得到plist文件在主資源包中的全路徑 NSString *file = [bundle pathForResource:@"shops" ofType:@"plist"]; // NSString *file = [bundle pathForResource:@"shops.plist" ofType:nil]; // 凡是參數名爲File,傳遞的都是文件的全路徑 self.shops = [NSArray arrayWithContentsOfFile:file]; } #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 { // self.shopsView.clipsToBounds = YES; // 每個商品的尺寸 CGFloat shopW = 80; CGFloat shopH = 90; // 一行的列數 int cols = 3; // 每一列之間的間距 CGFloat colMargin = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1); // 每一行之間的間距 CGFloat rowMargin = 10; // 建立一個父控件(總體:存放圖片和文字) UIView *shopView = [[UIView alloc] init]; shopView.backgroundColor = [UIColor redColor]; // 商品的索引 NSUInteger index = self.shopsView.subviews.count; // 商品的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]; // 得到index位置對應的商品數據 NSDictionary *shop = self.shops[index]; // 添加圖片 UIImageView *iconView = [[UIImageView alloc] init]; iconView.image = [UIImage imageNamed:shop[@"icon"]]; iconView.frame = CGRectMake(0, 0, shopW, shopW); iconView.backgroundColor = [UIColor blueColor]; [shopView addSubview:iconView]; // 添加文字 UILabel *label = [[UILabel alloc] init]; label.text = shop[@"name"]; label.frame = CGRectMake(0, shopW, shopW, shopH - shopW); label.font = [UIFont systemFontOfSize:11]; label.textAlignment = NSTextAlignmentCenter; [shopView addSubview:label]; // 控制按鈕的可用性 [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