{"id":386,"date":"2025-06-02T10:56:04","date_gmt":"2025-06-05T03:25:04","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=386"},"modified":"2025-06-05T10:56:04","modified_gmt":"2025-06-05T03:25:04","slug":"swift-timer-%e8%a8%88%e6%99%82%e5%99%a8-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-timer-%e8%a8%88%e6%99%82%e5%99%a8-%e7%af%84%e4%be%8b\/","title":{"rendered":"2025 \u6700\u65b0 Swift Timer \u8a08\u6642\u5668 \u5b8c\u6574\u6559\u5b78\u7bc4\u4f8b"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>\u524d\u8a00<\/h1>\n<p>\u5728 Swift \u4e2d\uff0cTimer\uff08\u8a08\u6642\u5668\uff09\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5728\u6307\u5b9a\u7684\u6642\u9593\u9593\u9694\u5167\u57f7\u884c\u67d0\u500b\u51fd\u6578\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 Timer\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4ee5\u53ca\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<h2>Swift Timer \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Timer \u4f7f\u6211\u5011\u80fd\u5920\u4ee5\u7c21\u55ae\u7684\u65b9\u5f0f\u57f7\u884c\u5b9a\u6642\u4efb\u52d9\u3002\u8207 Objective-C \u4e2d\u7684 NSTimer \u76f8\u6bd4\uff0cSwift \u7684 Timer \u63d0\u4f9b\u4e86\u66f4\u7c21\u6f54\u7684\u8a9e\u6cd5\u3002<\/p>\n<h2>\u4f7f\u7528 Timer \u7684\u57fa\u672c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u7bc4\u4f8b\u5c07\u5c55\u793a\u5982\u4f55\u6bcf\u4e94\u79d2\u9418\u81ea\u52d5\u91cd\u65b0\u52a0\u8f09\u4e00\u500b\u8868\u683c\u8996\u5716\uff08UITableView\uff09\u3002<\/p>\n<h3>Timer \u7684\u5ba3\u544a\u8207\u555f\u52d5<\/h3>\n<p>&#8220;`swift<br \/>\n\/\/ \u6bcf5\u79d2\u5faa\u74b0\u4e00\u6b21\uff0c\u547c\u53eb reloadTableView \u51fd\u6578<br \/>\nvar timer: Timer?<\/p>\n<p>func startTimer() {<br \/>\n    timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(reloadTableView), userInfo: nil, repeats: true)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u547c\u53eb\u51fd\u6578\u90e8\u5206<\/h3>\n<p>&#8220;`swift<br \/>\n@objc func reloadTableView() {<br \/>\n    tableview.reloadData()<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u505c\u6b62 Timer<\/h3>\n<p>\u7576\u6211\u5011\u4e0d\u518d\u9700\u8981\u8a08\u6642\u5668\u6642\uff0c\u61c9\u8a72\u505c\u6b62\u5b83\uff0c\u9019\u6a23\u53ef\u4ee5\u91cb\u653e\u8cc7\u6e90\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nfunc stopTimer() {<br \/>\n    timer?.invalidate()<br \/>\n    timer = nil<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>1. **Timer \u672a\u80fd\u555f\u52d5**\uff1a\u78ba\u4fdd\u547c\u53eb\u4e86 `startTimer()` \u51fd\u6578\uff0c\u4e26\u4e14\u7576\u524d\u5c0d\u8c61\u6c92\u6709\u88ab\u91cb\u653e\u3002<br \/>\n2. **\u51fd\u6578\u672a\u88ab\u547c\u53eb**\uff1a\u6aa2\u67e5 selector \u662f\u5426\u6b63\u78ba\uff0c\u4e26\u4e14\u51fd\u6578\u7c3d\u540d\u5fc5\u9808\u5e36\u6709 `@objc` \u6a19\u8a18\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>Timer \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u91cd\u8907\u7684\u4efb\u52d9\uff0c\u4e5f\u53ef\u4ee5\u8a2d\u7f6e\u5ef6\u9072\u4efb\u52d9\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nfunc scheduleDelayedTask() {<br \/>\n    Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { timer in<br \/>\n        print(&#8220;10\u79d2\u5f8c\u57f7\u884c\u7684\u4efb\u52d9&#8221;)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5728 10 \u79d2\u5f8c\u57f7\u884c\u6307\u5b9a\u7684\u4efb\u52d9\u3002<\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><br \/>\n    <img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift Timer \u8a08\u6642\u5668\u7684\u4f7f\u7528\u7bc4\u4f8b\" title=\"\"><br \/>\n<\/a><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: Timer \u53ef\u4ee5\u5728\u80cc\u666f\u57f7\u884c\u55ce\uff1f<\/h3>\n<p>A1: Timer \u5728\u61c9\u7528\u7a0b\u5f0f\u9032\u5165\u80cc\u666f\u5f8c\u5c07\u6703\u88ab\u66ab\u505c\u3002\u5982\u679c\u9700\u8981\u5728\u80cc\u666f\u57f7\u884c\u8a08\u6642\u4efb\u52d9\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `DispatchSourceTimer` \u6216 `Background Tasks`\u3002<\/p>\n<h3>Q2: Timer \u7684\u7cbe\u78ba\u5ea6\u5982\u4f55\uff1f<\/h3>\n<p>A2: Timer \u7684\u7cbe\u78ba\u5ea6\u53d6\u6c7a\u65bc\u7cfb\u7d71\u8cc7\u6e90\u548c\u904b\u884c\u74b0\u5883\u3002\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0cTimer \u53ef\u80fd\u6703\u6709\u5ef6\u9072\uff0c\u5c24\u5176\u662f\u5728\u4e3b\u57f7\u884c\u7dd2\u4e0a\u57f7\u884c\u5927\u91cf\u8a08\u7b97\u6642\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5728 Timer \u4e2d\u50b3\u905e\u53c3\u6578\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\u4f7f\u7528 `userInfo` \u53c3\u6578\u4f86\u50b3\u905e\u984d\u5916\u7684\u6578\u64da\u3002\u9019\u6a23\u5728\u89f8\u767c\u7684\u51fd\u6578\u4e2d\u53ef\u4ee5\u7372\u53d6\u9019\u4e9b\u6578\u64da\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Timer \u5728OC\u61c9\u8a72\u662fNSTimer<br \/>\n\u5728Swift\u5c31\u662f\u7c21\u5316\u6210Timer<br \/>\n\u4e3b\u8981\u529f\u7528\u5728\u8a08\u6642\u4f7f\u7528<br \/>\n\u56fa\u5b9a\u6642\u9593\u547c\u53eb\u67d0func \u5e7e\u500b\u5faa\u74b0<br \/>\n\u6216\u662f\u5ef6\u9072\u5e7e\u5999\u5f8c\u958b\u59cb\u8a08\u6642<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-386","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-6e","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":7564,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/386\/revisions\/7564"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}