1.模拟一个简单的购车购物模型,运用到九宫格的算法,通过点击加号按钮往UIView(购物车)中添加商品
2.可以通过减号按钮将UIView的商品减少
3.这份代码添加商品/删除商品和九宫格算法的代码全部在控制器中实现,没有实现抽取
ViewController.m文件
#import "ViewController.h"
#import "ZHProducts.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutletUIView *shopCarView;
@property (weak, nonatomic) IBOutletUIButton *add;
@property (weak, nonatomic) IBOutletUIButton *remove;
@property (strong,nonatomic)NSArray *product;
@end
@implementation ViewController
//懒加载
-(NSArray *)product{
if (_product ==nil) {
/*
_product = @[@{@"icon":@"danjianbao",@"title":@"单肩包"},
@{@"icon":@"shuangjianbao",@"title":@"双肩包"},
@{@"icon":@"xiekuabao",@"title":@"斜挎包"},
@{@"icon":@"qianbao",@"title":@"小钱包"},
@{@"icon":@"liantiaobao",@"title":@"链条包"},
@{@"icon":@"shoutibao",@"title":@"手提包"}
];
[_product writeToFile:@"/Users/admin/Desktop/product.plist" atomically:YES];
*/
//获取全路径
NSString *str = [[NSBundlemainBundle]pathForResource:@"product.plist"ofType:nil];
//获取数据
_product = [NSArrayarrayWithContentsOfFile:str];
//定义一个临时的数组,用于接收遍历所得的数据
NSMutableArray *arr = [NSMutableArrayarray];
for (NSDictionary *dicin self.product) {
ZHProducts *products = [[ZHProductsalloc]initWithProduct:dic];
[arr addObject:products];
}
_product = arr;
}
return_product;
}
- (void)viewDidLoad {
[superviewDidLoad];
}
//添加商品
- (IBAction)addImage:(UIButton *)sender {
//******************算法******************
//定义列数
NSInteger listNumber = 3;
//定义imagebutton的weith和height
CGFloat weith = 80;
CGFloat height = 100;
//计算shopimageview中的imagebutton数
NSInteger index =self.shopCarView.subviews.count;
//求出水平间距和垂直间距
CGFloat xGap = (self.shopCarView.frame.size.width - listNumber * weith)/(listNumber - 1);
CGFloat yGap = (self.shopCarView.frame.size.height - (index / listNumber + 1)* height);
//求出x和y
CGFloat x = (weith + xGap)*(index % listNumber);
CGFloat y = (height +yGap)*(index / listNumber);
//*********************创建button装入shopCarView中**************
UIButton *imageButton = [[UIButtonalloc]initWithFrame:CGRectMake(x, y, weith, height)];
//设置imageButton的背景颜色
imageButton.backgroundColor = [UIColorredColor];
//将创建好的imageButton加到shopcarView中
[self.shopCarViewaddSubview:imageButton];
//*********************向imageButton中增加图片和标题**************
//1.创建需要增加的图片对象
UIImageView *imageView = [[UIImageViewalloc]init];
imageView.frame = CGRectMake(0, 0, weith, weith);
imageView.backgroundColor = [UIColorblueColor];
[imageButton addSubview:imageView];
//2.创建需要增加的标题对象
UILabel *titleLabel = [[UILabelalloc]init];
titleLabel.frame = CGRectMake(0, weith, weith, height - weith);
titleLabel.backgroundColor = [UIColorpurpleColor];
titleLabel.textAlignment =NSTextAlignmentCenter;
[imageButton addSubview:titleLabel];
//3.赋值数据
ZHProducts *s = self.product[index];
imageView.image = [UIImageimageNamed:s.icon];
titleLabel.text = s.title;
//*********************判断按钮状态条件**************
sender.enabled = index!=5;
if (index == 0) {
self.remove.enabled =YES;
}
}
//移除商品
- (IBAction)removeImage:(UIButton *)sender {
NSInteger index =self.shopCarView.subviews.count;
UIImageView *view = [self.shopCarView.subviewslastObject];
[view removeFromSuperview];
self.add.enabled =YES;
sender.enabled = index !=1;
}
@end