美文网首页
小轮子:数量选择器

小轮子:数量选择器

作者: dc630f46ee2d | 来源:发表于2017-12-20 22:01 被阅读0次
#import <UIKit/UIKit.h>
typedef void(^DishQuantitySelectorViewDidTouchupBlock)(void);
@interface DishQuantitySelectorView : UIView
@property (nonatomic, copy) DishQuantitySelectorViewDidTouchupBlock addBlock;
@property (nonatomic, copy) DishQuantitySelectorViewDidTouchupBlock reduceBlock;
@property (nonatomic, assign) NSInteger selectorValue;
+ (DishQuantitySelectorView *)dishQuantitySelectorView;
@end
#import "DishQuantitySelectorView.h"
#import "Masonry.h"
@interface DishQuantitySelectorView ()
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UILabel *valueLabel;
@property (nonatomic, strong) UIButton *rightButton;
@end

@implementation DishQuantitySelectorView

+ (DishQuantitySelectorView *)dishQuantitySelectorView {
    DishQuantitySelectorView *quantitySelectorView = [[DishQuantitySelectorView alloc] init];
    quantitySelectorView.selectorValue = 1;
    return quantitySelectorView;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton addTarget:self action:@selector(leftButtonDidTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self.mas_width).multipliedBy(1/3);
    }];
    self.leftButton = leftButton;
    
    UILabel *valueLabel = [[UILabel alloc] init];
    [self addSubview:valueLabel];
    [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self.mas_width).multipliedBy(1/3);
    }];
    valueLabel.numberOfLines = 1;
    self.valueLabel = valueLabel;
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton addTarget:self action:@selector(rightButtonDidTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self.mas_width).multipliedBy(1/3);
    }];
    self.rightButton = rightButton;
}

- (void)leftButtonDidTouchUpInside:(UIButton *)button {
    if (self.addBlock) {
        self.addBlock();
    }
}

- (void)rightButtonDidTouchUpInside:(UIButton *)button {
    if (self.reduceBlock) {
        self.reduceBlock();
    }
}

- (void)setSelectorValue:(NSInteger)selectorValue {
    if (selectorValue < 0) {
        _selectorValue = 0;
    }
    if (selectorValue > 9) {
        _selectorValue = 9;
    }
    self.leftButton.enabled = (_selectorValue == 0);
    self.rightButton.enabled = (_selectorValue == 9);
}
@end

相关文章

网友评论

      本文标题:小轮子:数量选择器

      本文链接:https://www.haomeiwen.com/subject/agewwxtx.html