.h 中c#
typedef enum { START = 0, //开始 PAUSE //暂停 }ViewStatus; #import <UIKit/UIKit.h> @interface CircleView : UIView /** * 进度 */ @property (nonatomic, strong)NSString *progress; @property (nonatomic, assign)CGFloat pi; /** * 按钮状态 */ @property (nonatomic, assign)ViewStatus status; @end
.m 中atom
/** * 根据语音进度,画弧度 * * @param progress 语音进度 */ - (void)setProgress:(NSString *)progress { CGFloat pi = [progress floatValue] / 100 * 3.141593; self.pi = pi; if (self.pi >= 3.14) { self.pi = 0.0; self.status = PAUSE; [self setNeedsDisplay]; }else { [self setNeedsDisplay]; } } /** * 画按钮 */ - (void)drawRect:(CGRect)rect { if (self.status == START) { [self pauseButton]; //灰色的圆 CGFloat pi = self.pi; CGFloat colorGray = 102 / 255.0; circle(pi,YES,colorGray); drawBlueColor(pi, NO); }else { //灰色的圆 CGFloat pi = self.pi; CGFloat colorGray = 102 / 255.0; circle(pi,YES,colorGray); [self beginButton]; } } /** * 开始播放按钮 */ - (void)beginButton { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(ctx, 11.5, 8.5); CGContextAddLineToPoint(ctx, 11.5, 18.5); CGContextAddLineToPoint(ctx, 16.5, 13.5); CGContextClosePath(ctx); CGContextFillPath(ctx);//这个是画实心的 // CGContextStrokePath(<#CGContextRef _Nullable c#>);这个是画空心的 } /** * 暂停按钮 */ - (void)pauseButton { drawLine(11.5, 18.5); drawLine(17.5, 18.5); } /** * 画一条线 * * @param x 线的x值 * @param length 线的长度 */ void drawLine(int x, int length) { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(ctx, x, 8.5); CGContextAddLineToPoint(ctx, x, length); CGContextSetLineWidth(ctx, 2); CGContextStrokePath(ctx); } /** * 进度条 * * @param pi 圆的弧度 * @param b */ void drawBlueColor(float pi,bool b) { //0.得到当前图形上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //1.建立一个绘图路径 CGMutablePathRef path = CGPathCreateMutable(); //2.把绘图信息添加到路径中去 CGContextSetRGBStrokeColor(ctx, 32 / 225, 192 / 255.0, 218 / 255.0, 1); CGContextSetLineWidth(ctx, 2); CGPathAddArc(path, NULL, 14, 14, 12.5 , 0, 2 * pi, b); //3.把路径添加到上下文中 CGContextAddPath(ctx, path); //4.渲染 CGContextStrokePath(ctx); //5.释放路径 CGPathRelease(path); } /** * 画一个圆 * * @param */ void circle(float pi, bool b, float color) { //0.得到当前图形上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //1.建立一个绘图路径 CGMutablePathRef path = CGPathCreateMutable(); //2.把绘图信息添加到路径中去 CGContextSetRGBStrokeColor(ctx, color, color, color, 1); CGContextSetLineWidth(ctx, 2); // NSLog(@"%f",pi); CGPathAddArc(path, NULL, 14, 14, 12.5 ,12.5, 2 * pi, b); //3.把路径添加到上下文中 CGContextAddPath(ctx, path); //4.渲染 CGContextStrokePath(ctx); //5.释放路径 CGPathRelease(path); }
调用code
#import "CircleButton.h" @interface ViewController () - (IBAction)buttonClick:(id)sender; /** * 自定义的button */ @property (weak, nonatomic) IBOutlet CircleButton *circleButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //设定button的初始状态 self.circleButton.status = PAUSE; } - (void)progress { float progress ; self.circleButton.progress = [NSString stringWithFormat:@"%f",progress]; if (progress < 1) { progress += 0.01; [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(progress) userInfo:nil repeats:NO]; } } //点击切换状态,重绘按钮 - (IBAction)buttonClick:(id)sender { if (self.circleButton.status == PAUSE) { [self.circleButton setNeedsDisplay]; [self progress]; self.circleButton.status = START; }else{ [self.circleButton setNeedsDisplay]; self.circleButton.status = PAUSE; } }
如果想,在暂停的时候显示进度,只须要把progress 传给view重绘便可orm