ViewController.h 文件
#import "ViewController.h"
#import "ProgressView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *labelView;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet ProgressView *progressView;
@end
@implementation ViewController
- (IBAction)sliderAction:(UISlider *)sender {
//设置label上的文字内容
_labelView.text = [NSString stringWithFormat:@"%.2f%%",sender.value*100];
//传值
_progressView.progress = sender.value;
}
ProgressView.h 文件
#import <UIKit/UIKit.h>
@interface ProgressView : UIView
//提供一个接受滑动条value值的属性
@property(nonatomic,assign)CGFloat progress;
@end
ProgressView.m 文件
#import "ProgressView.h"
@implementation ProgressView
- (void)drawRect:(CGRect)rect {
//设置半径
CGFloat radius = rect.size.width*0.5;
//圆心
CGPoint center = CGPointMake(radius, radius);
//终点
CGFloat endA = - M_PI_2 +_progress*M_PI*2;
//伊瑟尔路径绘图
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius-3 startAngle:-M_PI_2 endAngle:endA clockwise:YES];
//渲染
[path stroke];
}
//复写set方法,作用:一旦progress的值改变,就重绘
-(void)setProgress:(CGFloat)progress{
_progress = progress;
//重绘,系统会先创建与view相关联的上下文,然后再调用drawrect
[self setNeedsDisplay];
//注意:drawRect不能手动调用,因为图形上下文我们自己创建不了,只能由系统帮我们创建,并且传递给我们
}
@end
storyboard中的布局
运行截图(拖动滚动条,进行画圆)