In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to customize iOS horizontal scroll bar, progress bar, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
To talk about the logic briefly, create a new class that inherits UIView, and add UITapGestureRecognizer click and UIPanGestureRecognizer slide gestures to tracks and sliders, respectively. Get the offset, calculate the position of the control, and refresh the view.
Paste the core code below:
Display the view and call the code in the controller:
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/***-split Line-* * / # 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]; / / Bubble image 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; / / score label 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 / / Bubble Arrow 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 / / track clickable view (track only has 5pt set, and the following click area is added through this view) 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] / / track background 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]; / / track foreground 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; / / Slider 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 / level label UILabel * levelLable = [[UILabel alloc] initWithFrame:CGRectMake (0, CGRectGetMaxY (thumb.frame) + 7, self.bounds.size.width, 13)]; levelLable.text = @ "very satisfied"; 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 / / refresh view [self reloadViewWithThumbCeneterX:score / 10.0 * self.bounds.size.width];} / / Click the slider-(void) handleTap: (UITapGestureRecognizer *) sender {/ / Refresh view [self reloadViewWithThumbCeneterX: [sender locationInView:self] .x];} / / Slider-(void) handlePan: (UIPanGestureRecognizer *) sender {/ / get offset CGFloat moveX = [sender translationInView:self] .x / / reset the offset to avoid getting the increment of the original foundation next time [sender setTranslation:CGPointMake (0,0) inView:self]; / / calculate the current center value CGFloat centerX = _ thumb.centerX + moveX; / / prevent instantaneous large offset sliding from affecting the display effect if (centerX
< 10) centerX = 10; if (centerX >Self.bounds.size.width-10) centerX = self.bounds.size.width-10; / / Refresh view [self reloadViewWithThumbCeneterX:centerX];}-(void) reloadViewWithThumbCeneterX: (CGFloat) thumbCeneterX {/ / Update track foreground color _ trackView.frameWidth = thumbCeneterX; / / Update slider position _ 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;} / update arrow location _ arrowImage.centerX = _ thumb.centerX; / / update bubble label position (bubble picture width 74, actual content width 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;} / update score tag location _ scoreLabel.centerX = _ bubbleImage.centerX; / / score, rounded to _ score = round (thumbCeneterX / self.bounds.size.width * 10); / / update tag content _ scoreLabel.text = [NSString stringWithFormat:@ "% ld", _ score]; if (_ score)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.