服务器之家

服务器之家 > 正文

iOS实现双向滑动条效果

时间:2021-01-11 16:52     来源/作者:jiangamh

最近做项目,碰到一种双向滑动条,自己实现了一下,随便写一下思路,方便以后开发,避免重复写代码,以后粘贴就行了。封装了一下,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
 
//格式化显示文本
@property(nonatomic,copy)hldoubleslideviewswitchstrbock block;
 
@end

源文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#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) / (_maxvalue - _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(@"拖拽左边按钮");
 
   }
   else if([vw isequal:_rightbtn])
   {
    _rightbtnorgx = _rightbtn.orgx;
    nslog(@"拖拽右边按钮");
   }
 
  }
   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 + 20)
    {
     _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


使用如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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元",count];
};
[self.view addsubview:doubleslideview];
 
doubleslideview.frame = cgrectmake(60, 64, 250, 80);
 
doubleslideview.currentleftvalue = 1200;
doubleslideview.currentrightvalue = 10000;

运行结果如下:

iOS实现双向滑动条效果

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

以上就是本文的全部内容,希望对大家的学习有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部