iOS 贝塞尔曲线展示百分比

0.效果图

贝塞尔占比.png

1.新建一个自定义视图,继承自 UIView,代码如下:
.h 文件

#import <UIKit/UIKit.h>

#define WINSIZE [UIScreen mainScreen].bounds.size

@interface DJBezierPathPercent : UIView

/**
 占比
 */
@property (nonatomic, assign) CGFloat percent;
@end

.m 文件

#import "DJBezierPathPercent.h"

#define progressCornerRadius WINSIZE.width * 0.164

@interface DJBezierPathPercent()

@property (nonatomic, strong) UILabel *percentLab;
@end

@implementation DJBezierPathPercent

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        int padding = 15;
        
        UIView *borderView = [UIView new];
        
        CGFloat W = progressCornerRadius * 2 + padding;
        CGFloat H = W;
        CGFloat X = (CGRectGetWidth(frame)  - W) / 2;
        CGFloat Y = (CGRectGetHeight(frame) - H) / 2;
        
        borderView.frame = CGRectMake(X, Y, W, H);
        
        borderView.layer.cornerRadius = (progressCornerRadius*2 + padding)/2;
        borderView.layer.borderWidth = 0.5;
        borderView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [self addSubview:borderView];
        
        
        _percentLab = [[UILabel alloc] init];
        _percentLab.frame = CGRectMake(X, Y + H/2 - 30/2, W, 30);
        _percentLab.textAlignment = NSTextAlignmentCenter;
        _percentLab.font = [UIFont systemFontOfSize:15.f];
        [self addSubview:_percentLab];
    }
    return self;
}

- (void)setPercent:(CGFloat)percent {
    _percent = percent;
    _percentLab.text = [NSString stringWithFormat:@"%.f%%",percent];
}

- (void)drawRect:(CGRect)rect {
    
    [self drawPercent:self.percent];
}

- (void)drawPercent:(CGFloat)percent {
    
    percent = percent / 100.f;
    
    UIBezierPath *path = [[UIBezierPath alloc]init];
    path.lineWidth = 5;
    [[UIColor cyanColor] set];
    [path addArcWithCenter:self.center radius:progressCornerRadius startAngle:- M_PI_2 endAngle:(2 * M_PI) * percent - M_PI_2 clockwise:YES];
    [path stroke];
}

@end

控制器内调用

    DJBezierPathPercent *bezier = [[DJBezierPathPercent alloc] initWithFrame:self.view.bounds];
    bezier.percent = arc4random()%100;
    [self.view addSubview:bezier];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容