-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoalView.m
More file actions
executable file
·152 lines (117 loc) · 4.69 KB
/
Copy pathGoalView.m
File metadata and controls
executable file
·152 lines (117 loc) · 4.69 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// GoalView.m
// Stepr
//
// Created by Philipp Hackbarth on 31.10.13.
// Copyright (c) 2013 Hackbarth GFX. All rights reserved.
//
#import "GoalView.h"
#import "StepManager.h"
#import "AppSettings.h"
@implementation GoalView
static const CGFloat stroke = 25.0f;
static const CGFloat pi = 3.1416f;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
self.reachedInPercent = [NSNumber numberWithFloat:0.0f];
// vollen Kreis im Hintergrund erstellen
[self createBackgroundCircle];
}
- (id)initWithCoder:(NSCoder*)aCoder
{
if (self = [super initWithCoder:aCoder]) {
[self initialize];
}
return self;
}
- (void)prepareForInterfaceBuilder {
self.reachedInPercent = [NSNumber numberWithFloat:.75f];
[self createBackgroundCircle];
[self createCircle];
}
-(void) createBackgroundCircle {
// Kreisdaten
CGFloat width = self.layer.bounds.size.width;
CGFloat height = self.layer.bounds.size.height;
CGFloat radius = width / 2.0f - stroke;
CGPoint startPoint = CGPointMake(width / 2.0f, height / 2.0f);
//Hintergrund-Kreis
CAShapeLayer* backgroundCircle = [CAShapeLayer layer];
backgroundCircle.path = [UIBezierPath bezierPathWithArcCenter:startPoint radius:radius startAngle:0 endAngle:2.0f * pi clockwise:YES].CGPath;
backgroundCircle.opacity = 0.05f;
backgroundCircle.fillColor = [UIColor clearColor].CGColor;
backgroundCircle.strokeColor = [UIColor blackColor].CGColor;
backgroundCircle.lineWidth = stroke;
backgroundCircle.lineCap = kCALineCapRound;
[self.layer addSublayer:backgroundCircle];
}
-(void) createCircle {
// Kreisdaten
CGFloat width = self.layer.bounds.size.width;
CGFloat height = self.layer.bounds.size.height;
CGPoint startPoint = CGPointMake(width / 2.0f, height / 2.0f);
CGFloat radius = width / 2.0f - stroke;
// Animierter Kreis
self.circle = [CAShapeLayer layer];
// Configure the apperence of the circle
self.circle.fillColor = [UIColor clearColor].CGColor;
self.circle.strokeColor = [StepManager steprGreenColor].CGColor;
self.circle.lineWidth = stroke;
self.circle.lineCap = kCALineCapRound;
self.circle.opacity = 0.80f;
// Add to parent layer
[self.layer addSublayer:self.circle];
self.circle.path = [UIBezierPath bezierPathWithArcCenter:startPoint radius:radius startAngle:-pi / 2.0f
endAngle:([self.reachedInPercent floatValue] * (2 * pi)) - pi / 2.0f
clockwise:YES].CGPath;
}
- (void)Animate
{
self.stepsText.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.stepCount];
self.goalText.text = [NSString stringWithFormat:NSLocalizedString(@"of %lu", nil), (unsigned long)[AppSettings getInstance].goal];
self.caloriesText.text = [NSString stringWithFormat:@"%d kcal", (int)[StepManager getCaloriesForSteps:self.stepCount]];
self.distanceText.text = [NSString stringWithFormat:@"%@", [StepManager localizedDistanceFor:self.stepCount*self.strideLength]];
//! Kreis erstellen
[self createCircle];
// Configure animation
CABasicAnimation* drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 2.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = YES; // Remain stroked after the animation..
// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //kCAMediaTimingFunctionEaseIn
// Add the animation to the circle
[self.circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}
- (void)ResetAnimation
{
[self.circle removeAllAnimations];
[self.circle removeFromSuperlayer];
}
- (void)setReachedInPercent:(NSNumber*)reachedInPercent
{
_reachedInPercent = [NSNumber numberWithFloat:MIN([reachedInPercent floatValue], 1.0f)];
}
- (void) updateGoalText
{
self.goalText.text = [NSString stringWithFormat:NSLocalizedString(@"of %lu", nil), (unsigned long)[AppSettings getInstance].goal];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
}
*/
@end