-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDDateCountdownFlipView.m
executable file
·177 lines (144 loc) · 5.77 KB
/
JDDateCountdownFlipView.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// JDCountdownFlipView.m
//
// Created by Markus Emrich on 12.03.11.
// Copyright 2011 Markus Emrich. All rights reserved.
//
#import "JDDateCountdownFlipView.h"
static CGFloat kFlipAnimationUpdateInterval = 0.5; // = 2 times per second
@interface JDDateCountdownFlipView ()
@property (nonatomic) NSInteger dayDigitCount;
@property (nonatomic, strong) JDFlipNumberView* dayFlipNumberView;
@property (nonatomic, strong) JDFlipNumberView* hourFlipNumberView;
@property (nonatomic, strong) JDFlipNumberView* minuteFlipNumberView;
@property (nonatomic, strong) JDFlipNumberView* secondFlipNumberView;
@property (nonatomic, strong) NSTimer *animationTimer;
- (void)setupUpdateTimer;
- (void)handleTimer:(NSTimer*)timer;
@end
@implementation JDDateCountdownFlipView
- (id)init
{
return [self initWithDayDigitCount:3];
}
- (id)initWithFrame:(CGRect)frame
{
self = [self initWithDayDigitCount:3];
if (self) {
self.frame = frame;
}
return self;
}
- (id)initWithDayDigitCount:(NSInteger)dayDigits;
{
self = [super initWithFrame: CGRectZero];
if (self) {
_dayDigitCount = dayDigits;
// view setup
self.backgroundColor = [UIColor clearColor];
self.autoresizesSubviews = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
// setup flipviews
self.dayFlipNumberView = [[JDFlipNumberView alloc] initWithDigitCount:_dayDigitCount];
self.hourFlipNumberView = [[JDFlipNumberView alloc] initWithDigitCount:2];
self.minuteFlipNumberView = [[JDFlipNumberView alloc] initWithDigitCount:2];
self.secondFlipNumberView = [[JDFlipNumberView alloc] initWithDigitCount:2];
self.hourFlipNumberView.maximumValue = 23;
self.minuteFlipNumberView.maximumValue = 59;
self.secondFlipNumberView.maximumValue = 59;
[self setZDistance: 60];
// set inital frame
CGRect frame = self.hourFlipNumberView.frame;
self.frame = CGRectMake(0, 0, frame.size.width*(dayDigits+7), frame.size.height);
// add subviews
for (JDFlipNumberView* view in @[self.dayFlipNumberView, self.hourFlipNumberView, self.minuteFlipNumberView, self.secondFlipNumberView]) {
[self addSubview:view];
}
// set inital dates
self.targetDate = [NSDate date];
[self setupUpdateTimer];
}
return self;
}
#pragma mark setter
- (void)setZDistance:(NSUInteger)zDistance;
{
for (JDFlipNumberView* view in @[self.dayFlipNumberView, self.hourFlipNumberView, self.minuteFlipNumberView, self.secondFlipNumberView]) {
[view setZDistance:zDistance];
}
}
- (void)setFrame:(CGRect)frame;
{
if (self.dayFlipNumberView == nil) {
[super setFrame:frame];
return;
}
CGFloat digitWidth = frame.size.width/(self.dayFlipNumberView.digitCount+7);
CGFloat margin = digitWidth/3.0;
CGFloat currentX = 0;
// resize first flipview
self.dayFlipNumberView.frame = CGRectMake(0, 0, digitWidth * self.dayDigitCount, frame.size.height);
currentX += self.dayFlipNumberView.frame.size.width;
// update flipview frames
for (JDFlipNumberView* view in @[self.hourFlipNumberView, self.minuteFlipNumberView, self.secondFlipNumberView]) {
currentX += margin;
view.frame = CGRectMake(currentX, 0, digitWidth*2, frame.size.height);
currentX += view.frame.size.width;
}
// take bottom right of last view for new size, to match size of subviews
CGRect lastFrame = self.secondFlipNumberView.frame;
frame.size.width = ceil(lastFrame.size.width + lastFrame.origin.x);
frame.size.height = ceil(lastFrame.size.height + lastFrame.origin.y);
[super setFrame:frame];
}
- (void)setTargetDate:(NSDate *)targetDate;
{
_targetDate = targetDate;
[self updateValuesAnimated:NO];
}
#pragma mark update timer
- (void)start;
{
if (self.animationTimer == nil) {
[self setupUpdateTimer];
}
}
- (void)stop;
{
[self.animationTimer invalidate];
self.animationTimer = nil;
}
- (void)setupUpdateTimer;
{
self.animationTimer = [NSTimer timerWithTimeInterval:kFlipAnimationUpdateInterval
target:self
selector:@selector(handleTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.animationTimer forMode:NSRunLoopCommonModes];
}
- (void)handleTimer:(NSTimer*)timer;
{
[self updateValuesAnimated:YES];
}
- (void)updateValuesAnimated:(BOOL)animated;
{
if (self.targetDate == nil) {
return;
}
if ([self.targetDate timeIntervalSinceDate:[NSDate date]] > 0) {
NSUInteger flags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents* dateComponents = [[NSCalendar currentCalendar] components:flags fromDate:[NSDate date] toDate:self.targetDate options:0];
[self.dayFlipNumberView setValue:[dateComponents day] animated:animated];
[self.hourFlipNumberView setValue:[dateComponents hour] animated:animated];
[self.minuteFlipNumberView setValue:[dateComponents minute] animated:animated];
[self.secondFlipNumberView setValue:[dateComponents second] animated:animated];
} else {
[self.dayFlipNumberView setValue:0 animated:animated];
[self.hourFlipNumberView setValue:0 animated:animated];
[self.minuteFlipNumberView setValue:0 animated:animated];
[self.secondFlipNumberView setValue:0 animated:animated];
[self stop];
}
}
@end