博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
定制选择范围的按钮RangeButton
阅读量:5081 次
发布时间:2019-06-12

本文共 8367 字,大约阅读时间需要 27 分钟。

定制选择范围的按钮RangeButton

效果:

源码:

RangeButton.h 与 RangeButton.m

////  RangeButton.h//  PulsingView////  Created by YouXianMing on 14/10/29.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
#import
typedef void (^RangeButtonCenterLabel)(UILabel *label);@interface RangeButton : UIView/** * 最开始显示的值 */@property (nonatomic, strong) NSNumber *startValue;/** * 每一次增加或者减少的值 */@property (nonatomic, strong) NSNumber *stepValue;/** * 最大值 */@property (nonatomic, strong) NSNumber *maxValue;/** * 最小值 */@property (nonatomic, strong) NSNumber *minValue;/** * 左侧按钮的普通图片,高亮图片,不能点击时的图片 */@property (nonatomic, strong) UIImage *leftNormalImage;@property (nonatomic, strong) UIImage *leftHighlightedImage;@property (nonatomic, strong) UIImage *leftDisableImage;/** * 右侧按钮的普通图片,高亮图片,不能点击时的图片 */@property (nonatomic, strong) UIImage *rightNormalImage;@property (nonatomic, strong) UIImage *rightHighlightedImage;@property (nonatomic, strong) UIImage *rightDisableImage;/** * 当前值 */@property (nonatomic, strong, readonly) NSNumber *currentValue;/** * 定制label * * @param block 中间的label */- (void)configCenterLabel:(RangeButtonCenterLabel)block;/** * 设置按钮的宽度(重置3个控件的frame值) */@property (nonatomic, assign) CGFloat buttonWidth;@end
////  RangeButton.m//  PulsingView////  Created by YouXianMing on 14/10/29.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "RangeButton.h"@interface RangeButton ()@property (nonatomic, strong) UIButton *leftButton;@property (nonatomic, strong) UIButton *rightButton;@property (nonatomic, strong) UILabel  *centerLabel;@property (nonatomic, strong) NSNumber *currentValue;@end@implementation RangeButton- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        CGFloat width  = frame.size.width / 3.f;        CGFloat height = frame.size.height;                        // 左侧按钮        _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];        [_leftButton addTarget:self                        action:@selector(buttonsEvent:)              forControlEvents:UIControlEventTouchUpInside];        [self addSubview:_leftButton];                // 中间Label        _centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(width, 0, width, height)];        _centerLabel.textAlignment = NSTextAlignmentCenter;        [self addSubview:_centerLabel];                        // 右侧按钮        _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(width * 2, 0, width, height)];        [_rightButton addTarget:self                         action:@selector(buttonsEvent:)               forControlEvents:UIControlEventTouchUpInside];        [self addSubview:_rightButton];    }    return self;}- (void)buttonsEvent:(UIButton *)button {    if (button == _leftButton) {        if (_rightNormalImage) {            [_rightButton setImage:_rightNormalImage forState:UIControlStateNormal];        }        if (_rightHighlightedImage) {            [_rightButton setImage:_rightHighlightedImage forState:UIControlStateHighlighted];        }                // 临时获取值        NSNumber *tmp = @([_currentValue intValue] - [_stepValue intValue]);                // 减去的值小于或者等于最小值时        if ([tmp intValue] < [_minValue intValue]) {        } else {            _currentValue = @([_currentValue intValue] - [_stepValue intValue]);            _centerLabel.text = [NSString stringWithFormat:@"%d", [_currentValue intValue]];                        if ([_currentValue intValue] == [_minValue intValue]) {                if (_leftDisableImage) {                    [_leftButton setImage:_leftDisableImage forState:UIControlStateNormal];                    [_leftButton setImage:_leftDisableImage forState:UIControlStateHighlighted];                }            }        }    } else {        if (_leftNormalImage) {            [_leftButton setImage:_leftNormalImage forState:UIControlStateNormal];        }        if (_leftHighlightedImage) {            [_leftButton setImage:_leftHighlightedImage forState:UIControlStateHighlighted];        }                // 临时获取值        NSNumber *tmp = @([_currentValue intValue] + [_stepValue intValue]);                // 减去的值小于或者等于最小值时        if ([tmp intValue] > [_maxValue intValue]) {        } else {            _currentValue = @([_currentValue intValue] + [_stepValue intValue]);            _centerLabel.text = [NSString stringWithFormat:@"%d", [_currentValue intValue]];                        if ([_currentValue intValue] == [_maxValue intValue]) {                if (_rightDisableImage) {                    [_rightButton setImage:_rightDisableImage forState:UIControlStateNormal];                    [_rightButton setImage:_rightDisableImage forState:UIControlStateHighlighted];                }            }        }    }}- (void)configCenterLabel:(RangeButtonCenterLabel)block {    block(self.centerLabel);}#pragma mark - 重写各种setter,getter方法@synthesize startValue = _startValue;- (void)setStartValue:(NSNumber *)startValue {    _startValue       = startValue;    _currentValue     = _startValue;    _centerLabel.text = [NSString stringWithFormat:@"%d", [startValue intValue]];}- (NSNumber *)startValue {    return _startValue;}@synthesize rightNormalImage = _rightNormalImage;- (void)setRightNormalImage:(UIImage *)rightNormalImage {    _rightNormalImage = rightNormalImage;    [_rightButton setImage:_rightNormalImage forState:UIControlStateNormal];}- (UIImage *)rightNormalImage {    return _rightNormalImage;}@synthesize rightHighlightedImage = _rightHighlightedImage;- (void)setRightHighlightedImage:(UIImage *)rightHighlightedImage {    _rightHighlightedImage = rightHighlightedImage;    [_rightButton setImage:rightHighlightedImage forState:UIControlStateHighlighted];}- (UIImage *)rightHighlightedImage {    return _rightHighlightedImage;}@synthesize leftNormalImage = _leftNormalImage;- (void)setLeftNormalImage:(UIImage *)leftNormalImage {    _leftNormalImage = leftNormalImage;    [_leftButton setImage:leftNormalImage forState:UIControlStateNormal];}- (UIImage *)leftNormalImage {    return _leftNormalImage;}@synthesize leftHighlightedImage = _leftHighlightedImage;- (void)setLeftHighlightedImage:(UIImage *)leftHighlightedImage {    _leftHighlightedImage = leftHighlightedImage;    [_leftButton setImage:leftHighlightedImage forState:UIControlStateHighlighted];}- (UIImage *)leftHighlightedImage {    return _leftHighlightedImage;}@synthesize buttonWidth = _buttonWidth;- (void)setButtonWidth:(CGFloat)buttonWidth {    _buttonWidth = buttonWidth;        if (_buttonWidth > 0 && _buttonWidth < self.frame.size.width / 2.f) {        _leftButton.frame  = CGRectMake(0, 0, _buttonWidth, self.frame.size.height);        _centerLabel.frame = CGRectMake(_buttonWidth, 0, self.frame.size.width - _buttonWidth*2, self.frame.size.height);        _rightButton.frame = CGRectMake(self.frame.size.width - _buttonWidth, 0, _buttonWidth, self.frame.size.height);    }    }- (CGFloat)buttonWidth {    return _buttonWidth;}@end

实现的源码:

////  ViewController.m//  PulsingView////  Created by YouXianMing on 14/10/29.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "ViewController.h"#import "RangeButton.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor blackColor];     RangeButton *rangeButton = [[RangeButton alloc] initWithFrame:CGRectMake(100, 100, 100, 22)];        rangeButton.maxValue     = @10;    rangeButton.minValue     = @0;    rangeButton.startValue   = @5;    rangeButton.stepValue    = @1;        rangeButton.rightNormalImage      = [UIImage imageNamed:@"后_nor"];    rangeButton.rightHighlightedImage = [UIImage imageNamed:@"后_high"];    rangeButton.rightDisableImage     = [UIImage imageNamed:@"后_dis"];    rangeButton.leftNormalImage       = [UIImage imageNamed:@"前_nor"];    rangeButton.leftHighlightedImage  = [UIImage imageNamed:@"前_high"];    rangeButton.leftDisableImage      = [UIImage imageNamed:@"前_dis"];        rangeButton.buttonWidth = 29;        [rangeButton configCenterLabel:^(UILabel *label) {        label.font      = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.f];        label.textColor = [UIColor yellowColor];    }];        [self.view addSubview:rangeButton];}@end

重点需要注意的地方:

 

转载于:https://www.cnblogs.com/YouXianMing/p/4062231.html

你可能感兴趣的文章
Qt 学习之路 :使用 QJson 处理 JSON
查看>>
NPOI操作Excel导入导出
查看>>
angularJS 移动端焦点图
查看>>
jvm 这我就能会了 擦
查看>>
实战技能:小小微信支付业务,何必虚惊一场
查看>>
17-1 djanjo进阶-路由,视图,模板
查看>>
Shell脚本8种字符串截取方法总结
查看>>
P3254 圆桌问题
查看>>
MapReduce的运行原理
查看>>
Leetcode: Partition List
查看>>
故障转移
查看>>
清空dataset中的某行某列的数据
查看>>
盒模型
查看>>
js中闭包和作用域
查看>>
关键词提取
查看>>
装饰器,迭代器与生成器
查看>>
endpoint 理解-1
查看>>
Lambad表达式树(转)
查看>>
<每日一题>题目28:简单的python练习题(51-60)
查看>>
15 SharePreference
查看>>