定制选择范围的按钮RangeButton
效果:
源码:
RangeButton.h 与 RangeButton.m
//
// RangeButton.h
// PulsingView
//
// Created by YouXianMing on 14/10/29.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> 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 / .f;
CGFloat height = frame.size.height; // 左侧按钮
_leftButton = [[UIButton alloc] initWithFrame:CGRectMake(, , width, height)];
[_leftButton addTarget:self
action:@selector(buttonsEvent:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_leftButton]; // 中间Label
_centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(width, , width, height)];
_centerLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_centerLabel]; // 右侧按钮
_rightButton = [[UIButton alloc] initWithFrame:CGRectMake(width * , , 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 > && _buttonWidth < self.frame.size.width / .f) {
_leftButton.frame = CGRectMake(, , _buttonWidth, self.frame.size.height);
_centerLabel.frame = CGRectMake(_buttonWidth, , self.frame.size.width - _buttonWidth*, self.frame.size.height);
_rightButton.frame = CGRectMake(self.frame.size.width - _buttonWidth, , _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(, , , )]; rangeButton.maxValue = @;
rangeButton.minValue = @;
rangeButton.startValue = @;
rangeButton.stepValue = @; 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 = ; [rangeButton configCenterLabel:^(UILabel *label) {
label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
label.textColor = [UIColor yellowColor];
}]; [self.view addSubview:rangeButton];
} @end
重点需要注意的地方: