十五,UISlider的使用,分段,加粗等以及进度条的使用

皇甫高阳
2023-12-01

 1,在开发中很多时候会用到进度条,或者进度条等,比如视频播放进度,下载进度,图片上传进度等等这些下面把我遇到的几种进度条相似的方法说一下

2,加粗方法

(1)使用继承的重写方法。代码如下

#import <UIKit/UIKit.h>

@interface SliderCustom : UISlider

- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds;
- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds;
- (CGRect)trackRectForBounds:(CGRect)bounds;
//- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;

@end

#import "SliderCustom.h"

@implementation SliderCustom

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds
{
    return CGRectMake(0, 0, CGRectGetWidth(self.frame)/ 2, CGRectGetHeight(self.frame) / 2);
}
// 设置最小值
- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds
{
    return CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
}

// 控制slider的宽和高,这个方法才是真正的改变slider滑道的高的
- (CGRect)trackRectForBounds:(CGRect)bounds
{
    return CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
}
@end


(2) 使用CGAffineTransform改变UIProgressView的指定高度

CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 8.0f);
    _progressView.transform = transform;

3,一些比较好的第三方的介绍(个人看法)

首先是进度条第三方使用MBProgressHUD,可以有圆形进度条,加粗的长形进度条

首先是使用圆形进度条,导入文件,并声明

 @property (nonatomic, strong) MBProgressHUD *proHUD;
触发方法:

- (void)circularprogress{
    UIViewController *vc = [SPAlertMode getCurrentVC];
    //UI操作有时候会用后台线程,这里一律用主线程,避免报错
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [vc.view addSubview:_proHUD];
        
    });
    
    _proHUD = [[MBProgressHUD alloc]initWithView:vc.view];
    _proHUD.color = DB_Black;
    _proHUD.labelColor = DB_Blue;
    //        _proHUD.labelText = @"正在上传";
    
    _proHUD.mode = MBProgressHUDModeAnnularDeterminate;
//    _proHUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    [vc.view addSubview:_proHUD];
    [_proHUD showAnimated:YES whileExecutingBlock:^{
       
        NSProgress * sub =[NSProgress progressWithTotalUnitCount:100];
        int i=0;
        while (i<100) {
            i++;
            sub.completedUnitCount++;
        _proHUD.progress = sub.fractionCompleted;
        _proHUD.labelText = [NSString stringWithFormat:@"%.2f%@",sub.fractionCompleted*100,@"%"];
                        NSLog(@"%f",sub.fractionCompleted);
                        usleep(50000);
            
        }
    }];
 

}
  长兴进度条调用如下
- (void)uploading{
    UIViewController *vc = [SPAlertMode getCurrentVC];
    //UI操作有时候会用后台线程,这里一律用主线程,避免报错
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [vc.view addSubview:_proHUD];
        
    });
    
    _proHUD = [[MBProgressHUD alloc]initWithView:vc.view];
    
    //        _proHUD.labelText = @"正在上传";
    
//    _proHUD.mode = MBProgressHUDModeAnnularDeterminate;
    _proHUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    [vc.view addSubview:_proHUD];
    [_proHUD showAnimated:YES whileExecutingBlock:^{
       
        NSProgress * sub =[NSProgress progressWithTotalUnitCount:100];
        int i=0;
        while (i<100) {
            i++;
            sub.completedUnitCount++;
            _proHUD.progress = sub.fractionCompleted;
            _proHUD.labelText = [NSString stringWithFormat:@"%.2f%@",sub.fractionCompleted*100,@"%"];
            NSLog(@"%f",sub.fractionCompleted);
            usleep(50000);
            
        }
    }];
   
}

4,UISlider好用的第三方CCHStepSizeSlider(个人看法),UISlider在开发中可能会有用户需要节点或者分段来处理,这个第三方的都符合要求

具体使用如下:

CCHStepSizeSlider *slider2 = [[CCHStepSizeSlider alloc] initWithFrame:CGRectMake(20, 400, W - 40, 50)];
    //    slider2.backgroundColor = [UIColor grayColor];
    //    slider2.margin = 100;
    slider2.titleArray = @[@"",@"你好",@"好吗",@"好的"];
    slider2.lineWidth = 5;
    slider2.titleOffset = 23;
    //    slider2.titleColor = [UIColor blueColor];
    slider2.index = 3;
    slider2.stepWidth = 10;
    slider2.sliderOffset = -8;
    slider2.stepTouchRate = 2;
    slider2.thumbSize = CGSizeMake(20, 20);
    slider2.thumbTouchRate = 2;
    //    slider2.numberOfStep = 3;
    slider2.continuous = NO;
    [slider2 addTarget:self action:@selector(valueChangeAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:slider2];
    
}
- (void)valueChangeAction:(CCHStepSizeSlider *)sender {
    NSLog(@"sender :%@,value :%f,index :%ld",sender,sender.value,(long)sender.index);
    
}




 类似资料: