forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOTPAuthClock.m
More file actions
executable file
·155 lines (137 loc) · 5.05 KB
/
Copy pathOTPAuthClock.m
File metadata and controls
executable file
·155 lines (137 loc) · 5.05 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
153
154
155
//
// OTPAuthClock.m
//
// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//
#import "OTPAuthClock.h"
#import "UIColor+MobileColors.h"
@interface OTPAuthClock ()
@property (nonatomic, strong, readwrite) NSTimer *timer;
@property (nonatomic, assign, readwrite) NSTimeInterval period;
- (void)startUpTimer;
@end
@implementation OTPAuthClock
- (id)initWithFrame:(CGRect)frame period:(NSTimeInterval)period {
if ((self = [super initWithFrame:frame])) {
[self startUpTimer];
self.opaque = NO;
self.period = period;
UIApplication *app = [UIApplication sharedApplication];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:app];
[nc addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)redrawTimer:(NSTimer *)timer {
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
CGFloat mod = fmod(seconds, self.period);
CGFloat percent = mod / self.period;
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect bounds = self.bounds;
[[UIColor clearColor] setFill];
CGContextFillRect(context, rect);
CGFloat midX = CGRectGetMidX(bounds);
CGFloat midY = CGRectGetMidY(bounds);
CGFloat radius = midY - 4;
//Draw bg
CGContextMoveToPoint(context, midX, midY);
CGFloat start = -M_PI_2;
CGFloat end = 2 * M_PI;
CGFloat sweep = end * percent + start;
CGContextAddArc(context, midX, midY, radius, start, sweep, 1);
[[[UIColor blackColor] colorWithAlphaComponent:0.7] setFill];
CGContextFillPath(context);
if (percent > .875) {
CGContextMoveToPoint(context, midX, midY);
CGContextAddArc(context, midX, midY, radius, start, sweep, 1);
CGFloat alpha = (percent - .875) / .125;
[[[UIColor redColor] colorWithAlphaComponent:alpha * 0.5] setFill];
CGContextFillPath(context);
}
// Draw top shadow
// CGFloat offset = 0.25;
// CGFloat x = midX + (radius - offset) * cos(0 - M_PI_4);
// CGFloat y = midY + (radius - offset) * sin(0 - M_PI_4);
// [[UIColor blackColor] setStroke];
// CGContextMoveToPoint(context, x , y);
// CGContextAddArc(context,
// midX, midY, radius - offset, 0 - M_PI_4, 5.0 * M_PI_4, 1);
// CGContextStrokePath(context);
// Draw bottom highlight
// x = midX + (radius + offset) * cos(0 + M_PI_4);
// y = midY + (radius + offset) * sin(0 + M_PI_4);
// [[UIColor whiteColor] setStroke];
// CGContextMoveToPoint(context, x , y);
// CGContextAddArc(context,
// midX, midY, radius + offset, 0 + M_PI_4, 3.0 * M_PI_4, 0);
// CGContextStrokePath(context);
//
// // Draw face
// [[UIColor googleBlueTextColor] setStroke];
// CGContextMoveToPoint(context, midX + radius , midY);
// CGContextAddArc(context, midX, midY, radius, 0, 2.0 * M_PI, 1);
// CGContextStrokePath(context);
//
// if (percent > .875) {
// CGFloat alpha = (percent - .875) / .125;
// [[[UIColor redColor] colorWithAlphaComponent:alpha] setStroke];
// CGContextStrokePath(context);
// }
// Hand
// x = midX + radius * cos(sweep);
// y = midY + radius * sin(sweep);
// CGContextMoveToPoint(context, midX, midY);
// CGContextAddLineToPoint(context, x, y);
// CGContextStrokePath(context);
}
- (void)invalidate {
[self.timer invalidate];
self.timer = nil;
}
- (void)startUpTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(redrawTimer:)
userInfo:nil
repeats:YES];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self startUpTimer];
[self redrawTimer:nil];
}
- (void)applicationWillResignActive:(UIApplication *)application {
[self invalidate];
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[super willMoveToSuperview:newSuperview];
if (!newSuperview) {
[self invalidate];
}
}
@end