高仿阿里巴巴購(gòu)物車(chē),因項(xiàng)目需求做了簡(jiǎn)單的封裝,后續(xù)繼續(xù)完善.大家相互學(xué)習(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.導(dǎo)入頭文件:#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點(diǎn)擊加號(hào)事件
- -(void)clickAddButtonAction:(NSString *)str;
- 4.2點(diǎn)擊減號(hào)事件
- (void)clickReduceButtonAction:(NSString *)str;
- 4.3 文字改變事件
- (void)textChangeAction:(NSString *)str;
- 4.4 鍵盤(pán)完成事件
- (void)clickKeyBordCompleteAction:(NSString *)str;
- 4.5點(diǎn)擊關(guān)閉事件
- (void)closeButtonAction;
- 4.6 點(diǎn)擊確定事件
- (void)confirmButtonAction;
- 4.7 規(guī)格參數(shù)
- 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:@"加入購(gòu)物車(chē)" 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(@"我要加入購(gòu)物車(chē)了");
}
#pragma mark - 彈出框代理
//點(diǎn)擊加號(hào)事件
- (void)clickAddButtonAction:(NSString *)str
{
NSLog(@"代理加號(hào)點(diǎn)擊事件:%@",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]];
}
//點(diǎn)擊減號(hào)事件
- (void)clickReduceButtonAction:(NSString *)str
{NSLog(@"代理減號(hào)點(diǎn)擊事件:%@",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);
}
// 鍵盤(pán)完成事件
- (void)clickKeyBordCompleteAction:(NSString *)str
{
NSLog(@"代理完成后輸入的文字是:%@",str);
}
//點(diǎn)擊關(guān)閉事件
- (void)closeButtonAction
{
[UIView animateWithDuration: 0.35 animations: ^{
self.chooseView.frame =CGRectMake(0, kScreenH, kScreenW, kScreenH);
} completion: nil];
}
// 點(diǎn)擊確定事件
- (void)confirmButtonAction
{
[UIView animateWithDuration: 0.35 animations: ^{
self.chooseView.frame =CGRectMake(0, kScreenH, kScreenW, kScreenH);
} completion: nil];
}
// 點(diǎn)擊規(guī)格序號(hào)
- (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(@"點(diǎn)擊的是:第%ld個(gè)",index);
}
#pragma mark - 設(shè)置字體顏色
- (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