IOS加减选择器

最近由于在做预约部分的功能,需要选择人数和时间,并且还需要在时间的时候做出选择到最大小时之后,显示成最大小时后,所以写了一个小的demo,在这里分享给大家,希望大家可以用到:

封装的View的.h文件


#import <UIKit/UIKit.h>

@interface NumberSeletedView : UIView

/**
 *  最低数字
 */
@property (nonatomic , assign) int lowI;

/**
 *  最高数字
 */
@property (nonatomic , assign) int HighI;

/**
 *  拼装文字
 */
@property (nonatomic , strong) NSString *title;

/**
 *  显示数字的区域
 */
@property (nonatomic , weak) UILabel *timeLable;

/**
 *  数字
 */
@property (nonatomic , assign) int i;

/**
 *  决定是否有 i人以上   YES为有  NO为没有
 */
@property (nonatomic , assign) BOOL HighType;

@end

封装View的.m文件


#import "NumberSeletedView.h"
#import "UIView+Extension.h"

@interface NumberSeletedView ()

/**
 *  减小按钮
 */
@property (nonatomic , weak) UIButton *leftBtn;

/**
 *  增加按钮
 */
@property (nonatomic , weak) UIButton *rightBtn;

/**
 *  N人以上区域
 */
@property (nonatomic , weak) UIView *topView;

/**
 *  N人以上文字
 */
@property (nonatomic , weak) UILabel *lable;



@end

@implementation NumberSeletedView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //设置背景
        UIImageView *comeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        comeView.image = [UIImage imageNamed:@"bespeakBG"];
        [self addSubview:comeView];

        //设置显示内容
        UILabel *timeLable = [[UILabel alloc] initWithFrame:comeView.frame];
        self.timeLable = timeLable;
        timeLable.textAlignment = NSTextAlignmentCenter;
        timeLable.font = [UIFont systemFontOfSize:12];
        [self addSubview:timeLable];

        //设置减小按钮
        UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 31, 30)];
        self.leftBtn = leftBtn;
        leftBtn.userInteractionEnabled = NO;
        [leftBtn setImage:[UIImage imageNamed:@"bespeakReduceEnable"] forState:UIControlStateNormal];
        [leftBtn setImage:[UIImage imageNamed:@"bespeakReduceHeight"] forState:UIControlStateHighlighted];
        [leftBtn addTarget:self action:@selector(leftBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:leftBtn];

        //设置增大按钮
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 31, 0, 31, 30)];
        self.rightBtn = rightBtn;
        [rightBtn setImage:[UIImage imageNamed:@"bespeakAdd"] forState:UIControlStateNormal];
        [rightBtn setImage:[UIImage imageNamed:@"bespeakAddHeight"] forState:UIControlStateHighlighted];
        [rightBtn addTarget:self action:@selector(rightBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rightBtn];

        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(leftBtn.width, 0, self.width - leftBtn.width, self.height)];
        self.topView = topView;
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.topView.width, self.topView.height)];
        imgView.image = [UIImage imageNamed:@"bespeakTopBG"];
        [self.topView addSubview:imgView];
        UILabel * lable = [[UILabel alloc] initWithFrame:imgView.frame];
        self.lable = lable;

        lable.font = [UIFont systemFontOfSize:14];
        lable.textAlignment = NSTextAlignmentCenter;
        [self.topView addSubview:lable];
        [self addSubview:self.topView];
        self.topView.hidden = YES;
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.i = self.lowI;
    self.timeLable.text = [NSString stringWithFormat:@"%d%@",self.i,self.title];
    self.lable.text = [NSString stringWithFormat:@"%d人以上",(self.HighI - 1)];
}

#pragma mark 点击减
- (void)leftBtn:(UIButton *)btn{
    self.i --;
    if (self.HighType == YES) {
        if (self.i <= self.HighI) {
            self.topView.hidden = YES;
        }
    }
    if (self.i <= self.lowI) {
        btn.userInteractionEnabled = NO;
        [btn setImage:[UIImage imageNamed:@"bespeakReduceEnable"] forState:UIControlStateNormal];
    } else {
        btn.userInteractionEnabled = YES;
        self.rightBtn.userInteractionEnabled = YES;
        [self.rightBtn setImage:[UIImage imageNamed:@"bespeakAdd"] forState:UIControlStateNormal];
    }
    self.timeLable.text = [NSString stringWithFormat:@"%d%@",self.i,self.title];
}

#pragma mark 点击加
- (void)rightBtn:(UIButton *)btn{
    self.i ++;
    if (self.HighType == YES) {
        if (self.i >= self.HighI) {
            self.topView.hidden = NO;
        }
    }
    if (self.i >= self.HighI) {
        btn.userInteractionEnabled = NO;
        [btn setImage:[UIImage imageNamed:@"bespeakAddEnable"] forState:UIControlStateNormal];
    } else {
        self.leftBtn.userInteractionEnabled = YES;
        [self.leftBtn setImage:[UIImage imageNamed:@"bespeakReduce"] forState:UIControlStateNormal];

    }
    self.timeLable.text = [NSString stringWithFormat:@"%d%@",self.i,self.title];
}

调用的方法

//到店时间
    UIView *timeView = [self addViewWithY:CGRectGetMaxY(divisionView.frame) WithTitle:@"到店时间:" WithHeight:100];
    //设置小时选择器
    NumberSeletedView *numberView = [[NumberSeletedView alloc] initWithFrame:CGRectMake(timeView.width - 150, (timeView.height - 30 ) * 0.5, 140, 30)];
    self.numberView = numberView;
    numberView.lowI = 1;
    numberView.HighI = 12;
    numberView.title = @"小时内";
    numberView.HighType = NO;
    [timeView addSubview:numberView];

    UILabel *PromptLable = [[UILabel alloc] init];
    PromptLable.font = [UIFont systemFontOfSize:12];
    PromptLable.text = @"注:只支持12小时内的预约哦!";
    [PromptLable sizeToFit];
    PromptLable.frame = CGRectMake(timeView.width - PromptLable.width - 10, timeView.height - PromptLable.height - 5, PromptLable.width, PromptLable.height);
    PromptLable.textColor = RGBA(242, 91, 66, 1);
    [timeView addSubview:PromptLable];
    [footerView addSubview:timeView];

    //预约人数
    UIView *bespeakView = [self addViewWithY:CGRectGetMaxY(timeView.frame) WithTitle:@"预约人数:" WithHeight:80];
    //设置预约人数选择器
    NumberSeletedView *personView = [[NumberSeletedView alloc] initWithFrame:CGRectMake(bespeakView.width - 120, (bespeakView.height - 30 ) * 0.5, 110, 30)];
    self.personView = personView;
    personView.lowI = 1;
    NSUInteger count = self.bespeakArray.count + 1;
    NSString *highCount = [NSString stringWithFormat:@"%lu",(unsigned long)count];
    personView.HighI = [highCount intValue];
    personView.title = @"";
    personView.HighType = YES;
    [bespeakView addSubview:personView];
    [footerView addSubview:bespeakView];

代码就这么多,如果有什么不好的地方,请大家多多指教!谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值