带有标线的UISlider

最近在项目中需要一个带有标线的slider,并且滑块滑动后停止在标线位置。所以自己写了一个。
下面是代码,仅供参考。

自定义view 的头文件如下

@interface TSlider: UISlider

@end

@interface LTSliderView : UIView
@property (nonatomic,strong) UIColor *lineColor; // 默认黑色
@property (nonatomic,assign) CGFloat lineHeight;
@property (nonatomic,strong) TSlider *t_slider;  
@end

自定义view 的实现文件如下

#define TRACK_HEIGHT 3.0
#import "LTSliderView.h"

@implementation TSlider

-(CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
    
    rect.origin.x=rect.origin.x-15;
    rect.size.width=rect.size.width+30;
    return CGRectInset([super thumbRectForBounds:bounds trackRect:rect value:value],15,15);
}

-(CGRect)trackRectForBounds:(CGRect)bounds{
    
    bounds.origin.x=0;
//    bounds.origin.y=bounds.origin.y;
    bounds.origin.y=bounds.origin.y + bounds.size.height - TRACK_HEIGHT;
    bounds.size.height = TRACK_HEIGHT;
    bounds.size.width=bounds.size.width;
    return bounds;
}

@end

@implementation LTSliderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.lineHeight = 30;
        self.lineColor = [UIColor blackColor];
        [self createSliderWithFrame:frame];
    }
    return self;
}

- (void)createSliderWithFrame:(CGRect)frame{
    
    self.t_slider = [[TSlider alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    self.t_slider.minimumValue = 0;
    self.t_slider.maximumValue = 10;
    
    [self addSubview:self.t_slider];
    for (int i = 0; i < 11; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*self.t_slider.bounds.size.width/10, 0, 1, frame.size.height - TRACK_HEIGHT)];
        label.backgroundColor = self.lineColor;
        [self addSubview:label];
    }
    [self bringSubviewToFront:self.t_slider];
}

- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UILabel class]]){
            subView.backgroundColor = lineColor;
        }
    }
}
- (void)setLineHeight:(CGFloat)lineHeight{
    _lineHeight = lineHeight;
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UILabel class]]){
            CGRect rect = subView.frame;
            rect.origin.y = self.t_slider.bounds.size.height - lineHeight-TRACK_HEIGHT;
            rect.size.height = lineHeight;
            subView.frame = rect;
        }
    }
}

自定义view 的使用如下:

    self.t_slider = [[LTSliderView alloc] initWithFrame:CGRectMake(40, 300, [UIScreen mainScreen].bounds.size.width-80, 50)];
    self.t_slider.lineColor = [UIColor redColor];
    self.t_slider.lineHeight = 10.f;
    [self.t_slider.t_slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.t_slider];
  • 当然还有很多熟悉你可以写到自定义的头文件中,以便用户修改。
  • 还有一个需要注意的就是在valueChange的方法中如果你要给label 赋值的话选择异步。

有需要的可以了解一下。

---来自涛胖子的工作笔记

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,003评论 3 119
  • 女神何以为女神。 很多人的心中都有着一个自己羡慕的人吧,那个人一直在你的前方,最可怕的是这些人再自己很优秀的时候,...
    完美呐阅读 388评论 7 3
  • 在前面的一系列的文章中,我们依据津巴多先生的普通心理学,初步了解了心理学目前各大分支的发展现状和理论。但是作为一本...
    地平线上的背影阅读 3,826评论 0 3
  • 一篇醍醐灌顶的深度好文 作为一名在信息服务行业浸淫快一年的老(nen)司(tou)机(qing),感触颇深。在如今...
    Edwist阅读 436评论 0 1
  • 1. 什么是符合海底捞标准的人?标准很多,但原则很简单,就是不怕吃苦的好人。比如,海底捞的员工要诚实肯干,要能快速...
    何舒卉阅读 519评论 0 1