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);
        };
    
  • 相关阅读:
    JS 里的数据类型转换
    mysql-5.7.25解压版本安装和navicat 12.1版本破解-4.8破解工具
    idea如何打包项目(java)
    idea如何使用git
    staruml百度网盘下载
    IDEA如何添加库lib(java)
    Win10下JDK环境搭建的两种方法
    HashMap的四种遍历!
    (转)排序算法之插入排序
    (转)排序算法之快速排序
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/9967757.html
Copyright © 2011-2022 走看看