// // ViewController.m // 自定义进度条 // // Created by dllo on 16/3/30. // Copyright © 2016年 HaiTeng. All rights reserved. // #import "ViewController.h" #import "ProgressViewCustom.h" @interface ViewController () @property (nonatomic, strong) UISlider *slider; @property (nonatomic, strong) ProgressViewCustom *progressViewCustom; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; self.slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 50, 200, 20)]; [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:self.slider]; self.progressViewCustom = [[ProgressViewCustom alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; _progressViewCustom.center = self.view.center; _progressViewCustom.backgroundColor = [UIColor purpleColor]; [self.view addSubview:_progressViewCustom]; } - (void)sliderAction:(UISlider *)sender{ NSLog(@"%lf",sender.value); _progressViewCustom.progress = sender.value; } @end
// // ProgressViewCustom.m // 自定义进度条 // // Created by dllo on 16/3/30. // Copyright © 2016年 HaiTeng. All rights reserved. // #import "ProgressViewCustom.h" @implementation ProgressViewCustom - (void)drawRect:(CGRect)rect { //半径 CGFloat radius = rect.size.width / 2; //中心点 CGPoint center = CGPointMake(radius, radius); //结束点. - 90 + (正在转的点slider的value*360) CGFloat endA = -M_PI_2 + _progress * M_PI * 2; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 5 startAngle:-M_PI_2 endAngle:endA clockwise:YES]; [path addLineToPoint:center]; [path closePath]; [path fill]; // [path stroke]; } - (void)setProgress:(CGFloat)progress { _progress = progress; //重绘 先调用此View的layer相关的上下文, 再调用drawRect方法 [self setNeedsDisplay]; } @end
原文:http://www.cnblogs.com/HaiTeng/p/5338435.html