服务器之家

服务器之家 > 正文

iOS自定义水平滚动条、进度条

时间:2021-05-24 15:56     来源/作者:hero_wqb

ios自定义水平滚动条进度条,继承uiview,可点击轨道、滑动滑块交互。

先看一下效果图:

iOS自定义水平滚动条、进度条

简单说一下逻辑,新建一个继承uiview的类,分别给轨道、滑块添加uitapgesturerecognizer点击、uipangesturerecognizer滑动手势。获取偏移量,计算控件位置,刷新视图。

下面贴上核心代码:

显示视图,在控制器调用代码:

?
1
2
hwslider *slider = [[hwslider alloc] initwithframe:cgrectmake(10, 50, 300, 75)];
[self.view addsubview:slider];

hwslider:

?
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
#import <uikit/uikit.h>
 
@interface hwslider : uiview
 
@property (nonatomic, assign) nsinteger score;
 
@end
 
/*** ---------------分割线--------------- ***/
 
#import "hwslider.h"
#import "uiview+additions.h"
 
@interface hwslider ()
 
@property (nonatomic, weak) uiimageview *bubbleimage;
@property (nonatomic, weak) uiimageview *arrowimage;
@property (nonatomic, weak) uilabel *scorelabel;
@property (nonatomic, weak) uilabel *levellable;
@property (nonatomic, weak) uiview *trackview;
@property (nonatomic, weak) uiimageview *thumb;
 
@end
 
@implementation hwslider
 
- (instancetype)initwithframe:(cgrect)frame
{
 if (self = [super initwithframe:frame]) {
  _score = 10;
  self.backgroundcolor = [uicolor whitecolor];
  
  //气泡图片
  uiimageview *bubbleimage = [[uiimageview alloc] initwithframe:cgrectmake(self.bounds.size.width - 70, 0, 74, 35)];
  [bubbleimage setimage:[uiimage imagenamed:@"alert_teachereva_bubbleimage"]];
  [self addsubview:bubbleimage];
  _bubbleimage = bubbleimage;
  
  //分数标签
  uilabel *scorelabel = [[uilabel alloc] initwithframe:cgrectmake(self.bounds.size.width - 71.5, 0, 74, 28)];
  scorelabel.text = @"10";
  scorelabel.textcolor = [uicolor blackcolor];
  scorelabel.font = [uifont systemfontofsize:15.f];
  scorelabel.textalignment = nstextalignmentcenter;
  [self addsubview:scorelabel];
  _scorelabel = scorelabel;
  
  //气泡箭头
  uiimageview *arrowimage = [[uiimageview alloc] initwithframe:cgrectmake(self.bounds.size.width - 16.5, 26, 13, 13)];
  [arrowimage setimage:[uiimage imagenamed:@"alert_teachereva_arrowimage"]];
  [self addsubview:arrowimage];
  _arrowimage = arrowimage;
  
  //轨道可点击视图(轨道只设置了5pt,通过这个视图增加以下点击区域)
  uiview *tapview = [[uiview alloc] initwithframe:cgrectmake(0, 34, self.bounds.size.width, 20)];
  [tapview addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(handletap:)]];
  [self addsubview:tapview];
  
  //轨道背景
  uiview *backview = [[uiview alloc] initwithframe:cgrectmake(0, 7.5, self.bounds.size.width, 5)];
  backview.backgroundcolor = [uicolor graycolor];
  backview.layer.cornerradius = 2.5f;
  backview.layer.maskstobounds = yes;
  [tapview addsubview:backview];
  
  //轨道前景
  uiview *trackview = [[uiview alloc] initwithframe:cgrectmake(1.5, 9, self.bounds.size.width - 3, 2)];
  trackview.backgroundcolor = [uicolor greencolor];
  trackview.layer.cornerradius = 1.f;
  trackview.layer.maskstobounds = yes;
  [tapview addsubview:trackview];
  _trackview = trackview;
  
  //滑块
  uiimageview *thumb = [[uiimageview alloc] initwithframe:cgrectmake(self.bounds.size.width - 20, 34, 20, 20)];
  [thumb setimage:[uiimage imagenamed:@"alert_teachereva_sliderimg"]];
  thumb.userinteractionenabled = yes;
  thumb.contentmode = uiviewcontentmodecenter;
  [thumb addgesturerecognizer:[[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)]];
  [self addsubview:thumb];
  _thumb = thumb;
  
  //级别标签
  uilabel *levellable = [[uilabel alloc] initwithframe:cgrectmake(0, cgrectgetmaxy(thumb.frame) + 7, self.bounds.size.width, 13)];
  levellable.text = @"非常满意";
  levellable.textcolor = [uicolor blackcolor];
  levellable.font = [uifont systemfontofsize:13.f];
  levellable.textalignment = nstextalignmentcenter;
  [self addsubview:levellable];
  _levellable = levellable;
 }
 
 return self;
}
 
- (void)setscore:(nsinteger)score
{
 _score = score;
 
 //刷新视图
 [self reloadviewwiththumbceneterx:score / 10.0 * self.bounds.size.width];
}
 
//点击滑动条
- (void)handletap:(uitapgesturerecognizer *)sender
{
 //刷新视图
 [self reloadviewwiththumbceneterx:[sender locationinview:self].x];
}
 
//滑动滑块
- (void)handlepan:(uipangesturerecognizer *)sender
{
 //获取偏移量
 cgfloat movex = [sender translationinview:self].x;
 
 //重置偏移量,避免下次获取到的是原基础的增量
 [sender settranslation:cgpointmake(0, 0) inview:self];
 
 //计算当前中心值
 cgfloat centerx = _thumb.centerx + movex;
 
 //防止瞬间大偏移量滑动影响显示效果
 if (centerx < 10) centerx = 10;
 if (centerx > self.bounds.size.width - 10) centerx = self.bounds.size.width - 10;
 
 //刷新视图
 [self reloadviewwiththumbceneterx:centerx];
}
 
- (void)reloadviewwiththumbceneterx:(cgfloat)thumbceneterx
{
 //更新轨道前景色
 _trackview.framewidth = thumbceneterx;
 
 //更新滑块位置
 _thumb.centerx = thumbceneterx;
 if (_thumb.centerx < 10) {
  _thumb.centerx = 10;
 }else if (_thumb.centerx > self.bounds.size.width - 10) {
  _thumb.centerx = self.bounds.size.width - 10;
 }
 
 //更新箭头位置
 _arrowimage.centerx = _thumb.centerx;
 
 //更新气泡标签位置(气泡图片宽度74,实际内容宽度66)
 _bubbleimage.centerx = _thumb.centerx;
 if (_bubbleimage.centerx < 33) {
  _bubbleimage.centerx = 33;
 }else if (_bubbleimage.centerx > self.bounds.size.width - 33) {
  _bubbleimage.centerx = self.bounds.size.width - 33;
 }
 
 //更新分数标签位置
 _scorelabel.centerx = _bubbleimage.centerx;
 
 //分数,四舍五入取整
 _score = round(thumbceneterx / self.bounds.size.width * 10);
 
 //更新标签内容
 _scorelabel.text = [nsstring stringwithformat:@"%ld", _score];
 if (_score <= 3) {
  _levellable.text = @"极不满意";
 }else if (_score <= 5) {
  _levellable.text = @"不满意";
 }else if (_score <= 7) {
  _levellable.text = @"一般";
 }else if (_score <= 9) {
  _levellable.text = @"满意";
 }else if (_score == 10) {
  _levellable.text = @"非常满意";
 }
}
 
@end

demo 下载链接

猜你喜欢:自定义垂直滚动条,可与scrollview联动交互

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/hero_wqb/article/details/78965106

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部