{"id":783,"date":"2016-10-12T09:43:40","date_gmt":"2016-10-12T01:43:40","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=783"},"modified":"2016-10-12T09:44:03","modified_gmt":"2016-10-12T01:44:03","slug":"%e8%87%aa%e5%ae%9a%e4%b9%89%e8%ae%a1%e6%97%b6%e5%99%a8%ef%bc%8c%e9%80%9a%e8%bf%87dispatch_after%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2016\/10\/12\/094340783.html","title":{"rendered":"\u81ea\u5b9a\u4e49\u8ba1\u65f6\u5668\uff0c\u901a\u8fc7dispatch_after()\u5b9e\u73b0"},"content":{"rendered":"<p><strong>1\u3001\u8bbe\u8ba1\u601d\u8def<\/strong><\/p>\n<p>1.1\u3001\u7cfb\u7edf\u7c7bNSTimer\u505a\u4e00\u6b21\u6027\u8ba1\u65f6\u4f7f\u7528\u8fd8\u53ef\u4ee5\uff0c\u5982\u679c\u9700\u8981\u6682\u505c\u7ee7\u7eed\u7684\u8bdd\u5c31\u4e0d\u592a\u7075\u6d3b\u4e86\uff1b<br \/>\n1.2\u3001\u901a\u8fc7dispatch_after\u5ef6\u65f6\u6267\u884c\u51fd\u6570\uff0c\u914d\u5957\u5faa\u73af\u8bed\u53e5\u7b97\u6cd5\u5b9e\u73b0\u95f4\u9694n\u79d2\u89e6\u53d1\u4e00\u6b21\u8c03\u7528\uff1b<br \/>\n1.3\u3001\u8bbe\u8ba1\u4e00\u4e2a\u72b6\u6001\u5c5e\u6027\uff0c\u5bf9\u5f53\u524d\u7684\u72b6\u6001\u8fdb\u884c\u7ba1\u7406\uff0c\u5982\u51c6\u5907\u3001\u6682\u505c\u3001\u7ee7\u7eed\u3001\u5f00\u59cb\u3001\u7ed3\u675f\u7b49\uff1b<\/p>\n<p><strong>2\u3001\u76f8\u5173\u5c5e\u6027\u4e0e\u65b9\u6cd5<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SATimer.h\n\/\/  Test\n\/\/\n\/\/  Created by \u4f59\u897f\u5b89 on 16\/8\/11.\n\/\/  Copyright ? 2016\u5e74 Sian. All rights reserved.\n\/\/\n\n#import <Foundation\/Foundation.h>\n\/\/ \u5b9a\u65f6\u5668\u72b6\u6001\ntypedef NS_ENUM(NSInteger, SATimerStatus){\n    SATimerStatusReady = 0, \/\/ \u51c6\u5907\n    SATimerStatusRunning,   \/\/ \u6b63\u5728\u8fd0\u884c\n    SATimerStatusPaused,    \/\/ \u5df2\u6682\u505c\uff0c\u53ef\u7ee7\u7eed\u8ba1\u65f6\n    SATimerStatusStoped     \/\/ \u5df2\u505c\u6b62\uff0c\u53ea\u80fd\u91cd\u65b0\u5f00\u59cb\n};\n\ntypedef void (^SATimerBlock)(NSUInteger remainCount);\n\n@interface SATimer : NSObject\n\/\/ \u95f4\u9694\u65f6\u95f4\n@property (nonatomic, assign) NSTimeInterval interval;\n\/\/ \u91cd\u590d\u6b21\u6570\n@property (nonatomic, assign) NSUInteger     repeatCount;\n\n@property (nonatomic, assign) SATimerStatus  status;\n\n+ (instancetype)timeWithInterval:(NSTimeInterval)ti repeatCount:(NSInteger)count block:(SATimerBlock)block;\n\n- (instancetype)initWithInterval:(NSTimeInterval)ti repeatCount:(NSInteger)count block:(SATimerBlock)block;\n\/\/ \u5f00\u59cb\uff08\u91cd\u65b0\u5f00\u59cb\uff09\n- (void)start;\n\/\/ \u6682\u505c\uff08\u53ef\u7ee7\u7eed\uff09\n- (void)pause;\n\/\/ \u7ee7\u7eed\n- (void)continues;\n\/\/ \u505c\u6b62\uff08\u4e0d\u53ef\u7ee7\u7eed\uff0c\u53ea\u80fd\u91cd\u65b0\u5f00\u59cb\uff09\n- (void)stop;\n\n@end\n<\/pre>\n<p><strong>3\u3001\u5b9e\u73b0<\/strong><!--more--><\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SATimer.m\n\/\/  Test\n\/\/\n\/\/  Created by \u4f59\u897f\u5b89 on 16\/8\/11.\n\/\/  Copyright ? 2016\u5e74 Sian. All rights reserved.\n\/\/\n\n#import \"SATimer.h\"\n\n@interface SATimer ()\n\n@property (nonatomic, assign) NSInteger  currentCount;\n\n@property (nonatomic, copy) SATimerBlock block;\n\n@end\n\n@implementation SATimer\n\n+ (instancetype)timeWithInterval:(NSTimeInterval)ti repeatCount:(NSInteger)count block:(SATimerBlock)block\n{\n    return [[self alloc] initWithInterval:ti repeatCount:count block:block];\n}\n\n- (instancetype)initWithInterval:(NSTimeInterval)ti repeatCount:(NSInteger)count block:(SATimerBlock)block\n{\n    if (self = [super init]){\n        self.interval = ti;\n        self.repeatCount = count;\n        self.currentCount = count;\n        self.block = block;\n    }\n    return self;\n}\n\n- (void)repeatMethod\n{\n    \n    if (self.currentCount <= 0 || self.status != SATimerStatusRunning) return;\n    self.currentCount--;\n    if (self.block) self.block(self.currentCount);\n    \n    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (self.interval * NSEC_PER_SEC));\n    dispatch_after(time, dispatch_get_main_queue(), ^{\n        \n        \n        [self repeatMethod];\n    });\n    \n}\n\n- (void)start\n{\n    if (self.status == SATimerStatusRunning) return;\n    self.currentCount = self.repeatCount;\n    self.status = SATimerStatusRunning;\n    [self repeatMethod];\n}\n\n- (void)continues\n{\n    if (self.status == SATimerStatusRunning || self.currentCount == 0) return;\n    self.status = SATimerStatusRunning;\n    [self repeatMethod];\n}\n\n- (void)pause\n{\n    self.status = SATimerStatusPaused;\n}\n\n- (void)stop\n{\n    self.status = SATimerStatusStoped;\n    self.currentCount = 0;\n}\n\n- (void)setCurrentCount:(NSInteger)currentCount\n{\n    self->_currentCount = currentCount;\n    if (currentCount == 0) self.status = SATimerStatusStoped;\n}\n\n- (void)dealloc\n{\n    NSLog(@\"%@---dealloc\", self);\n}\n\n@end\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u8bbe\u8ba1\u601d\u8def 1.1\u3001\u7cfb\u7edf\u7c7bNSTimer\u505a\u4e00\u6b21\u6027\u8ba1\u65f6\u4f7f\u7528\u8fd8\u53ef\u4ee5\uff0c\u5982\u679c\u9700\u8981\u6682\u505c\u7ee7\u7eed\u7684\u8bdd\u5c31\u4e0d\u592a\u7075\u6d3b\u4e86\uff1b 1.2\u3001\u901a\u8fc7dispatch_after\u5ef6\u65f6\u6267\u884c\u51fd\u6570\uff0c\u914d\u5957\u5faa\u73af\u8bed\u53e5\u7b97\u6cd5\u5b9e\u73b0\u95f4\u9694n\u79d2\u89e6\u53d1\u4e00\u6b21\u8c03\u7528\uff1b 1.3\u3001\u8bbe\u8ba1\u4e00\u4e2a\u72b6\u6001\u5c5e\u6027\uff0c\u5bf9\u5f53\u524d\u7684\u72b6\u6001\u8fdb\u884c\u7ba1\u7406\uff0c\u5982\u51c6\u5907\u3001\u6682\u505c\u3001\u7ee7\u7eed\u3001\u5f00\u59cb\u3001\u7ed3\u675f\u7b49\uff1b 2\u3001\u76f8\u5173\u5c5e\u6027\u4e0e\u65b9\u6cd5 \/\/ \/\/ SATimer.h \/\/ Test \/\/ \/\/ Created by \u4f59\u897f\u5b89 on 16\/8\/11. \/\/ Copyright ? 2016\u5e74 Sian. All rights reserved. \/\/ #import \/\/ \u5b9a\u65f6\u5668\u72b6\u6001 typedef NS_ENUM(NSInteger, SATimerStatus){ SATimerStatusReady = 0, \/\/ \u51c6\u5907 SATimerStatusRunning, \/\/ \u6b63\u5728\u8fd0\u884c SATimerStatusPaused, \/\/ \u5df2\u6682\u505c\uff0c\u53ef\u7ee7\u7eed\u8ba1\u65f6 SATimerStatusStoped \/\/ \u5df2\u505c\u6b62\uff0c\u53ea\u80fd\u91cd\u65b0\u5f00\u59cb }; typedef void (^SATimerBlock)(NSUInteger remainCount); @interface SATimer : NSObject [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[17],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-project","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}