-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLDGauges.m
124 lines (102 loc) · 3.44 KB
/
LDGauges.m
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// LDGauges.m
//
// Created by Luca D'Incà on 29/06/13.
// Copyright (c) 2013 Luca D'Incà. All rights reserved.
//
// This code comes with no warranty, free to use for any purpose
// Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
#import "LDGauges.h"
#define DEGRESS_TO_RADIUS(degress) ((M_PI * degress)/ 180)
@implementation LDGauges
{
CALayer *_containerLayer;
CGPoint _center;
CGFloat _startAngle;
NSInteger _percentage;
LDGaugesLayer *_gauges;
}
#pragma mark Inizialization methods
- (id)initWithFrame:(CGRect)frame startAngle:(CGFloat)startAngle gaugesRadius:(CGFloat)radius
gaugesThickness:(CGFloat)thickness
{
if ((self = [super initWithFrame:frame])) {
[self doInitialSetup];
_startAngle = startAngle;
_center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
_gauges = [LDGaugesLayer layer];
[_gauges setStartAngle:DEGRESS_TO_RADIUS(_startAngle)];
[_gauges setStopAngle:DEGRESS_TO_RADIUS(_startAngle)];
[_containerLayer addSublayer:_gauges];
[_gauges initWithFreame:self.bounds
center:_center
radius:radius
thickness:thickness];
[_gauges setNeedsDisplay];
}
return self;
}
- (id)initWithFrame:(CGRect)frame startAngle:(CGFloat)startAngle center:(CGPoint)center gaugesRadius:(CGFloat)radius
gaugesThickness:(CGFloat)thickness
{
if (self = [super initWithFrame:frame]) {
[self doInitialSetup];
_startAngle = startAngle;
_center = center;
_gauges = [LDGaugesLayer layer];
[_gauges setStartAngle:DEGRESS_TO_RADIUS(_startAngle)];
[_gauges setStopAngle:DEGRESS_TO_RADIUS(_startAngle)];
[_containerLayer addSublayer:_gauges];
[_gauges initWithFreame:self.bounds
center:_center
radius:radius
thickness:thickness];
[_gauges setNeedsDisplay];
}
return self;
}
- (void)doInitialSetup
{
//Impsoto il colore della UIVIew a transparente
self.backgroundColor = [UIColor clearColor];
//Istanzio il layer
_containerLayer = [CALayer layer];
_containerLayer.frame = self.bounds;
//Aggiungo il layer dove saranno disegnate tutte le componenti
[self.layer addSublayer:_containerLayer];
}
- (CGFloat)convertPercentageToDegrees:(NSInteger)rate
{
return (360.0f/100*rate)+_startAngle;
}
#pragma mark Draw text methods
- (void)drawString:(NSString *)string withFont:(UIFont *)font
{
CGSize fontSize = [string sizeWithFont:font];
CGFloat yOffset = ((_center.y - fontSize.height/2));
CGFloat xOffset = ((_center.x - fontSize.width/2));
CGRect textRect = CGRectMake(xOffset, yOffset, fontSize.width, fontSize.height);
[string drawInRect:textRect withFont:font];
}
- (void)drawRect:(CGRect)rect
{
[_fontColor set];
NSString *text = [NSString stringWithFormat:@"%d%%", _percentage];
[self drawString:text
withFont:_font];
}
#pragma mark Set/Update methods
- (void)setGaugeRate:(NSInteger)rate
{
_gauges.stopAngle = DEGRESS_TO_RADIUS([self convertPercentageToDegrees:rate]);
_percentage = rate;
[self setNeedsDisplay];
}
- (void)setGaugeColor:(UIColor *)color
{
[_gauges setBackgroudColor:color];
}
- (void)setGaugeBackgroundColor:(UIColor *)color{
[_gauges setColor:color];
}
@end