口袋优选

WLButtonCountdownManager.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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, atomic) NSTimeInterval leftTimeInterval;
  23. /**
  24. * 后台任务标识,确保程序进入后台依然能够计时
  25. */
  26. @property (assign, nonatomic) UIBackgroundTaskIdentifier taskIdentifier;
  27. /**
  28. * `NSOperation`的`name`属性只在iOS8+中存在,这里定义一个属性,用来兼容 iOS 7
  29. */
  30. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
  31. @property (copy) NSString *name;
  32. #endif
  33. @end
  34. @implementation WLCountdownTask
  35. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
  36. @synthesize name;
  37. #endif
  38. - (void)dealloc {
  39. _countingDownBlcok = nil;
  40. _finishedBlcok = nil;
  41. }
  42. - (void)main {
  43. self.taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
  44. do {
  45. dispatch_sync(dispatch_get_main_queue(), ^{
  46. if (self.countingDownBlcok) self.countingDownBlcok(self.leftTimeInterval);
  47. });
  48. [NSThread sleepForTimeInterval:1];
  49. } while (--self.leftTimeInterval > 0);
  50. dispatch_sync(dispatch_get_main_queue(), ^{
  51. if (self.finishedBlcok) {
  52. self.finishedBlcok(0);
  53. }
  54. });
  55. if (self.taskIdentifier != UIBackgroundTaskInvalid) {
  56. [[UIApplication sharedApplication] endBackgroundTask:self.taskIdentifier];
  57. self.taskIdentifier = UIBackgroundTaskInvalid;
  58. }
  59. }
  60. @end
  61. @interface WLButtonCountdownManager ()
  62. @property (nonatomic, strong) NSOperationQueue *pool;
  63. @end
  64. @implementation WLButtonCountdownManager
  65. + (instancetype)defaultManager {
  66. static id instance = nil;
  67. static dispatch_once_t onceToken;
  68. dispatch_once(&onceToken, ^{
  69. instance = [[self _alloc] _init];
  70. });
  71. if ([instance class] != [WLButtonCountdownManager class]) {
  72. NSCAssert(NO, @"该类不允许被继承");
  73. }
  74. return instance;
  75. }
  76. + (instancetype)alloc {
  77. NSCAssert(NO, @"请使用`+defaultManager`方法获取实例");
  78. return nil;
  79. }
  80. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  81. NSCAssert(NO, @"请使用`+defaultManager`方法获取实例");
  82. return nil;
  83. }
  84. + (instancetype)_alloc {
  85. return [super allocWithZone:NSDefaultMallocZone()];
  86. }
  87. - (instancetype)init {
  88. NSCAssert(NO, @"请使用`+defaultManager`方法获取实例");
  89. return nil;
  90. }
  91. - (instancetype)_init {
  92. if (self = [super init]) {
  93. _pool = [[NSOperationQueue alloc] init];
  94. }
  95. return self;
  96. }
  97. - (void)scheduledCountDownWithKey:(NSString *)aKey
  98. timeInterval:(NSTimeInterval)timeInterval
  99. countingDown:(void (^)(NSTimeInterval))countingDown
  100. finished:(void (^)(NSTimeInterval))finished
  101. {
  102. if (timeInterval > 120) {
  103. NSCAssert(NO, @"受操作系统后台时间限制,倒计时时间规定不得大于 120 秒.");
  104. }
  105. if (_pool.operations.count >= 20) // 最多 20 个并发线程
  106. return;
  107. WLCountdownTask *task = nil;
  108. if ([self countdownTaskExistWithKey:aKey task:&task]) {
  109. task.countingDownBlcok = countingDown;
  110. task.finishedBlcok = finished;
  111. if (countingDown && task.leftTimeInterval > 0) {
  112. countingDown(task.leftTimeInterval);
  113. }
  114. } else {
  115. task = [[WLCountdownTask alloc] init];
  116. task.leftTimeInterval = timeInterval;
  117. task.countingDownBlcok = countingDown;
  118. task.finishedBlcok = finished;
  119. task.name = aKey;
  120. [_pool addOperation:task];
  121. }
  122. }
  123. - (BOOL)countdownTaskExistWithKey:(NSString *)akey
  124. task:(WLCountdownTask *__autoreleasing _Nullable *)task
  125. {
  126. __block BOOL taskExist = NO;
  127. [_pool.operations enumerateObjectsUsingBlock:^(__kindof WLCountdownTask * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  128. if ([obj.name isEqualToString:akey]) {
  129. if (task) *task = obj;
  130. taskExist = YES;
  131. *stop = YES;
  132. }
  133. }];
  134. return taskExist;
  135. }
  136. @end