iOS自定義雙向slider

1.創(chuàng)建一個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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末秕岛,一起剝皮案震驚了整個濱河市境钟,隨后出現的幾起案子,更是在濱河造成了極大的恐慌鸯匹,老刑警劉巖雾消,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件灾搏,死亡現場離奇詭異,居然都是意外死亡立润,警方通過查閱死者的電腦和手機狂窑,發(fā)現死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來桑腮,“玉大人泉哈,你說我怎么就攤上這事〉降” “怎么了旨巷?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長添忘。 經常有香客問我采呐,道長,這世上最難降的妖魔是什么搁骑? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任斧吐,我火速辦了婚禮,結果婚禮上仲器,老公的妹妹穿的比我還像新娘煤率。我一直安慰自己,他們只是感情好乏冀,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布蝶糯。 她就那樣靜靜地躺著,像睡著了一般辆沦。 火紅的嫁衣襯著肌膚如雪昼捍。 梳的紋絲不亂的頭發(fā)上识虚,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機與錄音妒茬,去河邊找鬼担锤。 笑死,一個胖子當著我的面吹牛乍钻,可吹牛的內容都是我干的肛循。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼银择,長吁一口氣:“原來是場噩夢啊……” “哼多糠!你這毒婦竟也來了?” 一聲冷哼從身側響起浩考,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤熬丧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后怀挠,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡害捕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年绿淋,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尝盼。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡吞滞,死狀恐怖,靈堂內的尸體忽然破棺而出盾沫,到底是詐尸還是另有隱情裁赠,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布佩捞,位于F島的核電站,受9級特大地震影響蕾哟,放射性物質發(fā)生泄漏一忱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一谭确、第九天 我趴在偏房一處隱蔽的房頂上張望帘营。 院中可真熱鬧,春花似錦逐哈、人聲如沸芬迄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽禀梳。三九已至杜窄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間出皇,已是汗流浹背羞芍。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留郊艘,地道東北人荷科。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像纱注,于是被迫代替她去往敵國和親畏浆。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內容

  • Quartz2D以及drawRect的重繪機制字數1487 閱讀21 評論1 喜歡1一狞贱、什么是Quartz2D Q...
    PurpleWind閱讀 773評論 0 3
  • iOS開發(fā)過程中刻获,使用的一些常用宏定義 字符串是否為空#define kStringIsEmpty(str) ([...
    goyohol閱讀 5,356評論 30 85
  • 我喜歡你卻不能跟你表白,Goodnight瞎嬉。
    給我一個解脫閱讀 159評論 1 0
  • day8 思想蝎毡、行為、信息以及產品常常會像傳染病暴發(fā)一樣氧枣,迅速傳播蔓延沐兵。 個別人物法則:社會流行浪潮是由屈指可數的...
    喵皇后閱讀 122評論 0 0
  • 這幾天老聽到我隔壁的鄰居逗我兒子說:帥帥烧董,馬上母親節(jié)了毁靶,你給媽媽準備了什么禮物啊逊移!我才知道母親節(jié)快到了预吆,大約像我這...
    美玉石閱讀 480評論 0 0