• 仿淘宝购物车计数器


    我使用的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);
        };
    
  • 相关阅读:
    Codeforces Beta Round #69 (Div. 2 Only)
    Codeforces Beta Round #67 (Div. 2)
    Codeforces Beta Round #65 (Div. 2)
    UmBasketella
    Codeforces Beta Round #63 (Div. 2)
    [agc004d]Teleporter
    [arc076f]Exhausted?
    NOIp模拟赛二十八
    CDQ分治笔记
    (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/9967757.html
Copyright © 2020-2023  润新知