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

小编给大家分享一下iOS如何自定义水平滚动条、进度条,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联客户idc服务中心,提供眉山联通机房、成都服务器、成都主机托管、成都双线服务器等业务的一站式服务。通过各地的服务中心,我们向成都用户提供优质廉价的产品以及开放、透明、稳定、高性价比的服务,资深网络工程师在机房提供7*24小时标准级技术保障。

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

下面贴上核心代码:

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

HWSlider *slider = [[HWSlider alloc] initWithFrame:CGRectMake(10, 50, 300, 75)];[self.view addSubview:slider];

HWSlider:

#import @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

以上是“iOS如何自定义水平滚动条、进度条”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享题目:iOS如何自定义水平滚动条、进度条
网站URL:http://hbruida.cn/article/pchspe.html