1.创建一个ViewController控制器
2.ViewController.h 文件中声明一些必要的属性
3.具体在ViewController.m 中实现
4.上代码
ViewController.h中
//ViewController.h
//CustomSlider
//
//Created by lixiang on 2017/3/31.
//Copyright © 2017年lixiang. All rights reserved.
//
#import
#define SCREENW [UIScreen mainScreen].bounds.size.width
#define SCREENH [UIScreen mainScreen].bounds.size.height
#define PRICEBGW271.0
#define PRICEBGH21.0
#define PRICEBGX (SCREENW - PRICEBGW)*0.5
#define PRICEBGY (SCREENH - PRICEBGH)*0.5
#define PRICEMAX (SCREENW*0.5+ PRICEBGW*0.44)
#define PRICEMIN (SCREENW*0.5- PRICEBGW*0.45)
#define NODE1(PRICEBGX +103)
@interfaceViewController :UIViewController
@property(nonatomic,strong)UIImageView*leftSlideImageView;
@property(nonatomic,strong)UIImageView*rightSlideImageView;
@property(nonatomic,strong)UIView* pmgressbarView;
@property(nonatomic,strong)UILabel* resultLabel;
@property(nonatomic,assign)CGFloatleftValue;
@property(nonatomic,assign)CGFloatrightValue;
@end
ViewController.m中
//ViewController.m
//CustomSlider
//
//Created by lixiang on 2017/3/31.
//Copyright © 2017年lixiang. All rights reserved.
//
#import"ViewController.h"
@interfaceViewController()
@end
@implementationViewController
@synthesizeleftSlideImageView;
@synthesizerightSlideImageView;
@synthesizepmgressbarView;
@synthesizeresultLabel;
@synthesizeleftValue;
@synthesizerightValue;
- (void)viewDidLoad {
[superviewDidLoad];
leftValue=0;
rightValue=100;
[selfsetUpView];
}
-(void)setUpView
{
resultLabel= [[UILabelalloc]initWithFrame:CGRectMake(0,100,SCREENW,60)];
[resultLabelsetTextAlignment:NSTextAlignmentCenter];
[resultLabelsetFont:[UIFontsystemFontOfSize:18]];
resultLabel.backgroundColor=[UIColorcyanColor];
[self.viewaddSubview:resultLabel];
//进度条背景图片
UIImageView*priceBg = [[UIImageViewalloc]initWithFrame:CGRectMake(PRICEBGX,PRICEBGY,PRICEBGW,PRICEBGH)];
[priceBgsetImage:[UIImageimageNamed:@"priceBg"]];
[self.viewaddSubview:priceBg];
//进度条颜色
pmgressbarView= [[UIViewalloc]initWithFrame:CGRectMake(PRICEBGX,CGRectGetMaxY(priceBg.frame)-2,PRICEBGW,2.f)];
[pmgressbarViewsetBackgroundColor:[UIColorcolorWithRed:30.0/255.0green:144.0/255.0blue:255.0/255.0alpha:1.0]];
[self.viewaddSubview:pmgressbarView];
//左滑块
CGFloatcommonHandImageViewW =20.f;
CGFloatcommonHandImageViewH =25.f;
CGFloatleftHandImageViewX =PRICEBGX- commonHandImageViewW*0.5;
CGFloatleftHandImageViewY =PRICEBGY+ commonHandImageViewH;
leftSlideImageView= [[UIImageViewalloc]initWithFrame:CGRectMake(leftHandImageViewX, leftHandImageViewY, commonHandImageViewW, commonHandImageViewH)];
[leftSlideImageViewsetImage:[UIImageimageNamed:@"xiabashou"]];
[self.viewaddSubview:leftSlideImageView];
//右滑块
CGFloatrightHandImageViewX =CGRectGetMaxX(priceBg.frame) - commonHandImageViewW*0.5;
CGFloatrightHandImageViewY = leftHandImageViewY;
rightSlideImageView= [[UIImageViewalloc]initWithFrame:CGRectMake(rightHandImageViewX, rightHandImageViewY, commonHandImageViewW, commonHandImageViewH)];
[rightSlideImageViewsetImage:[UIImageimageNamed:@"xiabashou"]];
[self.viewaddSubview:rightSlideImageView];
//左滑块添加滑动手势
UIPanGestureRecognizer*leftPanRecognizer = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(leftSliderMove:)];
[leftPanRecognizersetMinimumNumberOfTouches:1];
[leftPanRecognizersetMaximumNumberOfTouches:1];
[leftSlideImageViewsetUserInteractionEnabled:YES];
[leftSlideImageViewaddGestureRecognizer:leftPanRecognizer];
//右滑块添加滑动手势
UIPanGestureRecognizer*rightPanRecognizer = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(rightSliderMove:)];
[rightSlideImageViewsetUserInteractionEnabled:YES];
[rightSlideImageViewaddGestureRecognizer:rightPanRecognizer];
}
-(void)leftSliderMove:(UIPanGestureRecognizer*)pan{
CGPointpoint = [pantranslationInView:leftSlideImageView];
CGFloatx =leftSlideImageView.center.x+ point.x;
if(x >PRICEMAX){
x =PRICEMAX;
}elseif(x
x =PRICEBGX;
}
leftValue= [selfx2price:ceilf(x)];
leftSlideImageView.center=CGPointMake(ceilf(x),leftSlideImageView.center.y);
if(rightValue-leftValue<=1) {
rightValue=leftValue+1;
rightSlideImageView.center=CGPointMake([selfprice2x:rightValue],rightSlideImageView.center.y);
}
[pansetTranslation:CGPointZeroinView:self.view];
[selfupdateData];
}
-(void)rightSliderMove:(UIPanGestureRecognizer*)pan{
CGPointpoint = [pantranslationInView:rightSlideImageView];
CGFloatx =rightSlideImageView.center.x+ point.x;
NSLog(@"x:%f",x);
if(x>PRICEBGX+PRICEBGW){
x =PRICEBGX+PRICEBGW;
}elseif(x
x =PRICEMIN;
}
rightValue= [selfx2price:ceilf(x)];
rightSlideImageView.center=CGPointMake(ceilf(x),rightSlideImageView.center.y);
if(rightValue-leftValue<=1) {
leftValue=rightValue-1;
leftSlideImageView.center=CGPointMake([selfprice2x:leftValue],leftSlideImageView.center.y);
}
[pansetTranslation:CGPointZeroinView:self.view];
[selfupdateData];
}
-(void)updateData{
[resultLabelsetText:[NSStringstringWithFormat:@"%.0f~%.0f",leftValue,rightValue]];
CGRectprogressRect =CGRectMake(leftSlideImageView.center.x,pmgressbarView.frame.origin.y,rightSlideImageView.center.x-leftSlideImageView.center.x,pmgressbarView.frame.size.height);
pmgressbarView.frame= progressRect;
resultLabel.backgroundColor=[UIColorcolorWithRed:arc4random_uniform(255)/255.0green:arc4random_uniform(255)/255.0blue:arc4random_uniform(255)/255.0alpha:1];
resultLabel.textColor=[UIColorcolorWithRed:arc4random_uniform(255)/255.0green:arc4random_uniform(255)/255.0blue:arc4random_uniform(255)/255.0alpha:1];
}
//坐标->数字
-(CGFloat)x2price:(CGFloat)x{
CGFloatprice =0.f;
NSLog(@"x=====%f",x);
//5-
if(x
price =0;
}
//5~25
elseif(x
price = (x -PRICEMIN) /120*20+5;
}
//25~40
elseif(x
price = (x -PRICEBGX-133) *0.5+25;
}
//40~100
elseif(x
price = (x -PRICEBGX-163) *2/3+40;
}
//100+
else{
price =100;
}
returnprice;
}
//数字->坐标
-(CGFloat)price2x:(CGFloat)price{
NSLog(@"price=====%f",price);
CGFloatx;
//<5
if(price<5) {
x =PRICEBGX;
}
//5~25
elseif(price >=5&& price <25) {
x = (price-5) *6+PRICEMIN;
}
//25~40
elseif(price >=25&& price <40) {
x = (price-25) *2+133+PRICEBGX;
}
//40~100
elseif(price >=40&& price <100){
x = (price-40) *3/2+163+PRICEBGX;
}elseif(price >=100){
x =PRICEBGX+PRICEBGW;
}
returnx;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end