iOS订单折扣视图应用于购物车界面(支持添加/删除/选择折扣)

简介: iOS订单折扣视图应用于购物车界面(支持添加/删除/选择折扣)

前言

应用场景

  1. 购物车界面选择整单折扣

image.png

  1. 无品收银台界面选择订单折扣

image.png

支持的视图类型

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

III、数据模型DiscountModel

@interface QCTDiscountModel : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) BOOL isSlelected;
@property (nonatomic,assign) BOOL isdeleted;
@property (nonatomic,copy) NSString *imageName;
@property (nonatomic,assign) BOOL isADDorDelete;
+ (instancetype)getQCTDiscountModelWithname:(NSString*)name;
+ (NSMutableArray*)getQCTDiscountModels:(NSMutableArray*)arr;
@property (nonatomic,assign) BOOL isfirstModel;

see also

git 代码分支管理教程

目录
相关文章
|
2月前
|
iOS开发 开发者
苹果iOS App Store上架操作流程详解:从开发者账号到应用发布
很多开发者在开发完iOS APP、进行内测后,下一步就面临上架App Store,不过也有很多同学对APP上架App Store的流程不太了解,下面我们来说一下iOS APP上架App Store的具体流程,如有未涉及到的部分,大家可以及时咨询,共同探讨。
|
2月前
|
开发者 iOS开发
iOS应用上架详细图文教程(上)
App Store作为苹果官方的应用商店,审核严格周期长一直让用户头疼不已,很多app都“死”在了审核这一关,那我们就要放弃iOS用户了吗?当然不是!本期我们从iOS app上架流程开始梳理,详细了解下iOS app上架的那些事。
|
2月前
|
Swift iOS开发 开发者
iOS 应用上架流程详解
iOS 应用上架流程详解
|
27天前
|
安全 数据安全/隐私保护 虚拟化
iOS应用加固方案解析:ipa加固安全技术全面评测
iOS应用加固方案解析:ipa加固安全技术全面评测
36 3
|
1月前
|
运维 监控 安全
应用研发平台EMAS常见问题之sophix ios flutter热更新如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
77 0
|
1月前
|
Web App开发 前端开发 网络安全
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
【2月更文挑战第21天】前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
47 1
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
|
2月前
|
Linux Android开发 iOS开发
iOS 应用上架的步骤和工具简介
APP开发助手是一款能够辅助iOS APP上架到App Store的工具,它解决了iOS APP上架流程繁琐且耗时的问题,帮助跨平台APP开发者顺利将应用上架到苹果应用商店。最重要的是,即使没有配置Mac苹果机,也可以使用该工具完成一系列操作,包括iOS证书申请、创建iOS开发者证书和 iOS发布证书等各类证书。此外,在Windows、Linux或Mac系统中上传IPA到App Store也变得简单快捷,从而大大简化了iOS APP上架的流程。
|
2月前
|
移动开发 前端开发 安全
保护你的 iOS 应用,防止逆向破解
保护你的 iOS 应用,防止逆向破解
|
2月前
|
存储 安全 数据安全/隐私保护
iOS应用上架详细图文教程(下)
我们这边介绍一个简便的证书制作小方法。
|
2月前
|
安全 算法 数据安全/隐私保护
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤