最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Objective-C 自定義漸變色Slider的實現(xiàn)方法

 更新時間:2024年07月16日 09:56:58   作者:富春山居_ZYY(已黑化)  
系統(tǒng)提供UISlider,但在開發(fā)過程中經(jīng)常需要自定義,本次需求內(nèi)容是實現(xiàn)一個擁有漸變色的滑動條,且漸變色隨著手指touch的位置不同改變區(qū)域,這篇文章主要介紹了Objective-C 自定義漸變色Slider,需要的朋友可以參考下

一、前情概要

系統(tǒng)提供UISlider,但在開發(fā)過程中經(jīng)常需要自定義,本次需求內(nèi)容是實現(xiàn)一個擁有漸變色的滑動條,且漸變色隨著手指touch的位置不同改變區(qū)域,類似如下

可以使用CAGradientLayer實現(xiàn)漸變效果,但是發(fā)現(xiàn)手指滑動的快時會有不跟手的情況。我們可以重寫左側(cè)有漸變色的UIView的drawRect: 方法來繪制漸變色

二、具體實現(xiàn)

左側(cè)的漸變色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會自動調(diào)用drawRect方法
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
    CGSize size = self.bounds.size;
    // 獲取圖形上下文對象CGContextRef
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 創(chuàng)建一個顏色空間
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 設(shè)置的顏色 每四個元素表示一種顏色,值的范圍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表示結(jié)束的位置
    CGFloat gradientLocations[] = {0, 1};
    // 漸變的個數(shù)
    size_t locationCount = 2;
    // 創(chuàng)建漸變
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, gradientLocations, locationCount);
    // 指定漸變的開始位置和結(jié)束位置 這里設(shè)置完效果是整塊區(qū)域的水平方向的漸變
    CGPoint gradientStartPoint = CGPointMake(0, size.height/2);
    CGPoint gradientEndPoint = CGPointMake(size.width, size.height/2);
    // 將漸變畫到上下文中,最后一個參數(shù)表示發(fā)散的方式
    CGContextDrawLinearGradient(context, gradient, gradientStartPoint, gradientEndPoint, kCGGradientDrawsBeforeStartLocation);
    // 釋放內(nèi)存
    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; //不顯示超過父視圖的內(nèi)容
        self.layer.cornerRadius = 8;
        self.progressView = [[HLProgressView alloc] initWithFrame:CGRectMake(0, 0, 140, 44)];
        [self addSubview:self.progressView];
    }
    return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [touches.anyObject locationInView:self];
    self.progressView.viewSize = CGSizeMake(point.x, self.bounds.size.height);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [touches.anyObject locationInView:self];
    self.progressView.viewSize = CGSizeMake(point.x, self.bounds.size.height);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [touches.anyObject locationInView:self];
    self.progressView.viewSize = CGSizeMake(point.x, self.bounds.size.height);
}
@end

調(diào)用滑動條
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];
}

到此這篇關(guān)于Objective-C 自定義漸變色Slider的文章就介紹到這了,更多相關(guān)Objective-C 自定義漸變色內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

山阴县| 台中县| 探索| 内丘县| 灵山县| 东至县| 项城市| 牡丹江市| 陈巴尔虎旗| 尚义县| 林口县| 萨迦县| 崇州市| 永年县| 社会| 铜鼓县| 丰原市| 广平县| 辽中县| 芜湖市| 镇康县| 托克逊县| 且末县| 共和县| 阳新县| 四川省| 鄢陵县| 徐水县| 措美县| 丹巴县| 汾西县| 绥宁县| 微山县| 甘谷县| 大新县| 巴马| 兴安县| 敖汉旗| 青冈县| 通海县| 信宜市|