self.slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, 200, 20)]; self.slider.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)+150); [self.view addSubview:self.slider]; [self.slider addTarget:self action:@selector(actionProgressMove:) forControlEvents:UIControlEventValueChanged]; //進度視圖 self.circleSlideView = [[CircleSlideView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)]; self.circleSlideView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame) - 100); [self.view addSubview:self.circleSlideView];
- (void)actionProgressMove:(UISlider *)slider{ self.circleSlideView.progress = self.slider.value; }
繪製精度環:ide
#import <UIKit/UIKit.h> @interface CircleSlideView : UIView @property (nonatomic, assign) CGFloat progress; @end
#import "CircleSlideView.h" @interface CircleSlideView () @property (nonatomic, strong) CAShapeLayer * backLayer; @property (nonatomic, strong) UIBezierPath * backPath; @property (nonatomic, strong) CAShapeLayer * progressLayer; @property (nonatomic, strong) UIBezierPath * progressPath; @end @implementation CircleSlideView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self loadContent]; } return self; } - (void)loadContent { // 底部Layer self.backLayer = [CAShapeLayer layer]; self.backLayer.bounds = self.bounds; self.backLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2); self.backLayer.lineWidth = 10.0; self.backLayer.strokeColor = [UIColor lightGrayColor].CGColor; self.backLayer.fillColor = [UIColor clearColor].CGColor; // 設置路徑 self.backPath = [UIBezierPath bezierPathWithOvalInRect:self.bounds]; self.backLayer.path = self.backPath.CGPath; [self.layer addSublayer:self.backLayer]; // 進度layer self.progressLayer = [CAShapeLayer layer]; self.progressLayer.bounds = self.bounds; self.progressLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2); self.progressLayer.lineWidth = 10.0; self.progressLayer.strokeColor = [UIColor purpleColor].CGColor; self.progressLayer.fillColor = [UIColor clearColor].CGColor; self.progressLayer.lineCap = kCALineCapRound; // 設置路徑 self.progressPath = [UIBezierPath bezierPathWithOvalInRect:self.bounds]; self.progressLayer.path = self.progressPath.CGPath; [self.layer addSublayer:self.progressLayer]; self.progressLayer.strokeStart = 0; //路徑起始位置 self.progressLayer.strokeEnd = 0; //路徑結束位置 } - (void)setProgress:(CGFloat)progress { if (progress < 0) { progress = 0; } if (progress > 1) { progress = 1; } _progress = progress; _progressLayer.strokeEnd = self.progress; } @end