类似淘宝的选择价格区间Demo

来源:互联网 发布:linux指令速查手册 编辑:程序博客网 时间:2024/04/30 11:35

1、大致搭建UI:
这里写图片描述

2、键盘
(1)点击空白处退下键盘

@property (weak, nonatomic) IBOutlet UITextField *lowPrice;@property (weak, nonatomic) IBOutlet UITextField *highPrice;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    [_lowPrice resignFirstResponder];    [_highPrice resignFirstResponder];}

(2)右上角添加”确定”按钮
这里关键就是给textField添加inputAccessoryView

// 创建“完成”按钮    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 90, 7, 80, 30)];    button.layer.cornerRadius = 5;    button.layer.borderWidth = 0.5;    button.layer.borderColor = [UIColor grayColor].CGColor;    [button setTitle:@"完成"forState:UIControlStateNormal];    button.titleLabel.font = [UIFont systemFontOfSize:14];    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [button addTarget:self action:@selector(refreshPriceInterval) forControlEvents:UIControlEventTouchUpInside];
- (void)refreshPriceInterval {    time = 2; // 这个地方模拟拉取网络数据(更新成对应价格区间的商品)所需的时间    [_lowPrice resignFirstResponder];    [_highPrice resignFirstResponder];    // 数据大小判断    if((![_lowPrice.text isEqualToString:@""]) && (![_highPrice.text isEqualToString:@""])) {        if([_lowPrice.text integerValue] > [_highPrice.text integerValue]) {            _lowP = _highPrice.text;            _highP = _lowPrice.text;        }    } else {        _lowP = _lowPrice.text;        _highP = _highPrice.text;    }      [_activityIndicatorView startAnimating];    [self hideActivityIndicatorView];}- (void)hideActivityIndicatorView {    _myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];    [[NSRunLoop mainRunLoop] addTimer:_myTimer forMode:NSRunLoopCommonModes];}- (void)updateTimer {    time--;     // 判断是否拉取完数据    if (time == 0) {        _lowPrice.text =  _lowP ;        _highPrice.text = _highP;        [self.myTimer invalidate];        [self.activityIndicatorView stopAnimating];    }}

// 给textField添加inputAccessoryView

【方法一】:用UIView

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];    [view setBackgroundColor: [UIColor colorWithRed:210/250.0 green:214/250.0 blue:220/250.0 alpha:1.0]];    [view addSubview:button];    _lowPrice.inputAccessoryView = view;    _highPrice.inputAccessoryView = view;

【方法二】:用UIToolBar
用barTintColor设置UIToolBar的背景色

 UIToolbar *view = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)]; view.barTintColor = [UIColor colorWithRed:210/250.0 green:214/250.0 blue:220/250.0 alpha:1.0]; // 注意:是barTintColor[view addSubview:button];_lowPrice.inputAccessoryView = view;_highPrice.inputAccessoryView = view;

到此,可以运行看到效果:
这里写图片描述

3、添加转动的小菊花

@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];_activityIndicatorView.color = [UIColor grayColor];_activityIndicatorView.frame = self.view.bounds;[self.view addSubview:_activityIndicatorView];

完整的Demo可以在:https://github.com/Yangchengfeng/IntervalOfPrice查看


这里所说的“类似淘宝的选择价格区间”并没有置于tableView和scrollView之上来做的,所以未涉及到上面所讲视图引起的问题,待后续···

1 0
原创粉丝点击