zoukankan      html  css  js  c++  java
  • 仿淘宝购物车计数器

    我使用的Masonary布局,当然这里可以改成frame,很简单,新建CountView继承UIView,CreateUI这个类是我封装创建基础的UILabel,UIButton的,很简单的代码

    /*
     项目新增数量计数器
     */
    #import <UIKit/UIKit.h>
    @interface CountView : UIView
    //如果点击+或者-,把当前的计数传出去 @property(nonatomic,copy)void(^getNumber)(int number); @end

     #import "CountView.h"#import "CreateUI.h"#import "Masonry.h"

    #import "CountView.h"
    #import "CreateUI.h"
    //#import "DefineStr.h"
    #import "Masonry.h"
    
    #define BackColor @"#d6d6d6"
    @interface CountView ()
    @property(nonatomic,strong)UIButton *subBtn;
    @property(nonatomic,strong)UILabel *numberLab;
    @property(nonatomic,strong)UIButton *addBtn;
    @end
    
    @implementation CountView
    
    -(instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if(self){
            //给当前View加圆角
            [CreateUI radiuCornerWithColor:self radius:3.0 colorStr:BackColor];
            //创建UIButton
            self.subBtn = [CreateUI createBtnWithTile:@"-" withTitleColorStr:@"#2a2a2a" withFontSize:12];
            [self addSubview:self.subBtn];
            //创建UILabel
            self.numberLab = [CreateUI createLabelWithTextColorStr:@"#2a2a2a" withFontSize:12 withText:@"0"];
            [self addSubview:self.numberLab];
            
            self.addBtn = [CreateUI createBtnWithTile:@"+" withTitleColorStr:@"#2a2a2a" withFontSize:12];
            [self addSubview:self.addBtn];
            
            
            [self.subBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
            [CreateUI addFrameWithRightView:self.subBtn borderWidth:1.0f withBorderColor:BackColor];
            [CreateUI addFrameWithRightView:self.numberLab borderWidth:1.0f withBorderColor:BackColor];
            [self.addBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
            
            [self.subBtn setEnabled:NO];
            [self.subBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.offset(0);
                make.width.equalTo(self.mas_width).multipliedBy(2/7.0);
            }];
            self.numberLab.textAlignment = NSTextAlignmentCenter;
            [self.numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.subBtn.mas_right);
                make.top.bottom.offset(0);
                make.right.equalTo(self.addBtn.mas_left);
            }];
            [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.subBtn.mas_right);
                make.top.bottom.right.offset(0);
                make.width.equalTo(self.subBtn.mas_width);
            }];
            
            
            [self.subBtn addTarget:self action:@selector(subBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
            
        }
        return self;
    }
    //点击-号事件
    -(void)subBtnClick{
        int number = [self.numberLab.text intValue]-1;
        self.numberLab.text = [NSString stringWithFormat:@"%d",number];
        //如果已经是0了,则-号不能点击
        if([self.numberLab.text intValue] == 0){
            [self.subBtn setEnabled:NO];
        }
        if(self.getNumber){
            self.getNumber(number);
        }
    }
    //点击+号事件
    -(void)addBtnClick{
        int number = [self.numberLab.text intValue]+1;
        self.numberLab.text = [NSString stringWithFormat:@"%d",number];
        //如果-号不能点击,当前计数大于0,则-号UIButton可点击
        if(![self.subBtn isEnabled]){
            [self.subBtn setEnabled:YES];
        }
        if(self.getNumber){
            self.getNumber(number);
        }
    }
    
    @end
    
     

    使用:

    CountView *view = [CountView new];
        [self.view addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(500);
            make.left.offset(100);
            make.width.offset(100);
            make.height.offset(50);
        }];
        view.getNumber = ^(int number) {
            NSLog(@"----:%d",number);
        };
    
  • 相关阅读:
    Laravel 初始化
    ant design pro 左上角 logo 修改
    请求到服务端后是怎么处理的
    Websocket 知识点
    王道数据结构 (7) KMP 算法
    王道数据结构 (6) 简单的模式匹配算法
    王道数据结构 (4) 单链表 删除节点
    王道数据结构 (3) 单链表 插入节点
    王道数据结构 (2) 单链表 尾插法
    王道数据结构 (1) 单链表 头插法
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/9967757.html
Copyright © 2011-2022 走看看