服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现双向滑动条效果

iOS实现双向滑动条效果

2021-01-11 16:52jiangamh IOS

这篇文章主要为大家详细介绍了iOS实现双向滑动条效果的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

?
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

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

延伸 · 阅读

精彩推荐
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28