-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
90 lines (72 loc) · 3.15 KB
/
Copy pathViewController.m
File metadata and controls
90 lines (72 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// ViewController.m
// MBProgress
//
// Created by Circle on 16/4/12.
// Copyright © 2016年 Hoolang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIView *container;
@property (weak, nonatomic) IBOutlet UILabel *percentLabel;
@property (nonatomic, strong) CAShapeLayer *shapeLayerProgress;
@property (nonatomic, strong) CAShapeLayer *shapeLayerBackground;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.slider.value = 0;
// 初始旋转,则从顶部开始画圆,否则从右侧开始花园
self.container.transform = CGAffineTransformMakeRotation(-M_PI/2);
[self initLayer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)changeValue:(id)sender {
_shapeLayerProgress.strokeStart = 0;
_shapeLayerProgress.strokeEnd = self.slider.value;
[_percentLabel setText:[NSString stringWithFormat:@"%.f%%", self.slider.value * 100]];
NSLog(@"self.slider.value:%f", self.slider.value);
}
-(void)initLayer{
#pragma mark 背景
//创建出CAShapeLayer
self.shapeLayerBackground = [CAShapeLayer layer];
self.shapeLayerBackground.frame = CGRectMake(0, 0, 100, 100);
self.shapeLayerBackground.position = CGPointMake(self.container.frame.size.width *0.5, self.container.frame.size.height * 0.5);
self.shapeLayerBackground.fillColor = [UIColor clearColor].CGColor;
//设置线条的宽度和颜色
self.shapeLayerBackground.lineWidth = 2.0f;
self.shapeLayerBackground.strokeColor = [UIColor redColor].CGColor;
//设置stroke起始点
self.shapeLayerBackground.strokeStart = 0;
self.shapeLayerBackground.strokeEnd = 1;
//创建出圆形贝塞尔曲线
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];
//让贝塞尔曲线与CAShapeLayer产生联系
self.shapeLayerBackground.path = circlePath.CGPath;
//添加并显示
[self.container.layer addSublayer:self.shapeLayerBackground];
#pragma mark 进度条
//创建出CAShapeLayer
self.shapeLayerProgress = [CAShapeLayer layer];
self.shapeLayerProgress.frame = CGRectMake(0, 0, 100, 100);
self.shapeLayerProgress.position = CGPointMake(self.container.frame.size.width *0.5, self.container.frame.size.height * 0.5);
self.shapeLayerProgress.fillColor = [UIColor clearColor].CGColor;
//设置线条的宽度和颜色
self.shapeLayerProgress.lineWidth = 2.0f;
self.shapeLayerProgress.strokeColor = [UIColor blueColor].CGColor;
//设置stroke起始点
self.shapeLayerProgress.strokeStart = 0;
self.shapeLayerProgress.strokeEnd = 0;
//创建出圆形贝塞尔曲线
UIBezierPath *circlePath2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];
//让贝塞尔曲线与CAShapeLayer产生联系
self.shapeLayerProgress.path = circlePath2.CGPath;
//添加并显示
[self.container.layer addSublayer:self.shapeLayerProgress];
}
@end