UISlider with ProgressView combined

后端 未结 9 1396
无人共我
无人共我 2020-11-29 16:07

Is there an apple-house-made way to get a UISlider with a ProgressView. This is used by many streaming applications e.g. native quicktimeplayer or youtube. (Just to be sure:

9条回答
  •  囚心锁ツ
    2020-11-29 17:09

    Here is a solution in Objective C. https://github.com/abhimuralidharan/BufferSlider

    The idea is to create a UIProgressview as a property in the UISlider subclass and add the required constraints programatically.

    #import  //.h file
    
    @interface BufferSlider : UISlider
    @property(strong,nonatomic) UIProgressView *bufferProgress;
    @end
    
    #import "BufferSlider.h" //.m file
    
    @implementation BufferSlider
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setup];
        }
        return self;
    }
    -(void)setup {
        self.bufferProgress = [[UIProgressView alloc] initWithFrame:self.bounds];
        self.minimumTrackTintColor = [UIColor redColor];
        self.maximumTrackTintColor = [UIColor clearColor];
        self.value = 0.2;
        self.bufferProgress.backgroundColor = [UIColor clearColor];
        self.bufferProgress.userInteractionEnabled = NO;
        self.bufferProgress.progress = 0.7;
        self.bufferProgress.progressTintColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        self.bufferProgress.trackTintColor = [[UIColor lightGrayColor] colorWithAlphaComponent:2];
        [self addSubview:self.bufferProgress];
        [self setThumbImage:[UIImage imageNamed:@"redThumb"] forState:UIControlStateNormal];
        self.bufferProgress.translatesAutoresizingMaskIntoConstraints = NO;
    
        NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.bufferProgress attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:self.bufferProgress attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0.75];  // edit the constant value based on the thumb image
        NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.bufferProgress attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
    
        [self addConstraints:@[left,right,centerY]];
        [self sendSubviewToBack:self.bufferProgress];
    }
    - (instancetype)initWithCoder:(NSCoder *)coder
    {
        self = [super initWithCoder:coder];
        if (self) {
            [self setup];
        }
        return self;
    }
    @end
    

提交回复
热议问题