forked from dogo/SCLAlertView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCLTimerDisplay.m
More file actions
133 lines (111 loc) · 3.66 KB
/
SCLTimerDisplay.m
File metadata and controls
133 lines (111 loc) · 3.66 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
//
// SCLTimerDisplay.m
// SCLAlertView
//
// Created by Taylor Ryan on 8/18/15.
// Copyright (c) 2015-2017 AnyKey Entertainment. All rights reserved.
//
#import "SCLTimerDisplay.h"
#import "SCLMacros.h"
@interface SCLTimerDisplay ()
@property (strong, nonatomic) UILabel *countLabel;
@end
@implementation SCLTimerDisplay
@synthesize currentAngle;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
currentAngle = 0.0f;
}
return self;
}
- (instancetype)initWithOrigin:(CGPoint)origin radius:(CGFloat)r
{
return [self initWithOrigin:(CGPoint)origin radius:r lineWidth:5.0f];
}
- (instancetype)initWithOrigin:(CGPoint)origin radius:(CGFloat)r lineWidth:(CGFloat)width
{
self = [super initWithFrame:CGRectMake(origin.x, origin.y, r*2, r*2)];
if (self) {
self.backgroundColor = [UIColor clearColor];
currentAngle = START_DEGREE_OFFSET;
radius = r-(width/2);
lineWidth = width;
self.color = [UIColor whiteColor];
self.userInteractionEnabled = NO;
// Add count label
_countLabel = [[UILabel alloc] init];
_countLabel.textColor = [UIColor whiteColor];
_countLabel.backgroundColor = [UIColor clearColor];
_countLabel.font = [UIFont fontWithName: @"HelveticaNeue-Bold" size:12.0f];
_countLabel.textAlignment = NSTextAlignmentCenter;
_countLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self addSubview:_countLabel];
}
return self;
}
- (void)updateFrame:(CGSize)size
{
CGFloat r = radius+(lineWidth/2);
CGFloat originX = size.width - (2*r) - 5;
CGFloat originY = (size.height - (2*r))/2;
self.frame = CGRectMake(originX, originY, r*2, r*2);
self.countLabel.frame = CGRectMake(0, 0, r*2, r*2);
}
- (void)drawRect:(CGRect)rect
{
UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius+(lineWidth/2), radius+(lineWidth/2))
radius:radius
startAngle:DEGREES_TO_RADIANS(START_DEGREE_OFFSET)
endAngle:DEGREES_TO_RADIANS(currentAngle)
clockwise:YES];
[self.color setStroke];
aPath.lineWidth = lineWidth;
[aPath stroke];
_countLabel.text = [NSString stringWithFormat:@"%d", (int)currentTime];
}
- (void)startTimerWithTimeLimit:(int)tl completed:(SCLActionBlock)completed
{
if (_reverse)
{
currentTime = tl;
}
timerLimit = tl;
timer = [NSTimer scheduledTimerWithTimeInterval:TIMER_STEP target:self selector:@selector(updateTimerButton:) userInfo:nil repeats:YES];
completedBlock = completed;
_countLabel.textColor = _color;
}
- (void)cancelTimer
{
[timer invalidate];
}
- (void)stopTimer
{
[timer invalidate];
if (completedBlock != nil) {
completedBlock();
}
}
- (void)updateTimerButton:(NSTimer *)timer
{
if (_reverse)
{
currentTime -= TIMER_STEP;
currentAngle = (currentTime/timerLimit) * 360 + START_DEGREE_OFFSET;
if(currentTime <= 0) {
[self stopTimer];
}
}
else {
currentTime += TIMER_STEP;
currentAngle = (currentTime/timerLimit) * 360 + START_DEGREE_OFFSET;
if(currentAngle >= (360 + START_DEGREE_OFFSET)) {
[self stopTimer];
}
}
[self setNeedsDisplay];
}
@end