Objective-C 自定义渐变色Slider

2024-07-16 1713阅读

文章目录

    • 一、前情概要
    • 二、具体实现

      一、前情概要

      系统提供UISlider,但在开发过程中经常需要自定义,本次需求内容是实现一个拥有渐变色的滑动条,且渐变色随着手指touch的位置不同改变区域,类似如下

      Objective-C 自定义渐变色Slider

      可以使用CAGradientLayer实现渐变效果,但是发现手指滑动的快时会有不跟手的情况。我们可以重写左侧有渐变色的UIView的drawRect: 方法来绘制渐变色

      二、具体实现

      左侧的渐变色UIView

      HLProgressView.h

      @interface HLProgressView : UIView
      @property (nonatomic, assign) CGSize viewSize;
      @end
      

      HLProgressView.m

      @implementation HLProgressView
      - (instancetype)initWithFrame:(CGRect)frame{
          self = [super initWithFrame:frame];
          if (self) {
          }
          return self;
      }
      - (void)setViewSize:(CGSize)viewSize {
          _viewSize = viewSize;
          self.frame = CGRectMake(0, 0, viewSize.width, viewSize.height);
          // setNeedsDisplay会自动调用drawRect方法
          [self setNeedsDisplay];
      }
          
      - (void)drawRect:(CGRect)rect {
          CGSize size = self.bounds.size;
          // 获取图形上下文对象CGContextRef
          CGContextRef context = UIGraphicsGetCurrentContext();
          // 创建一个颜色空间
          CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
          // 设置的颜色 每四个元素表示一种颜色,值的范围0~1,分别表示R、G、B、透明度 
          CGFloat colors[] = {
              55.0/255.0, 180.0/255.0, 255.0/255.0, 1.0,
              55.0/255.0, 80.0/255.0, 255.0/255.0, 1.0
          };
          // 渐变的位置信息范围0~1 0表示开始的位置 1表示结束的位置
          CGFloat gradientLocations[] = {0, 1};
          // 渐变的个数
          size_t locationCount = 2;
          // 创建渐变
          CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, gradientLocations, locationCount);
          // 指定渐变的开始位置和结束位置 这里设置完效果是整块区域的水平方向的渐变
          CGPoint gradientStartPoint = CGPointMake(0, size.height/2);
          CGPoint gradientEndPoint = CGPointMake(size.width, size.height/2);
          // 将渐变画到上下文中,最后一个参数表示发散的方式
          CGContextDrawLinearGradient(context, gradient, gradientStartPoint, gradientEndPoint, kCGGradientDrawsBeforeStartLocation);
          // 释放内存
          CGGradientRelease(gradient);
          CGColorSpaceRelease(colorSpace);
      }
      @end
      

      滑动条

      UICustomSlider.h

      @interface UICustomSlider : UIView
      @end
      

      UICustomSlider.m

      @interface UICustomSlider ()
      @property (nonatomic, strong) HLProgressView *progressView;
      @end
      @implementation UICustomSlider
      - (instancetype)initWithFrame:(CGRect)frame {
          self = [super initWithFrame:frame];
          if (self) {
              self.backgroundColor = [UIColor grayColor];
              self.clipsToBounds = YES; //不显示超过父视图的内容
              self.layer.cornerRadius = 8;
              self.progressView = [[HLProgressView alloc] initWithFrame:CGRectMake(0, 0, 140, 44)];
              [self addSubview:self.progressView];
          }
          return self;
      }
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
          CGPoint point = [touches.anyObject locationInView:self];
          self.progressView.viewSize = CGSizeMake(point.x, self.bounds.size.height);
      }
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
          CGPoint point = [touches.anyObject locationInView:self];
          self.progressView.viewSize = CGSizeMake(point.x, self.bounds.size.height);
      }
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
          CGPoint point = [touches.anyObject locationInView:self];
          self.progressView.viewSize = CGSizeMake(point.x, self.bounds.size.height);
      }
      @end
      

      调用滑动条

      ViewController.m

      #import "GradientSlider.h"
      @interface ViewController ()
      @end
      @implementation ViewController
      - (void)viewDidLoad {
          [super viewDidLoad];
          UICustomSlider *customSlider = [[UICustomSlider alloc] initWithFrame:CGRectMake(20, 100, 280, 44)];
          [self.view addSubview:customSlider];
      }
      
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]