您的位置:首页 > 移动开发 > IOS开发

IOS开发-利用绘图表示进度

2016-05-20 12:07 507 查看
(1)创建自定义view。

(2)导入QuartzCore框架

(3)在h文件,定义一个属性,用于接收进度值,最大值是1,最小值是0:

#import <UIKit/UIKit.h>

@interface ZCView : UIView
@property(nonatomic,assign)CGFloat progress;
@end


(4)m文件的代码如下:

#import "ZCView.h"
#import <QuartzCore/QuartzCore.h>

@interface ZCView ()
@property(nonatomic,weak)UILabel *progressLabel;
@end

@implementation ZCView

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
CGFloat proX = 0;
CGFloat proW = frame.size.width;
CGFloat proH = 20;
CGFloat proY = (frame.size.height-proH)/2;
UILabel *progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(proX, proY, proW, proH)];
[progressLabel setTextAlignment:NSTextAlignmentCenter];
self.progressLabel = progressLabel;
[self addSubview:progressLabel];
}
return self;
}

- (void)drawRect:(CGRect)rect {
//线宽
CGFloat lineWidth = 5;
//半径
CGFloat radius = (rect.size.width-lineWidth*4)/2;
//圆心
CGPoint center = CGPointMake(rect.size.width/2, rect.size.width/2);
//结束弧度
CGFloat endAngle = -M_PI_2+(_progress*M_PI*2);
//路径
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:-M_PI_2 endAngle:endAngle clockwise:YES];
[path setLineWidth:lineWidth];
[path stroke];
}

- (void)setProgress:(CGFloat)progress
{
_progress = progress;

[self.progressLabel setText:[NSString stringWithFormat:@"%.2f%%",progress*100]];

[self setNeedsDisplay];
}
@end


传入progress的值就能刷新进度了:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息