添加九宫格商品步骤

来源:互联网 发布:淮南市大数据办公室 编辑:程序博客网 时间:2024/06/04 19:03

九宫格代码步骤

  1. 在View加载完毕后(viewDidLoad())创建“添加”和“删除”按钮,用函数表达出来。
  2. 按钮的属性:按钮函数中需要添加按钮的各个属性:按钮在不同状态下的图片,按钮的大小位置,监听按钮,最后在想要创建按钮的View中创建按钮。
  3. 添加按钮的动作(九宫格):
    • 添加含有图片和文字整体的父控件(其中包含父控件的位置和大小)
    • 分别添加父控件中图片和文字
    • 检查按钮的可用性
  4. 删除按钮的动作:
    • 删除最后添加的按钮
    • 检查按钮的可用性
  5. 检查添加和删除按钮的条件:
    • 添加按钮的使用条件是添加的物品数量小于添加商品的总数
    • 删除按钮的使用条件是添加的物品数量大于0
@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *shopsView;@property (weak, nonatomic) IBOutlet UILabel *labelView;@property (weak, nonatomic) UIButton *addBtn;@property (weak, nonatomic) UIButton *removeBtn;@property (strong, nonatomic) NSArray *shops;@end@implementation ViewController#pragma mak 懒加载-(NSArray *)shops{    if(_shops == nil){    NSString *file = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];    _shops = [NSArray arrayWithContentsOfFile:file];    }    return _shops;}- (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;}- (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;}- (void)add{    //控件的大小    CGFloat shopW = 70;    CGFloat shopH = 90;    //增加一个整体的父控件    UIView *shopView = [[UIView alloc] init];    //一行的列数    int cols = 3;    //每一列间的距离    CGFloat maginX = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1);    //每一行间的距离    CGFloat maginY = 10;    //索引    NSUInteger index = self.shopsView.subviews.count;    //X的位置    NSUInteger col = index % cols;    CGFloat shopX = col * (maginX + shopW);    //Y的位置    NSUInteger row = index / cols;    CGFloat shopY = row * (maginY + shopH);    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);    [shopView addSubview:iconView];    //添加父控件的文字    UILabel *label = [[UILabel alloc] init];    label.text = shop[@"name"];    label.frame = CGRectMake(0, shopW, shopW, shopH - shopW);    label.font = [UIFont systemFontOfSize:15];    label.textAlignment = NSTextAlignmentCenter;    [shopView addSubview:label];    [self checkpoint];}- (void)remove{    [[self.shopsView.subviews lastObject] removeFromSuperview];    [self checkpoint];}- (void)checkpoint{    //添加按钮的判断条件    self.addBtn.enabled = (self.shopsView.subviews.count < self.shops.count);    //删除按钮的判断条件    self.removeBtn.enabled = (self.shopsView.subviews.count > 0);    //HUD    NSString *text = nil;    if (self.addBtn.enabled == NO) {//添加满了        text = @"已经全部添加进去";    } else if(self.removeBtn.enabled == NO){//删除光了        text = @"已经全部删除";    }    if (text == nil) return;    self.labelView.text = text;    self.labelView.alpha = 0.5;    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        self.labelView.alpha = 0.0;    });}
0 1
原创粉丝点击