zoukankan      html  css  js  c++  java
  • 定制选择范围的按钮RangeButton

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

    重点需要注意的地方:

  • 相关阅读:
    借贷宝什么鬼 砸钱推广是妙招还是险棋
    div+css 怎么让一个小div在另一个大div里面 垂直居中
    php重新整理数组索引
    JS 得细心的坑位
    chrome表单自动填充导致input文本框背景变成偏黄色问题解决
    phpstorm配置xdebug
    MySQLi基于面向对象的编程
    PHP中开启gzip压缩的2种方法
    SVN创建主干,分支、合并分支
    懒加载和预加载【转载】
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4062231.html
Copyright © 2011-2022 走看看