#import "ViewController.h"
#import "IProgressView.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet IProgressView *pinkView;
@property (strong, nonatomic) IBOutlet UILabel *progressLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)sliderValueChanged:(UISlider *)sender {
float value = sender.value;
self.progressLabel.text = [NSString stringWithFormat:@"%.1f%%", value * 100];
self.pinkView.progress = value;
}
@end
#import <UIKit/UIKit.h>
@interface IProgressView : UIView
@property (nonatomic, assign) float progress;
@end
#import "IProgressView.h"
@implementation IProgressView
- (void)setProgress:(float)progress
{
_progress = progress;
[self setNeedsDisplay]; // 重绘(系统会自动调用drawRect方法)
}
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = rect.size.width * 0.5 - 10;
CGFloat startAngle = -M_PI_2; // 从-90°开始
CGFloat endAngle = startAngle + self.progress * M_PI * 2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
CGContextSetLineWidth(context, 3);
[[UIColor blueColor] setStroke];
CGContextAddPath(context, path.CGPath);
CGContextStrokePath(context);
}
@end
效果图如下: