美文网首页
iOS 半圆弧进度条

iOS 半圆弧进度条

作者: StevenHu_Sir | 来源:发表于2021-10-28 15:59 被阅读0次

1.效果图

image.png

2.代码

.h

//  Created by 胡锦涛 on 2021/10/28.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ArcLineView : UIView
//起始分值
- (void)setStarScore:(CGFloat)starScore title:(NSString *)title;
@end

NS_ASSUME_NONNULL_END

.m

//
//  ArcLineView.m
//  Decorate
//
//  Created by 胡锦涛 on 2021/10/28.
// 初始化只需设置分数(0~1)即可

#import "ArcLineView.h"
#import "PunchCard.h"
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)//角都转弧度

#define ANGLE 20 //没份20度 共220度


@interface ArcLineView()
///  <#标题#>
@property (nonatomic, strong) UILabel * scoreLabel ;
/// <#数据源#>
@property (nonatomic, strong) NSMutableArray * labelArray ;
/// <#数据源#>
@property (nonatomic, strong) NSMutableArray * titleArray ;
/// 标题
@property (nonatomic, strong) NSString * title ;
@end

@implementation ArcLineView
{
    CGFloat _starScore ;
}
- (void)drawRect:(CGRect)rect {
    self.backgroundColor = [UIColor whiteColor];
    //设置圆弧的半径
    CGFloat radius =  rect.size.width/2;
    //设置圆弧的圆心
    CGPoint center = CGPointMake(radius, radius);

    //背景线条
    CGFloat startAngleBag = DEGREES_TO_RADIANS(165);
    CGFloat endAngleBag = DEGREES_TO_RADIANS(165) + DEGREES_TO_RADIANS(210);
    UIBezierPath *pathBag = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 6 startAngle:startAngleBag endAngle:endAngleBag clockwise:YES];
    pathBag.lineWidth = 12;
    pathBag.lineCapStyle = kCGLineJoinRound;
    [[UIColor lightGrayColor] set];
    [pathBag stroke];
    //显示的进度条
    
    CGFloat startAngle = DEGREES_TO_RADIANS(165);
    CGFloat endAngle = DEGREES_TO_RADIANS(165) + DEGREES_TO_RADIANS(210)*_starScore;
    if (_starScore >= 0.6) {
        
        endAngle = DEGREES_TO_RADIANS(165) + DEGREES_TO_RADIANS(210)*(_starScore-0.5)*2;
    } else {
        endAngle = DEGREES_TO_RADIANS(165) + DEGREES_TO_RADIANS(210)*(_starScore/0.6)*0.2;
        
    }
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 6 startAngle:startAngle endAngle:endAngle clockwise:YES];
    
    path.lineWidth = 12;
    
    path.lineCapStyle = kCGLineJoinRound;
    
    [HKHexString(@"#2e99a4") set];
    
    [path stroke];
    
    
    
    //创建文字说明
    
    if (!self.labelArray.count) {
        
        for (int i = 0; i < self.titleArray.count; i++) {
            
            [self creatLabel:self.titleArray[i] withScore:i];
            
        }
        
        self.scoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        
        self.scoreLabel.center = CGPointMake(center.x, center.y-10);
        
        self.scoreLabel.textAlignment = NSTextAlignmentCenter;
        
        self.scoreLabel.font = [UIFont systemFontOfSize:27];
        
        self.scoreLabel.text = _title == nil || [_title isEqualToString:@""] ? @"20 / 26" : _title;
        
        self.scoreLabel.textColor = HKHexString(@"#222222");
        
        [self addSubview:self.scoreLabel];
        
        
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 110, 50)];
        
        label.center = CGPointMake(self.scoreLabel.center.x, center.y+20);
        
        label.text = @"打卡人数 / 应到人数";
        
        label.font = [UIFont systemFontOfSize:12];
        
        label.textColor = HKHexString(@"#666666");
        
        label.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:label];
        
    }
    
}




//起始分值

- (void)setStarScore:(CGFloat)starScore title:(NSString *)title {
    
    _starScore = starScore;
    _title = title;
    //当下载进度改变时,手动调用重绘方法
    [self setNeedsDisplay];
}




//创建文字说明 label

- (void)creatLabel:(NSString *)title withScore:(CGFloat)index{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 23, 15)];
    
    label.font = [UIFont systemFontOfSize:10];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.hidden = YES;
    
    label.text = title;
    
    label.textColor = [UIColor blackColor];
    
    [self addSubview:label];
    
    CGFloat endAngle = index*ANGLE-200+10;
    
    label.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(endAngle)+M_PI_2);//label 旋转
    
    
    
    CGSize size = self.frame.size;
    
    CGFloat  centerY = size.width/2 - (size.width/2-20)*sin(DEGREES_TO_RADIANS(index*ANGLE-10));
    
    CGFloat centerX = size.width/2 - (size.width/2-20)*cos(DEGREES_TO_RADIANS(index*ANGLE-10));
    
    label.center = CGPointMake(centerX, centerY);
    
    
    
    [self.labelArray addObject:label];
    
}



#pragma mark - lazy loading

- (NSMutableArray *)labelArray {
    
    if (!_labelArray) {
        
        _labelArray = [NSMutableArray arrayWithCapacity:0];
        
    }
    
    return _labelArray;
    
}

- (NSMutableArray *)titleArray {//分值对应等级
    
    if (!_titleArray) {
        
        _titleArray = [NSMutableArray arrayWithCapacity:0];
        
        [_titleArray addObject:@"0"];
        
        [_titleArray addObject:@"一般"];
        
        [_titleArray addObject:@"60"];
        
        [_titleArray addObject:@"中等"];
        
        [_titleArray addObject:@"70"];
        
        [_titleArray addObject:@"良好"];
        
        [_titleArray addObject:@"80"];
        
        [_titleArray addObject:@"优秀"];
        
        [_titleArray addObject:@"90"];
        
        [_titleArray addObject:@"极好"];
        
        [_titleArray addObject:@"100"];
        
    }
    
    return _titleArray;
    
}


@end

使用

ArcLineView * view = [ArcLineView new];
view.backgroundColor = HKWhite;
[view setStarScore:0.85 title:@"66 / 88"];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(83);
    make.width.mas_equalTo(180);
    make.top.equalTo(closeButton.mas_bottom).mas_offset(26);
    make.height.mas_equalTo(150);
}];

相关文章

  • iOS 的颜色渐变圆弧

    iOS 的颜色渐变 实现一个圆弧进度条主要分为三步 一、画圆弧这里用的贝赛尔曲线,就是这个东西:UIBezierP...

  • iOS 半圆弧进度条

    1.效果图 2.代码 .h .m 使用

  • 自定义 View 实战 04 - 圆形进度条

    圆形进度条分析: 圆 圆弧 进度值 所以这里我们先用 drawCircle画出圆,再使用drawArc画出圆弧(这...

  • 圆弧进度条

    已经很久没有写文章了,这次来说说环形进度条吧。 如上图所示,与设计稿还原度99%,本来想忽悠设计师用echars的...

  • ios 渐变色圆弧进度条

    经过一番折腾做了以下工具类:

  • iOS圆弧渐变进度条的实现

    由于项目需要一个环形渐变进度条显示课程,这方便网上的确有很多相关资料但是,都是比较零散的而且,大多数只是放一堆代码...

  • iOS 各种圆形进度条

    iOS 各种圆形进度条:UAProgressView iOS手把手教你实现圆形进度条

  • 自定义圆形进度条

    原理就是画一个圆弧,然后用内圆去覆盖圆弧实心的地方,然后写一个监听去时时更新进度条的进度.分为外圆,内圆,文字进度...

  • 2019-05-09

    圆形进度条,网上代码一大片,就不多说了 现在要实现的是进度圆弧,设置圆弧的起始点颜色和终点颜色 而不是设置半圆的起...

  • iOS造轮子系列-圆弧形进度条

    小菜鸡一枚, 因为项目紧, 而且喜欢用自己写的东西, 所以造了很多小轮子, 肯定会有缺陷,还有很多不完善的地方, ...

网友评论

      本文标题:iOS 半圆弧进度条

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