14. 综合练习 按钮状态/抽取方法/HUD/定时器

按钮的状态使用

抽取封装

HUD/指示器/蒙板/遮盖

定时器

  • 写代码的时候可以用return 不用{ }
  • 文档注释 /** */, 方法的封装
//
//  ViewController.m
//  Created by xiaofei on 15/5/25.
//  Copyright (c) 2015年 xiaofei. All rights reserved.
//

#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];
    
    // 添加“添加按钮” 方法的封装 注意Action的参数类型
    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;
    
    
    // 数据
    self.shops = @[
                       @{
                           @"icon" : @"danjianbao",
                           @"name" : @"单肩包"
                           },
                       @{
                           @"icon" : @"liantiaobao",
                           @"name" : @"链条包"
                           },
                       @{
                           @"icon" : @"qianbao",
                           @"name" : @"钱包"
                           },
                       @{
                           @"name" : @"手提包",
                           @"icon" : @"shoutibao.png"
                           },
                       @{
                           @"name" : @"双肩包",
                           @"icon" : @"shuangjianbao.png"
                           },
                       @{
                           @"name" : @"斜挎包",
                           @"icon" : @"xiekuabao.png"
                           }
                       ];
}

#pragma mark 添加按钮  sel是参数类型,返回的类型可以做特殊处理
- (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; // 创建一个父控件(整体:存放图片和文字) 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 = @"已经添加满了"; }
//写代码的时候可以用return 不用 {} 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; }); } #pragma mark 隐藏HUD //- (void)hideHUD //{ // self.hud.alpha = 0.0; //} // 定时任务 // SEL:对方法的包装, 使用@selector(方法名)包装一个SEL数据 // 2.0s以后会自动调用self的hidHUD方法 // [self performSelector:@selector(hideHUD) withObject:nil afterDelay:1.5]; // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // self.hud.alpha = 0.0; // }); // [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideHUD) userInfo:nil repeats:NO]; @end

把复杂的代码简洁化:

/    if (self.shopsView.subviews.count == 0) {
//        self.removeBtn.enabled = NO;
//    } else {
//        self.removeBtn.enabled = YES;
//    }
//
    // 删除按钮什么时候可以点击:商品个数 > 0
    self.removeBtn.enabled = (self.shopsView.subviews.count > 0);
    
//    if (self.shopsView.subviews.count == self.shops.count) {
//        self.addBtn.enabled = NO;
//    } else {
//        self.addBtn.enabled = YES;
//    }
    // 添加按钮什么时候可以点击:商品个数 < 总数
    self.addBtn.enabled = (self.shopsView.subviews.count < self.shops.count);

 

posted @ 2016-09-04 13:23  <瑾瑜>  阅读(113)  评论(0)    收藏  举报