高仿阿里巴巴購物車,因項目需求做了簡單的封裝,后續(xù)繼續(xù)完善.大家相互學習

ali.gif

Simulator Screen Shot 2017年3月25日 下午4.54.28.png
![Uploading Simulator Screen Shot 2017年3月25日 下午4.54.28_371589.png . . .]
1.使用方法
1.導入頭文件:#import "GFChooseView.h"
2.遵守代理 <GFChooseViewDelegate>
3.創(chuàng)建
self.chooseView = [[GFChooseView alloc]initWithFrame:CGRectMake(0, kScreenH, kScreenW, kScreenH)];
_chooseView.delegate = self;
[self.view addSubview:_chooseView];
4.GFChooseViewDelegate代理方法
- 4.1點擊加號事件
- -(void)clickAddButtonAction:(NSString *)str;
- 4.2點擊減號事件
- (void)clickReduceButtonAction:(NSString *)str;
- 4.3 文字改變事件
- (void)textChangeAction:(NSString *)str;
- 4.4 鍵盤完成事件
- (void)clickKeyBordCompleteAction:(NSString *)str;
- 4.5點擊關閉事件
- (void)closeButtonAction;
- 4.6 點擊確定事件
- (void)confirmButtonAction;
- 4.7 規(guī)格參數
- 4.8(void)clickBtnIndexWithTag:(NSInteger)index;
2.代碼
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 加載底部視圖
[self setUpBottomView];
}
#pragma makr - 視圖
- (void)setUpBottomView
{
UIButton *bottomBtn =[YNTUITools createButton:CGRectMake(0, kScreenH - 50, kScreenW, 50) bgColor:[UIColor orangeColor] title:@"加入購物車" titleColor:[UIColor whiteColor] action:@selector(pushViews:) vc:self];
[self.view addSubview:bottomBtn];
self.chooseView = [[GFChooseView alloc]initWithFrame:CGRectMake(0, kScreenH, kScreenW, kScreenH)];
_chooseView.delegate = self;
[self.view addSubview:_chooseView];
}
- (void)pushViews:(UIButton *)sender
{
[UIView animateWithDuration: 0.35 animations: ^{
_chooseView.frame =CGRectMake(0, 0, kScreenW, kScreenH);
} completion: nil];
NSLog(@"我要加入購物車了");
}
#pragma mark - 彈出框代理
//點擊加號事件
- (void)clickAddButtonAction:(NSString *)str
{
NSLog(@"代理加號點擊事件:%@",str);
NSArray *arr1 =@[@"10",@"15",@"35",@"35",@"60"];
NSInteger priceNumber = [arr1[self.index] integerValue];
NSInteger number = [str integerValue];
NSInteger totalNumber = number * priceNumber;
_chooseView.totallMoneyLab.text = [NSString stringWithFormat:@"¥%ld",totalNumber];
[self setToatalNumberColor:_chooseView.goodsNumberLab andStr:[NSString stringWithFormat:@"共%@件",str]];
}
//點擊減號事件
- (void)clickReduceButtonAction:(NSString *)str
{NSLog(@"代理減號點擊事件:%@",str);
NSArray *arr1 =@[@"10",@"15",@"35",@"35",@"60"];
NSInteger priceNumber = [arr1[self.index] integerValue];
NSInteger number = [str integerValue];
NSInteger totalNumber = number * priceNumber;
_chooseView.totallMoneyLab.text = [NSString stringWithFormat:@"¥%ld",totalNumber];
[self setToatalNumberColor:_chooseView.goodsNumberLab andStr:[NSString stringWithFormat:@"共%@件",str]];
}
// 文字改變事件
- (void)textChangeAction:(NSString *)str
{
NSLog(@"代理文字正在改變:%@",str);
}
// 鍵盤完成事件
- (void)clickKeyBordCompleteAction:(NSString *)str
{
NSLog(@"代理完成后輸入的文字是:%@",str);
}
//點擊關閉事件
- (void)closeButtonAction
{
[UIView animateWithDuration: 0.35 animations: ^{
self.chooseView.frame =CGRectMake(0, kScreenH, kScreenW, kScreenH);
} completion: nil];
}
// 點擊確定事件
- (void)confirmButtonAction
{
[UIView animateWithDuration: 0.35 animations: ^{
self.chooseView.frame =CGRectMake(0, kScreenH, kScreenW, kScreenH);
} completion: nil];
}
// 點擊規(guī)格序號
- (void)clickBtnIndexWithTag:(NSInteger)index
{
NSArray *arr = @[@"1",@"2",@"3",@"4",@"5"];
NSArray *arr1 =@[@"¥10",@"¥15",@"¥35",@"¥35",@"¥60"];
self.chooseView.img.image = [UIImage imageNamed:arr[index]];
self.chooseView.priceLab.text = arr1[index];
self.index = index;
NSLog(@"點擊的是:第%ld個",index);
}
#pragma mark - 設置字體顏色
- (void)setToatalNumberColor:(UILabel *)lab andStr:(NSString *)str
{
NSRange Range1 = NSMakeRange([str rangeOfString:@"共"].location, [str rangeOfString:@"共"].length);
NSRange Range2 = NSMakeRange([str rangeOfString:@"件"].location, [str rangeOfString:@"件"].length);
NSMutableAttributedString *textLabelStr =
[[NSMutableAttributedString alloc]
initWithString:str];
[textLabelStr
setAttributes:@{NSForegroundColorAttributeName :
[UIColor grayColor], NSFontAttributeName :
[UIFont systemFontOfSize:17]} range:Range1];
[textLabelStr setAttributes:@{NSForegroundColorAttributeName :
[UIColor grayColor], NSFontAttributeName :
[UIFont systemFontOfSize:17]} range:Range2];
lab.attributedText = textLabelStr;
}
@end