进度条

进度条是很常见的一个控件,在开发中系统的进度条往往是不能满足需求的,这里就介绍一个简单的圆形的进度条的实现。

这里用到的是系统原生态绘图Quartz2D,直接看代码吧;

新建一个UIView,在.h里面先写上两个属性为:

/**进度值:0 ~ 1*/

@property (nonatomic,assign) CGFloat progress;

/**进度条的颜色*/

@property (nonatomic,strong) UIColor *progressColor;

然后在.m文件的

- (void)drawRect:(CGRect)rect方法里面去实现画圆,如下:

- (void)drawRect:(CGRect)rect {

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 绘制底层灰色颜色的圆

    CGContextAddArc(ctx, CGRectGetMidX(rect),CGRectGetMidY(rect), rect.size.width*0.4, 0, M_PI * 2, 0);

    CGContextSetLineWidth(ctx, self.bounds.size.width*0.1);

    [[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1] set];

    CGContextStrokePath(ctx);

    // 绘制进度颜色的圆

    CGContextAddArc(ctx, CGRectGetMidX(rect),CGRectGetMidY(rect), rect.size.width*0.4, 0,M_PI*2*self.progress, 0);

    CGContextSetLineWidth(ctx, self.bounds.size.width*0.1);

    [self.progressColor set];

    CGContextStrokePath(ctx);

    

    [self setText];   // 进度中间的进度值

}

- (void)setText{

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width*0.1, self.bounds.size.height*0.1, self.bounds.size.width*0.8, self.bounds.size.height*0.8)];

    // 字体大小适应

    label.font = [UIFont systemFontOfSize:(25*self.bounds.size.width)/100];

    label.textColor = [UIColor blackColor];

    label.textAlignment = NSTextAlignmentCenter;

    label.text = [NSString stringWithFormat:@"%ld%%",(NSInteger)(self.progress*100)];

    label.adjustsFontSizeToFitWidth = YES;

    [self addSubview:label];

}

最后运行的结果:
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值