.h文件
#import <UIKit/UIKit.h>
//宽
#define LINEWIDTH 5
//总时间
//#define ALLTOTALTIME 61
//定时器时间
#define TIMER 1
typedef NS_ENUM(NSInteger,CircularProgressViewType)
{
CircularProgressViewType_Label = 0,
CircularProgressViewType_Button,
};
@protocol CircularProgressDelegate;
@interface CircularProgressView : UIView
@property (assign, nonatomic) id <CircularProgressDelegate> delegate;
- (id)initWithFrame:(CGRect)frame
backColor:(UIColor *)backColor
progressColor:(UIColor *)progressColor
lineWidth:(CGFloat)lineWidth
totalTime:(CGFloat)totalTime
type:(CircularProgressViewType)type;
- (void)play;
- (void)pause;
- (void)revert;
@end
@protocol CircularProgressDelegate <NSObject>
- (void)didUpdateProgressView:(CGFloat)progress;
//- (void)stopEvent;
@end
.m文件
//
// PPX_CircularProgressView.m
// PPX_CircularProgress
//
// Created by pipixia on 16/10/9.
// Copyright © 2016年 pipixia. All rights reserved.
//
#import "CircularProgressView.h"
#import "CommonUI.h"
@interface CircularProgressView ()
{
BOOL isStart;
}
@property (strong, nonatomic) UIColor *backColor;
@property (strong, nonatomic) UIColor *progressColor;
@property (assign, nonatomic) CGFloat lineWidth;
@property (assign, nonatomic) float progress;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) NSInteger currentProgress;//目前的进展
@property (assign, nonatomic) NSInteger timerFloat;//总进展
@property (assign, nonatomic) NSInteger timeWhole;//总进展
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *stopButton;
@end
@implementation CircularProgressView
- (id)initWithFrame:(CGRect)frame
backColor:(UIColor *)backColor
progressColor:(UIColor *)progressColor
lineWidth:(CGFloat)lineWidth
totalTime:(CGFloat)totalTime
type:(CircularProgressViewType)type;
{
CGSize size = [CommonUI screenDp];
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
_backColor = backColor;
_progressColor = progressColor;
_lineWidth = lineWidth;
_timerFloat = totalTime;
_currentProgress = totalTime;
_progress = 1;
_timeWhole = totalTime;
isStart = YES;
if (type == CircularProgressViewType_Label)
{
_timeLabel = [[UILabel alloc]init];
_timeLabel.font = [UIFont boldSystemFontOfSize:(size.width*0.1)];
_timeLabel.textAlignment = NSTextAlignmentCenter;
_timeLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
_timeLabel.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height*0.4);
NSString *str_minute = [NSString stringWithFormat:@"%02ld", (_timeWhole % 3600) / 60];
NSString *str_second = [NSString stringWithFormat:@"%02ld", _timeWhole % 60];
NSString *format_time = [NSString stringWithFormat:@"%@ : %@", str_minute, str_second];
// 修改倒计时标签及显示内容
self.timeLabel.text = format_time;
_timeLabel.textColor = [UIColor whiteColor];
[self addSubview:_timeLabel];
UILabel *warm = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.width*0.12)];
warm.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height*0.65);
warm.textAlignment = NSTextAlignmentCenter;
warm.font = [UIFont systemFontOfSize:size.width*0.04];
warm.text = @"Warm up";
warm.textColor = [UIColor whiteColor];
[self addSubview:warm];
}
else
{
_stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
_stopButton.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[_stopButton setTitle:@"Stop" forState:UIControlStateNormal];
[_stopButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_stopButton addTarget:self action:@selector(stopClicked) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_stopButton];
}
}
return self;
}
- (void)drawRect:(CGRect)rect{
//draw background circle
UIBezierPath *backCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2,self.bounds.size.height / 2)
radius:self.bounds.size.width / 2 - self.lineWidth / 2
startAngle:(CGFloat) - M_PI_2
endAngle:(CGFloat)(1.5 * M_PI)
clockwise:YES];
[self.backColor setStroke];
backCircle.lineWidth = self.lineWidth;
[backCircle stroke];
if (self.progress != 0){
//draw progress circle
UIBezierPath *progressCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2,self.bounds.size.height / 2)
radius:self.bounds.size.width / 2 - self.lineWidth / 2
startAngle:(CGFloat) - M_PI_2
endAngle:(CGFloat)(- M_PI_2 + self.progress * 2 * M_PI)
clockwise:YES];
[self.progressColor setStroke];
progressCircle.lineWidth = self.lineWidth;
[progressCircle stroke];
}
}
- (void)updateProgressCircle{
self.currentProgress -= 1;
self.timeWhole -= 1;
//update progress value
self.progress = (float) (_currentProgress * 1.0 / _timerFloat);
//redraw back & progress circles
[self setNeedsDisplay];
if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressDelegate)]){
[self.delegate didUpdateProgressView:self.currentProgress];
}
NSString *str_minute = [NSString stringWithFormat:@"%02ld", (_timeWhole % 3600) / 60];
NSString *str_second = [NSString stringWithFormat:@"%02ld", _timeWhole % 60];
NSString *format_time = [NSString stringWithFormat:@"%@ : %@", str_minute, str_second];
// 修改倒计时标签及显示内容
self.timeLabel.text = format_time;
if (self.progress <= 0.0f){
//invalid timer
[self.timer invalidate];
self.currentProgress = _timeWhole;
self.timeWhole = _timeWhole;
//restore progress value
// self.progress = 0;
//self redraw
// [self setNeedsDisplay];
}
}
//- (void)stopClicked
//{
// if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressDelegate)])
// {
// [self.delegate stopEvent];
// }
//
//}
- (void)play{
if (isStart){
self.timer = [NSTimer scheduledTimerWithTimeInterval:TIMER target:self selector:@selector(updateProgressCircle) userInfo:nil repeats:YES];
[self.timer fire];
self->isStart = NO;
}
}
- (void)pause{
if (!isStart){
[self.timer invalidate];
self.timer = nil;
isStart = YES;
}
}
- (void)revert{
isStart = YES;
[self updateProgressCircle];
self.progress = _timeWhole;
self.currentProgress = _timeWhole;
self.timeWhole = _timeWhole;
NSString *str_minute = [NSString stringWithFormat:@"%02ld", (_timeWhole % 3600) / 60];
NSString *str_second = [NSString stringWithFormat:@"%02ld", _timeWhole % 60];
NSString *format_time = [NSString stringWithFormat:@"%@ : %@", str_minute, str_second];
// 修改倒计时标签及显示内容
self.timeLabel.text = format_time;
[self.timer invalidate];
self.timer = nil;
}
@end