WLButtonCountdownManager.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // WLButtonCountdownManager.m
  3. // WLButtonCountingDownDemo
  4. //
  5. // Created by wayne on 16/1/14.
  6. // Copyright © 2016年 ZHWAYNE. All rights reserved.
  7. //
  8. #import "WLButtonCountdownManager.h"
  9. #import <UIKit/UIKit.h>
  10. @interface WLCountdownTask : NSOperation
  11. /**
  12. * 计时中回调
  13. */
  14. @property (copy, nonatomic) void (^countingDownBlcok)(NSTimeInterval timeInterval);
  15. /**
  16. * 计时结束后回调
  17. */
  18. @property (copy, nonatomic) void (^finishedBlcok)(NSTimeInterval timeInterval);
  19. /**
  20. * 计时剩余时间
  21. */
  22. @property (assign, nonatomic) NSTimeInterval leftTimeInterval;
  23. /**
  24. * 后台任务标识,确保程序进入后台依然能够计时
  25. */
  26. @property (assign, nonatomic) UIBackgroundTaskIdentifier taskIdentifier;
  27. /**
  28. * `NSOperation`的`name`属性只在iOS8+中存在,这里定义一个属性,用来兼容 iOS7
  29. */
  30. @property (copy, nonatomic) NSString *operationName;
  31. @end
  32. @implementation WLCountdownTask
  33. - (void)dealloc {
  34. _countingDownBlcok = nil;
  35. _finishedBlcok = nil;
  36. }
  37. - (void)main {
  38. self.taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
  39. while (--_leftTimeInterval > 0) {
  40. dispatch_async(dispatch_get_main_queue(), ^{
  41. if (self.countingDownBlcok) self.countingDownBlcok(self.leftTimeInterval);
  42. });
  43. [NSThread sleepForTimeInterval:1];
  44. }
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. if (self.finishedBlcok) {
  47. self.finishedBlcok(0);
  48. }
  49. });
  50. if (self.taskIdentifier != UIBackgroundTaskInvalid) {
  51. [[UIApplication sharedApplication] endBackgroundTask:self.taskIdentifier];
  52. self.taskIdentifier = UIBackgroundTaskInvalid;
  53. }
  54. }
  55. @end
  56. @interface WLButtonCountdownManager ()
  57. @property (nonatomic, strong) NSOperationQueue *pool;
  58. @end
  59. @implementation WLButtonCountdownManager
  60. + (instancetype)defaultManager {
  61. static id instance = nil;
  62. static dispatch_once_t onceToken;
  63. dispatch_once(&onceToken, ^{
  64. instance = [[self _alloc] _init];
  65. });
  66. if ([instance class] != [WLButtonCountdownManager class]) {
  67. NSCAssert(NO, @"该类不允许被继承");
  68. }
  69. return instance;
  70. }
  71. + (instancetype)alloc {
  72. NSCAssert(NO, @"请使用`+defaultManager`方法获取实例");
  73. return nil;
  74. }
  75. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  76. NSCAssert(NO, @"请使用`+defaultManager`方法获取实例");
  77. return nil;
  78. }
  79. + (instancetype)_alloc {
  80. return [super allocWithZone:NSDefaultMallocZone()];
  81. }
  82. - (instancetype)init {
  83. NSCAssert(NO, @"请使用`+defaultManager`方法获取实例");
  84. return nil;
  85. }
  86. - (instancetype)_init {
  87. if (self = [super init]) {
  88. _pool = [[NSOperationQueue alloc] init];
  89. }
  90. return self;
  91. }
  92. - (void)scheduledCountDownWithKey:(NSString *)aKey
  93. timeInterval:(NSTimeInterval)timeInterval
  94. countingDown:(void (^)(NSTimeInterval))countingDown
  95. finished:(void (^)(NSTimeInterval))finished
  96. {
  97. if (timeInterval > 120) {
  98. NSCAssert(NO, @"受操作系统后台时间限制,倒计时时间规定不得大于 120 秒.");
  99. }
  100. if (_pool.operations.count >= 20) // 最多 20 个并发线程
  101. return;
  102. WLCountdownTask *task = nil;
  103. if ([self countdownTaskExistWithKey:aKey task:&task]) {
  104. task.countingDownBlcok = countingDown;
  105. task.finishedBlcok = finished;
  106. if (countingDown) {
  107. countingDown(task.leftTimeInterval);
  108. }
  109. } else {
  110. task = [[WLCountdownTask alloc] init];
  111. task.leftTimeInterval = timeInterval;
  112. task.countingDownBlcok = countingDown;
  113. task.finishedBlcok = finished;
  114. if ([@([UIDevice currentDevice].systemVersion.doubleValue) compare:@(8)] == NSOrderedAscending) {
  115. task.operationName = aKey;
  116. }
  117. else {
  118. task.name = aKey;
  119. }
  120. [_pool addOperation:task];
  121. }
  122. }
  123. - (BOOL)countdownTaskExistWithKey:(NSString *)akey
  124. task:(WLCountdownTask *__autoreleasing _Nullable *)task
  125. {
  126. __block BOOL taskExist = NO;
  127. if ([@([UIDevice currentDevice].systemVersion.doubleValue) compare:@(8)] == NSOrderedAscending) {
  128. [_pool.operations enumerateObjectsUsingBlock:^(__kindof WLCountdownTask * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  129. if ([obj.operationName isEqualToString:akey]) {
  130. if (task) *task = obj;
  131. taskExist = YES;
  132. *stop = YES;
  133. }
  134. }];
  135. }
  136. else {
  137. [_pool.operations enumerateObjectsUsingBlock:^(__kindof WLCountdownTask * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  138. if ([obj.name isEqualToString:akey]) {
  139. if (task) *task = obj;
  140. taskExist = YES;
  141. *stop = YES;
  142. }
  143. }];
  144. }
  145. return taskExist;
  146. }
  147. @end