IOS中画虚线和圆形的进度条

本文展示了如何在iOS应用中创建虚线和圆形进度条。通过自定义`DrawingView`类,利用`Core Graphics`进行路径绘制,设置线宽、颜色以及虚线模式。代码详细解释了如何根据百分比画出不同颜色的圆形进度条,并添加了百分比显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码了 


如下

.h文件

@interface DrawingView : UIView

@property(nonatomic,assign)BOOL circle;

@property(nonatomic,strong)NSString* percent;

@end




.m文件

#import "DrawingView.h"

#define PI 3.14159265358979323846

@implementation DrawingView

-(id)initWithFrame:(CGRect)frame

{

    if (self=[super initWithFrame:frame]){

     

    }

    return self;

}

-(void)drawRect:(CGRect)rect

{

    if (!_circle) {

        self.backgroundColor=[UIColor clearColor];

        CGContextRef context =UIGraphicsGetCurrentContext();

        CGContextBeginPath(context);

        CGContextSetLineWidth(context, 1.0);

        CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);


        CGFloat lengths[] = {3,1};//此值可以确定所花虚线的点数  

        CGContextSetLineDash(context, 0, lengths,2);


      

        CGContextSetLineDash(context, 0,lengths,2);


        CGContextMoveToPoint(context, 0.0, 1.0);

        CGContextAddLineToPoint(context, 300.0,1.0);

        CGContextStrokePath(context);

    }else

    {

        int selfPercent=(int)([self.percent floatValue]*100);

        CGContextRef context =UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 5);

        CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-11)/2, 0, 2*PI, 0);

        CGContextSetStrokeColorWithColor(context, [UtilityHelper colorWithHexString:@"#efefef"].CGColor);

        CGContextDrawPath(context, kCGPathStroke);

        

        CGContextSetLineWidth(context, 5);

        CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-11)/2, -PI/2, (selfPercent*2*PI)/100-PI/2, 0);

        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

        CGContextDrawPath(context, kCGPathStroke);

        UILabel *percentLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height)];

        percentLabel.text=[NSString stringWithFormat:@"%d%%",(int)(selfPercent)];

        percentLabel.backgroundColor=[UIColor clearColor];

        percentLabel.textColor=[UIColor redColor];

        if (selfPercent==0) {

            percentLabel.textColor=[UtilityHelper colorWithHexString:@"#efefef"];

        }

        percentLabel.font=[UIFont systemFontOfSize:10];

        percentLabel.textAlignment=NSTextAlignmentCenter;

        [self addSubview:percentLabel];

        

        /*

        CGContextSetLineWidth(context, 0.5);

        CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-15)/2, 0, 2*PI, 0);

        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

        CGContextDrawPath(context, kCGPathStroke);


        CGContextSetLineWidth(context, 0.5);

        CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-5)/2, 0, 2*PI, 0);

        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

        CGContextDrawPath(context, kCGPathStroke);

        */

    }

 

}


@end

在alloc这个view的时候一定要设置view的backgroundcolor=clearcolor   否则图画不出来
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值