IOS realizes bidirectional slider effect _ios

Source: Internet
Author: User
Tags uikit

Recently do the project, encountered a two-way slider, their realization a bit, casually write ideas, easy to develop later, to avoid duplication of code, paste on the line later. Encapsulated, the code is as follows:

#import <UIKit/UIKit.h>

typedef nsstring* (^hldoubleslideviewswitchstrbock) (cgfloat count);

@interface Hldoubleslideview:uiview

@property (nonatomic,assign) cgfloat maxValue;
@property (nonatomic,assign) cgfloat minvalue;
@property (nonatomic,assign) cgfloat currentleftvalue;
@property (nonatomic,assign) cgfloat currentrightvalue;

Formatted display text
@property (nonatomic,copy) Hldoubleslideviewswitchstrbock block;

@end

The source files are as follows:

#import "HLDoubleSlideView.h" #import "Uiview+add.h" @interface Hldoubleslideview () <uigesturerecognizerdelegate
> @property (nonatomic,strong) Uiimageview *leftimageview;
@property (Nonatomic,strong) Uiimageview *rightimageview;
@property (Nonatomic,strong) Uilabel *leftlabel;

@property (Nonatomic,strong) Uilabel *rightlabel;
@property (Nonatomic,strong) UIButton *leftbtn;

@property (Nonatomic,strong) UIButton *rightbtn;
@property (nonatomic,assign) cgfloat leftbtnorgx;

@property (nonatomic,assign) cgfloat rightbtnorgx;
 @end @implementation Hldoubleslideview-(ID) init {if (self = [super init]) {[Self setupui];
return self;
 }-(void) setupui {_leftimageview = [[Uiimageview alloc] init];
 _leftimageview.image = [UIImage imagenamed:@ "Progressimage"];
 _leftimageview.frame = CGRectMake (0, 5, 60, 40);

 [Self addsubview:_leftimageview];
 _leftlabel = [[Uilabel alloc] initwithframe:_leftimageview.bounds];
 _leftlabel.backgroundcolor = [Uicolor Clearcolor]; _leftlabel.font = [UifonT systemfontofsize:13];
 _leftlabel.textalignment = Nstextalignmentcenter;
 _leftlabel.textcolor = [Uicolor Whitecolor];

 [_leftimageview Addsubview:_leftlabel];
 _rightimageview = [[Uiimageview alloc] init];
 _rightimageview.image = [UIImage imagenamed:@ "Progressimage"];
 _rightimageview.frame = CGRectMake (0, 5, 60, 40);

 [Self addsubview:_rightimageview];
 _rightlabel = [[Uilabel alloc] initwithframe:_rightimageview.bounds];
 _rightlabel.backgroundcolor = [Uicolor Clearcolor];
 _rightlabel.font = [Uifont systemfontofsize:13];
 _rightlabel.textalignment = Nstextalignmentcenter;
 _rightlabel.textcolor = [Uicolor Whitecolor];

 [_rightimageview Addsubview:_rightlabel];
 _LEFTBTN = [UIButton buttonwithtype:uibuttontypecustom];
 _leftbtn.frame = CGRectMake (0, 50, 20,20);
 _leftbtn.backgroundcolor = [Uicolor Bluecolor];
 _leftbtn.layer.cornerradius = 10;
 [Self addsubview:_leftbtn]; Uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector(tapgestureaction:)];
 Pangesture.delegate = self;

 [_leftbtn Addgesturerecognizer:pangesture];

 _leftimageview.centerx = _leftbtn.centerx;
 _RIGHTBTN = [UIButton buttonwithtype:uibuttontypecustom];
 _rightbtn.backgroundcolor = [Uicolor Bluecolor];
 _rightbtn.frame = CGRectMake (240, 50, 20, 20);
 _rightbtn.layer.cornerradius = 10;
 Pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (tapgestureaction:)];
 Pangesture.delegate = self;
 [_rightbtn Addgesturerecognizer:pangesture];


 _rightimageview.centerx = _rightbtn.centerx;

[Self addsubview:_rightbtn];

}-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) Gesturerecognizer {return YES;}
 -(uiview*) HitTest: (cgpoint) point withevent: (Uievent *) event {NSLog (@ "Doubleview hittest");
return [Super Hittest:point withevent:event];
 }-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {NSLog (@ "began");
[Super Touchesbegan:touches Withevent:event]; }-(void) touchesMoved: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {NSLog (@ "move");
[Super Touchesmoved:touches Withevent:event];  }-(void) Layoutsubviews {cgfloat Centenx = (_currentleftvalue-_minvalue) * (self.bounds.size.width-20)/(_maxValue
 -_minvalue) + 10;

 _leftbtn.centerx = Centenx; if (_currentleftvalue!= 0) {cgfloat Centenx = (_currentrightvalue-_minvalue) * (self.bounds.size.width-20)/(_max
  Value-_minvalue) + 10;


 _rightbtn.centerx = Centenx;

 else {_rightbtn.centerx = self.bounds.size.width-10;
 } _leftimageview.centerx = _leftbtn.centerx;
 _rightimageview.centerx = _rightbtn.centerx;
  if (_block) {_leftlabel.text = _block (_currentleftvalue);
 _rightlabel.text = _block (_currentrightvalue);

 }-(void) Tapgestureaction: (uipangesturerecognizer*) pangesture {UIView *vw = Pangesture.view;
 Cgpoint transpoint = [Pangesture translationinview:self];

 NSLog (@ "X:%lf,y:%lf", TRANSPOINT.X,TRANSPOINT.Y); Switch (pangesture.state) {CAsE Uigesturerecognizerstatebegan: {if ([VW isequal:_leftbtn]) {_leftbtnorgx = _LEFTBTN.ORGX;

   NSLog (@ "Drag left button");
    else if ([VW isequal:_rightbtn]) {_rightbtnorgx = _RIGHTBTN.ORGX;
   NSLog (@ "Drag the right button");
  }} break;
    Case uigesturerecognizerstatechanged: {if ([VW isequal:_leftbtn]) {cgfloat Orginx = _LEFTBTN.ORGX;
    _LEFTBTN.ORGX = _leftbtnorgx + transpoint.x;
    if (_leftbtn.orgx < 0) {_leftbtn.orgx = 0;
    else if (_leftbtn.orgx >= _rightbtn.orgx-20) {_leftbtn.orgx = Orginx;
   } _leftimageview.centerx = _leftbtn.centerx;
    else if ([VW isequal:_rightbtn]) {cgfloat Orginx = _RIGHTBTN.ORGX;
    _RIGHTBTN.ORGX = _rightbtnorgx + transpoint.x;
    if (_rightbtn.orgx >= self.bounds.size.width-20) {_rightbtn.orgx = self.bounds.size.width-20;
    else if (_rightbtn.orgx <= _leftbtn.orgx +) {_rightbtn.orgx = Orginx; } _rightimageview.centerx = _rIghtbtn.centerx;
  }} break;

  Case uigesturerecognizerstateended: {} break;
 Default:break;
 } _currentleftvalue = _minvalue + (_maxvalue-_minvalue) * ((_LEFTBTN.CENTERX-10)/(self.bounds.size.width-20));
 _currentrightvalue = _minvalue + (_maxvalue-_minvalue) * ((_RIGHTBTN.CENTERX-10)/(self.bounds.size.width-20));
  if (_block) {_leftlabel.text = _block (_currentleftvalue);
 _rightlabel.text = _block (_currentrightvalue);

 } NSLog (@ "Leftvalue:%lf,rightvalue:%lf", _currentleftvalue,_currentrightvalue);
[Self setneedsdisplay];
 }-(void) Setcurrentleftvalue: (cgfloat) currentleftvalue {_currentleftvalue = Currentleftvalue;
 CGFloat Centenx = (Currentleftvalue-_minvalue) * (self.bounds.size.width-20)/(_maxvalue-_minvalue) + 10;
 _leftbtn.centerx = Centenx;
[Self setneedsdisplay];
 }-(void) Setcurrentrightvalue: (cgfloat) currentrightvalue {_currentrightvalue = Currentrightvalue; CGFloat Centenx = (_currentrightvalue-_minvalue) * (self. bounds.size.width-20)/(_maxvalue-_minvalue) + 10;
 _rightbtn.centerx = Centenx;


[Self setneedsdisplay];
 }-(void) DrawRect: (cgrect) Rect {cgcontextref context = Uigraphicsgetcurrentcontext ();
 Cgcontextsetlinecap (context, kcglinecapround);
 Cgcontextsetlinewidth (context, 3);
 [[Uicolor Graycolor] setstroke];
 Cgcontextmovetopoint (context, 0, 60);
 Cgcontextaddlinetopoint (context, self.bounds.size.width, 60);

 Cgcontextstrokepath (context);
 [[Uicolor Redcolor] setstroke];
 Cgcontextmovetopoint (context, _LEFTBTN.ORGX + 10, 60);
 Cgcontextaddlinetopoint (context, _rightbtn.orgx,60);

Cgcontextstrokepath (context);
 } @end


Use the following:

 Hldoubleslideview *doubleslideview = [[Hldoubleslideview alloc] init];
 Doubleslideview.backgroundcolor = [Uicolor whitecolor];//hlcolor (244, 244, 244);
 Doubleslideview.minvalue = 1000;
 Doubleslideview.maxvalue = 10000;
 Doubleslideview.block = ^nsstring* (cgfloat count)
 {return
  [nsstring stringwithformat:@ '%.0f yuan ', Count];
 [Self.view Addsubview:doubleslideview];

 Doubleslideview.frame = CGRectMake (.)

 Doubleslideview.currentleftvalue = 1200;
 Doubleslideview.currentrightvalue = 10000;

The results of the operation are as follows:


Demo:https://github.com/jiangtaidi/hldoubleslideview.git

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.