定制选择范围的按钮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

重点需要注意的地方:

定制选择范围的按钮RangeButton的更多相关文章

  1. MFC选择文件(夹)按钮实现

    MFC选择文件(夹)按钮实现 选择文件(夹) void CFileSelectDlg::OnBnClickedButtonSelect() { if(((CButton*)(GetDlgItem(ID ...

  2. 定制二选一按钮SwitchButton

    定制二选一按钮SwitchButton 效果: 源码: SwitchButton.h 与 SwitchButton.m // // SwitchButton.h // KongJian // // C ...

  3. Java+Selenium 上传文件,点击选择“浏览文件”按钮,报错invalid argument

    Java+Selenium 上传文件,点击选择"浏览文件"按钮,报错invalid argument 解决代码: Actions action=new Actions(driver ...

  4. Dynamics CRM定制子网格添加按钮实例之二:调试代码、打开Web资源及获取选择的记录

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复222或者20160501可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  5. Dynamics CRM定制子网格添加按钮实例之一

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复221或者20160430可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  6. ASPxGridView后台获取edit、delete、选择框等按钮。

    GridViewCommandColumn commandColumn = (GridViewCommandColumn)ASPxGridViewInstance.Columns["#&qu ...

  7. 定制Android透明按钮

    自己在学习和做例子的过程中,常常会需要按钮,由于系统自带按钮样式不太好看,所以需要我们自己来定制项目得按钮,我常常采用2中方法: 1.是制作9-patch的图片,这样能够匹配文字内容的长短. 2.是指 ...

  8. WebUploader 上传插件结合bootstrap的模态框使用时选择上传文件按钮无效问题的解决方法

    由于种种原因(工作忙,要锻炼健身,要看书,要学习其他兴趣爱好,谈恋爱等),博客已经好久没有更新,为这个内心一直感觉很愧疚,今天开始决定继续更新博客,每周至少一篇,最多不限篇幅. 今天说一下,下午在工作 ...

  9. Flex4之皮肤定制

    Flex4之皮肤定制[Skin类和Skin类]          博客分类: RIA-Flex4专栏 FlexAdobeUPFlashUI 第一.关于spark.skin.SparkSkin类的 1. ...

随机推荐

  1. Chapter 3 Phenomenon——6

    A low oath made me aware that someone was with me, and the voice was impossible not to recognize. 某人 ...

  2. iptables关键学习总结

    iptables技术推荐参考这位仁兄的博客:http://www.zsythink.net/archives/category/%E8%BF%90%E7%BB%B4%E7%9B%B8%E5%85%B3 ...

  3. R语言常用包分类总结

    常用包: ——数据处理:lubridata ,plyr ,reshape2,stringr,formatR,mcmc: ——机器学习:nnet,rpart,tree,party,lars,boost, ...

  4. ibatis SQLmap mysql模糊查询字符串拼的三种方法

    在通常情况下iBATIS的参数在sqlmap中使用#param#的形式,参数名以’#’包着,但当使用sql的LIKE语句时就发生了问题,在单引号中无法使用#param#这种形式,下面列举出了3种方法来 ...

  5. Nginx使用记录

    配置常见解释: ########### 每个指令必须有分号结束.################# #user administrator administrators; #配置用户或者组,默认为no ...

  6. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

  7. js加载事件和js函数定义

    一  dom文档树加载完之后执行一个函数 在Dom加载完成后执行函数,下面这三个的作用是一样的,window.onload 是JavaScript的,window.onload是在dom文档树加载完和 ...

  8. Mac下显示和隐藏隐藏文件的命令

    打开终端,输入: 1.defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com. ...

  9. SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器

    一.参数的传递 1.简单的参数传递 /* @RequestParam用法:入参名字与方法名参数名不一致时使用{ * value:传入的参数名,required:是否必填,defaultValue:默认 ...

  10. AGC008E:Next or Nextnext

    传送门 考虑转化成图论问题,\(i\) 向 \(p_i\) 连边,那么合法方案一定是形成了若干个简单环或自环 考虑一个环内的情况: 如果 \(a_i=p_i\),那么 \(i\) 向 \(a_i\) ...