-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFTimeCountDownManager.m
More file actions
161 lines (137 loc) · 4.02 KB
/
DFTimeCountDownManager.m
File metadata and controls
161 lines (137 loc) · 4.02 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
156
157
158
159
160
161
//
// DFTimeCountDownManager.m
// DevilFinger Team Project
//
// Created by DevilFinger on 17/6/1.
// Copyright © 2017年 DevilFinger Team. All rights reserved.
//
#import "DFTimeCountDownManager.h"
@implementation DFTimeCountDownManager
+ (nonnull instancetype)sharedManager {
static dispatch_once_t once;
static DFTimeCountDownManager *sharedManager;
dispatch_once(&once, ^{
sharedManager = [DFTimeCountDownManager new];
});
return sharedManager;
}
-(instancetype)init
{
self = [super init];
if (self) {
self.isCountDown = YES;
self.isCycle = NO;
self.isCouning = NO;
self.perCountDownTime = 1.0f;
self.minCountDownTime = 0.0f;
self.maxCountDownTime = 15.0f;
self.currentTime = self.maxCountDownTime;
}
return self;
}
-(void)doAction
{
self.isCouning = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(counting) object:nil];
self.isCouning = YES;
[self performSelector:@selector(counting) withObject:nil afterDelay:self.perCountDownTime];
}
-(void)cancelAction
{
self.isCouning = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(counting) object:nil];
if (self.countDownDelegate && [self.countDownDelegate respondsToSelector:@selector(countDidCancelWithValue:)]) {
[self.countDownDelegate countDidCancelWithValue:self.currentTime];
self.countDownDelegate = nil;
}
}
-(void)redoAction
{
self.isCouning = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(counting) object:nil];
if (self.isCountDown) {
self.currentTime = self.maxCountDownTime;
}
else
self.currentTime = self.minCountDownTime;
if (self.countDownDelegate && [self.countDownDelegate respondsToSelector:@selector(countDidDoWithValue:)]) {
[self.countDownDelegate countDidDoWithValue:self.currentTime];
}
[self doAction];
}
-(void)counting
{
NSLog(@"counting");
if (self.isCountDown) {
self.currentTime--;
}
else
self.currentTime++;
if (self.isCycle) {
[self setCycleTime];
if (self.countDownDelegate && [self.countDownDelegate respondsToSelector:@selector(countDidDoWithValue:)]) {
[self.countDownDelegate countDidDoWithValue:self.currentTime];
}
[self doAction];
}
else
{
if ([self isToEnd]) {
[self cancelAction];
if (self.countDownDelegate && [self.countDownDelegate respondsToSelector:@selector(countDidStopWithValue:)]) {
[self.countDownDelegate countDidStopWithValue:self.currentTime];
}
}
else
{
if (self.countDownDelegate && [self.countDownDelegate respondsToSelector:@selector(countDidDoWithValue:)]) {
[self.countDownDelegate countDidDoWithValue:self.currentTime];
}
[self doAction];
}
}
}
-(void)refreshImmediatel
{
if (self.countDownDelegate && [self.countDownDelegate respondsToSelector:@selector(countDidDoWithValue:)]) {
[self.countDownDelegate countDidDoWithValue:self.currentTime];
}
}
-(BOOL)isToEnd
{
if (self.isCountDown) {
if (self.currentTime <= self.minCountDownTime) {
return YES;
}
else
return NO;
}
else
{
if (self.currentTime >= self.maxCountDownTime) {
return YES;
}
else
return NO;
}
}
-(void)setCycleTime
{
if (self.currentTime < self.minCountDownTime) {
self.currentTime = self.maxCountDownTime;
}
if (self.currentTime > self.maxCountDownTime) {
self.currentTime = self.minCountDownTime;
}
}
-(void)setCountDownDelegate:(id<DFTimeCountDownManagerDelegate>)countDownDelegate
{
if (_countDownDelegate) {
if (self.isCouning) {
[self cancelAction];
}
_countDownDelegate = nil;
}
_countDownDelegate = countDownDelegate;
}
@end