前言
应用场景
- 购物车界面选择整单折扣
- 无品收银台界面选择订单折扣
支持的视图类型
typedef enum : NSUInteger { /** 下拉选择折扣,支持删除和添加 (最大列数4个) */ QCTDiscountViewType4col, /** 从侧边划出选择折扣视图 (最大列数3个) */ QCTDiscountViewType3col, } QCTDiscountViewType;
iOS开发交流,欢迎关注公众号:iOS逆向
I 、用法
折扣视图由表格的cell组成
1.1 cell的初始化
case QCTDropDownMenuViewSection4delete: { QCTDiscountTableViewCell *cell = [QCTDiscountTableViewCell tableViewCellWithTableView:tableView block:^(QCTDiscountModel * sender) { if (sender == nil) { return ; } if (sender.isADDorDelete) { if ([sender.name isEqualToString:@"+"]) { UIView *view = weakSelf.modal.contentView; if ([view isMemberOfClass:[DiscountPickerView class]]) { [weakSelf.modal show:YES]; }else{ [weakSelf.modal showContentView:self.discountPickerV animated:YES]; weakSelf.modal.positionMode = STModelPositionCenterBottom; } }else if([sender.name isEqualToString:@"-"]){ [weakSelf showpromptDeleteView]; } }else{ #pragma mark - ******** 处理折扣的点击事件 [weakSelf setupClickDiscount:sender]; } // } models:nil]; [cell setType:QCTDiscountViewType4col]; [cell setModels:[self getdiscountModelsWithArray:self.viewModel.discountContainAddDeleteModels index:indexPath.row clos:4 ]]; return cell; // // } break;
获取每一行的数据模型
#pragma mark - ******** 获取每一行的数据模型 - (NSMutableArray*)getdiscountModelsWithArray:(NSMutableArray*)discountModels index:(NSInteger)row clos:(NSInteger)clos{ // 获取总行 NSInteger all = (discountModels.count -1) /clos +1;//4 if (row> all) { return nil; } NSInteger startIndex = (row)*(clos);//0 4 NSInteger endIndex = startIndex+(clos-1);//3,7 NSMutableArray* tmp = [NSMutableArray array]; NSInteger realendIndex = endIndex > (discountModels.count-1) ? discountModels.count-1 : endIndex; for (NSInteger i =startIndex ; i<= realendIndex ; i++) { [tmp addObject:discountModels[i]]; } return tmp; }
1.2 视图的初始化
- (void)setModels:( id)models{ _models =models; self.cellView.model = models; } - (QCTDiscountView *)cellView{ if (nil == _cellView) { QCTDiscountView *tmpView = [[QCTDiscountView alloc]init]; _cellView = tmpView; [tmpView setBackgroundColor:kcellColor]; [self.contentView addSubview:_cellView]; __weak __typeof__(self) weakSelf = self; [_cellView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.contentView).offset( kAdjustRatio(kSideMargin+4)); make.right.equalTo(weakSelf.contentView).offset(- kAdjustRatio(kSideMargin+4)); make.top.equalTo(weakSelf.contentView).offset(kAdjustRatio(0)); make.bottom.equalTo(weakSelf.contentView).offset(kAdjustRatio(0)); }]; [_cellView setBlock:^(id _Nonnull sender) { if (weakSelf.block) { weakSelf.block(sender); } }]; } return _cellView; }
II、 DiscountView核心代码
2.1 QCTDiscountView.h
#import "QCTDiscountBtn.h" NS_ASSUME_NONNULL_BEGIN typedef enum : NSUInteger { QCTDiscountViewType4col, QCTDiscountViewType3col, } QCTDiscountViewType; @interface QCTDiscountView : UIView @property (nonatomic, copy) void (^block)(id sender); @property (nonatomic, strong) id model; @property (nonatomic, assign) QCTDiscountViewType type; @end
2.2 QCTDiscountView.m
#import "QCTDiscountView.h" /** iOS 自定义折扣处理视图:支持添加/删除/选择折扣 【 应用场景:购物车界面选择整单折扣,无品收银台界面选择订单折扣】 */ @interface QCTDiscountView () @end @implementation QCTDiscountView - (instancetype)init { self = [super init]; if (self) { UITapGestureRecognizer *cutTap = [[UITapGestureRecognizer alloc] init]; cutTap.cancelsTouchesInView = NO;// 设置tableView的点击事件优先级,低于cell的选中事件 [[cutTap rac_gestureSignal] subscribeNext:^(id x) { NSLog(@"QCTDiscountView"); }]; [self addGestureRecognizer:cutTap]; } return self; } - (void)setType:(QCTDiscountViewType)type{ switch (type) { case QCTDiscountViewType4col: { [self setupSubView:4]; } break; case QCTDiscountViewType3col: { [self setupSubView:3]; } break; default: break; } } - (void)setupSubView:(NSInteger)col{ QCTDiscountBtn * lasttmp; for (int i = 0; i<col; i++) { QCTDiscountBtn * tmp = [QCTDiscountBtn new]; tmp.tag = i+1000; [self addSubview:tmp]; __weak __typeof__(self) weakSelf = self; [tmp mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(weakSelf); make.width.equalTo(weakSelf).offset(-kAdjustRatio(15*(col-1)*0.25)).dividedBy(col); if (i%col == 0) { make.left.equalTo(weakSelf); }else{ make.left.equalTo(lasttmp.mas_right).offset(kAdjustRatio(15)); } make.bottom.equalTo(weakSelf).offset(kAdjustRatio(-20)); make.top.equalTo(weakSelf).offset(kAdjustRatio(0)); }]; lasttmp = tmp; tmp.hidden = YES; UITapGestureRecognizer *cutTap = [[UITapGestureRecognizer alloc] init]; [[cutTap rac_gestureSignal] subscribeNext:^(id x) { NSLog(@"model: %@",tmp.models.name); if (weakSelf.block) { weakSelf.block(tmp.models); } }]; [tmp addGestureRecognizer:cutTap]; } } /** Masonry比例用法 */ - (void)setModel:(NSMutableArray *)model{ _model = model; // 构建子试图 for (int i = 0; i<model.count; i++) { id obj = model[i]; QCTDiscountBtn * tmp = (QCTDiscountBtn *)[self viewWithTag:i + 1000];; tmp.models = obj; } } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ CGPoint redBtnPoint = [self convertPoint:point toView:self]; for (UIView *obj in self.subviews) { if ( CGRectContainsPoint(obj.frame, redBtnPoint) ) { return obj; } } if (redBtnPoint.y <= CGRectGetMaxY(self.frame)) { return self; } return [super hitTest:point withEvent:event]; } @end