美文网首页
iOS 自定义长方形进度条

iOS 自定义长方形进度条

作者: New_卡迪熊 | 来源:发表于2022-11-18 17:51 被阅读0次

    由于工作需要,做一个拍照显示的进度条;样式铺满整个屏幕

    自定义view:

    h文件

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface TakePhotoProgress : UIView
    @property(assign,nonatomic)CGFloat progress;
    @end
    
    NS_ASSUME_NONNULL_END
    

    m文件

    #import "TakePhotoProgress.h"
    
    @interface TakePhotoProgress()
    
    @property (nonatomic, strong) UILabel *progressLabel;
    
    @end
    
    @implementation TakePhotoProgress
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    
    self.backgroundColor = [UIColor clearColor];
    
    self.progressLabel = ({
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2 - 100, self.frame.size.height/2 - 25, 200, 50)];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    label;
    });
    [self addSubview:self.progressLabel];
    
    
    }
    return self;
    }
    
    
    - (void)drawRect:(CGRect)rect {
    //    定义扇形中心
    CGPoint origin = CGPointMake(kScreenWidth/2 +50, kScreenheight/2);
    //    定义扇形半径
    CGFloat radius = kScreenWidth/2 +100;
    
    //    设定扇形起点位置
    CGFloat startAngle = - M_PI_2;
    //    根据进度计算扇形结束位置
    CGFloat endAngle = startAngle + self.progress * M_PI * 2;
    
    //    根据起始点、原点、半径绘制弧线
    UIBezierPath *sectorPath = [UIBezierPath bezierPathWithArcCenter:origin radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    
    //    从弧线结束为止绘制一条线段到圆心。这样系统会自动闭合图形,绘制一条从圆心到弧线起点的线段。
    [sectorPath addLineToPoint:origin];
    
    //    设置扇形的填充颜色
    [RGBCOLOR(0, 0, 0, 0.5) set];
    
    //    设置扇形的填充模式
    [sectorPath fill];
    }
    
    
    //重写progress的set方法,可以在赋值的同时给label赋值
    - (void)setProgress:(CGFloat)progress{
    _progress = progress;
    
    //    对label进行赋值
    //    self.progressLabel.text = [NSString stringWithFormat:@"图片接收中%0.2f%%",progress * 100];
    self.progressLabel.text = [NSString stringWithFormat:@"%@ %0.2f%%",NSLocalizedString(@"图片接收中:", nil),progress * 100];
    [self setNeedsDisplay];
    }
    
    @end
    

    接下来就是调用了

    在需要调用的方法直接引用


    image.png
    -(void)takePhotoProgress{
    
    if(!_PhotoProgress){
    _PhotoProgress = [[TakePhotoProgress alloc] initWithFrame:CGRectMake(-50, 0, kScreenWidth +100, kScreenheight)];
    [self. view addSubview:_PhotoProgress];
    [self startTimer];
    }
    
    
    }
    

    定时器

    #pragma mark - 定时器
    
    - (NSTimer *)Ttimer
    {
    if (_Ttimer == nil) {
    _Ttimer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(progressChange) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_Ttimer forMode:NSRunLoopCommonModes];   //防止计时器卡顿等其它现象
    }
    return _Ttimer;
    }
    
    - (void)startTimer
    {
    self. takeprogress = 0.0;
    [self. Ttimer setFireDate:[NSDate distantPast]];  //开启定时器
    
    }
    -(void)stopTimer{
    
    [_Ttimer invalidate];  //销毁
    _Ttimer=nil;
    
    }
    
    - (void)progressChange
    {
    self. takeprogress += 0.025;
    NSLog(@"1 progress = %f", self.takeprogress);
    
    if (self. takeprogress >= 1) {
    [self stopTimer];
    if(_PhotoProgress){
    [_PhotoProgress removeFromSuperview];
    _PhotoProgress =nil;
    
    }
    return;
    }
    _PhotoProgress. progress = self.takeprogress;
    }
    

    欢迎评论区交流。

    相关文章

      网友评论

          本文标题:iOS 自定义长方形进度条

          本文链接:https://www.haomeiwen.com/subject/lgcvxdtx.html