九宫格 - 购物车抽象实现
2016-01-13 10:28
393 查看
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
//
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
//
相关文章推荐
- IOS网络访问之使用AFNetworking
- 堆和栈的区别
- 设计模式笔记--迭代器模式
- My mac cannot run 类相关的操作 , which is lower than 类相关的操作。
- PHP防止重复提交表单
- 教你在Excel里做GA的水平百分比图的详细步骤(图文教程)-成为excel大师(1)
- jQuery获取及设置单选框、多选框、文本框
- android 编译命令,线程选择,输出log
- Javascript学习笔记03——Js基本概念
- 未能加载文件或程序集“AspNetPager”或它的某一个依赖项。参数错误。 (异常来自 HRESULT:0x80070057 (E_INVALIDARG))
- UML基础
- v$archived_log视图中,有关时间的几列的含义
- 使用Swiper开发移动端页面,轻松实现图片的轮播
- 【C++基础05】引用的基础
- 记一次DataNode慢启动问题
- docker 查看容器详细
- lintcode : 二叉树的层次遍历II
- IOS中获取各种文件的目录路径的方法
- MFC学习(23)MFC 文件操作详解
- android ListView 动态切换Item布局