{"id":7202,"date":"2025-05-29T15:00:25","date_gmt":"2025-06-05T07:19:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7202"},"modified":"2025-06-05T15:00:25","modified_gmt":"2025-06-05T07:19:24","slug":"https-badgameshow-com-steven-274","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-274\/","title":{"rendered":"Swift UISwitch \u958b\u95dc\u63a7\u5236\u5668\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UISwitch, \u958b\u95dc\u63a7\u5236\u5668, iOS \u958b\u767c, Swift \u6559\u5b78\"><\/p>\n<h1>Swift UISwitch \u958b\u95dc\u63a7\u5236\u5668\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f Apple \u958b\u767c\u7684\u4e00\u7a2e\u958b\u653e\u6e90\u78bc\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4e3b\u8981\u7528\u65bc\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u3002UISwitch \u662f iOS \u4e2d\u7684\u4e00\u7a2e\u958b\u95dc\u63a7\u5236\u5668\uff0c\u901a\u5e38\u7528\u4f86\u63a7\u5236\u7279\u5b9a\u8a2d\u5b9a\u6216\u529f\u80fd\u7684\u958b\u95dc\u72c0\u614b\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 UISwitch\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u53ca\u932f\u8aa4\u6392\u9664\u6307\u5357\u3002<\/p>\n<h2>UISwitch \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>\u5728 Swift \u4e2d\u5275\u5efa\u4e00\u500b UISwitch \u63a7\u5236\u5668\u76f8\u5c0d\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5275\u5efa UISwitch \u4e26\u5c07\u5176\u6dfb\u52a0\u5230\u754c\u9762\u4e0a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let switchControl = UISwitch()\nswitchControl.frame = CGRect(x: 0, y: 0, width: 50, height: 30)\nview.addSubview(switchControl)<\/code><\/pre>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 UISwitch \u7684 <code>isOn<\/code> \u5c6c\u6027\u4f86\u6aa2\u67e5\u958b\u95dc\u662f\u5426\u5df2\u6253\u958b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if switchControl.isOn {\n    \/\/ Do something when switch is ON\n}<\/code><\/pre>\n<h2>\u76e3\u807d UISwitch \u7684\u8b8a\u5316<\/h2>\n<p>\u70ba\u4e86\u97ff\u61c9 UISwitch \u7684\u72c0\u614b\u8b8a\u5316\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>addTarget<\/code> \u65b9\u6cd5\u4f86\u8a2d\u7f6e\u4e00\u500b\u56de\u8abf\uff0c\u7576\u958b\u95dc\u72c0\u614b\u6539\u8b8a\u6642\uff0c\u9019\u500b\u56de\u8abf\u5c07\u88ab\u89f8\u767c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">switchControl.addTarget(self, action: #selector(switchChanged(_:)), for: .valueChanged)\n\n@objc func switchChanged(_ sender: UISwitch) {\n    if sender.isOn {\n        print(\"Switch is ON\")\n    } else {\n        print(\"Switch is OFF\")\n    }\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c UISwitch \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u4f8b\u5982\u63a7\u5236\u5668\u672a\u6b63\u78ba\u986f\u793a\u6216\u56de\u8abf\u672a\u89f8\u767c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6392\u9664\u932f\u8aa4\u7684\u6280\u5de7\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd UISwitch \u7684 frame \u8a2d\u7f6e\u6b63\u78ba\uff0c\u4e26\u4e14\u5df2\u7d93\u6dfb\u52a0\u5230\u6b63\u78ba\u7684\u8996\u5716\u4e2d\u3002<\/li>\n<li>\u6aa2\u67e5\u60a8\u7684 @objc \u65b9\u6cd5\u662f\u5426\u6b63\u78ba\u9023\u63a5\u5230 UISwitch \u7684\u4e8b\u4ef6\u3002<\/li>\n<li>\u5728\u8abf\u8a66\u6642\uff0c\u4f7f\u7528 <code>print()<\/code> \u8a9e\u53e5\u4f86\u78ba\u4fdd\u56de\u8abf\u51fd\u6578\u88ab\u547c\u53eb\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UISwitch \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u7c21\u55ae\u7684\u958b\u95dc\u529f\u80fd\uff0c\u9084\u53ef\u4ee5\u8207\u5176\u4ed6 UI \u5143\u7d20\u7d50\u5408\u4f7f\u7528\uff0c\u5275\u5efa\u66f4\u8907\u96dc\u7684\u4e92\u52d5\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 UISwitch \u4f86\u63a7\u5236\u4e00\u500b\u97f3\u91cf\u6ed1\u584a\u7684\u958b\u95dc\uff0c\u6216\u662f\u7528\u65bc\u555f\u7528\/\u7981\u7528\u7279\u5b9a\u529f\u80fd\u7684\u9078\u9805\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UISwitch-\ud83d\udca1\u958b\u95dc\u63a7\u5236\u5668\uff01_1675404758.6911418.webp\" alt=\"Swift UISwitch \ud83d\udca1\u958b\u95dc\u63a7\u5236\u5668\uff01\" title=\"Swift UISwitch, \u958b\u95dc\u63a7\u5236\u5668, Swift\u4e2d\u7684UISwitch, UISwitch\u529f\u80fd, UISwitch\u4f7f\u7528\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UISwitch \u5982\u4f55\u8a2d\u7f6e\u521d\u59cb\u72c0\u614b\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u900f\u904e\u8a2d\u7f6e <code>switchControl.isOn = true<\/code> \u6216 <code>switchControl.isOn = false<\/code> \u4f86\u8a2d\u7f6e UISwitch \u7684\u521d\u59cb\u72c0\u614b\u3002<\/p>\n<h3>2. UISwitch \u53ef\u4ee5\u8207\u5176\u4ed6\u63a7\u5236\u9805\u4e00\u8d77\u4f7f\u7528\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5c07 UISwitch \u8207 UILabel\u3001UISlider \u7b49\u5176\u4ed6\u63a7\u4ef6\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u5be6\u73fe\u66f4\u8c50\u5bcc\u7684\u7528\u6236\u754c\u9762\u3002<\/p>\n<h3>3. \u6211\u5982\u4f55\u5728 SwiftUI \u4e2d\u4f7f\u7528\u958b\u95dc\u63a7\u5236\u5668\uff1f<\/h3>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>Toggle<\/code> \u7d44\u4ef6\u4f86\u5be6\u73fe\u985e\u4f3c\u65bc UISwitch \u7684\u529f\u80fd\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u7c21\u55ae\u5730\u9032\u884c\u72c0\u614b\u7ba1\u7406\u3002<\/p>\n<p>\u901a\u904e\u9019\u7bc7\u6587\u7ae0\uff0c\u60a8\u61c9\u8a72\u5c0d Swift \u4e2d\u7684 UISwitch \u63a7\u5236\u5668\u6709\u4e86\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u4e26\u80fd\u5920\u5be6\u73fe\u5404\u7a2e\u529f\u80fd\uff0c\u63d0\u5347\u60a8\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\u7684\u6548\u7387\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:Swift\u4e2d\u7684UISwitch\u958b\u95dc\u63a7\u5236\u5668\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u63a7\u5236App\u7684\u529f\u80fd\uff0c\u672c\u6587\u5c07\u4ecb\u7d39UISwitch\u958b\u95dc\u63a7\u5236\u5668\u7684\u57fa\u672c\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"author":1,"featured_media":7201,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7202","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UISwitch-\ud83d\udca1\u958b\u95dc\u63a7\u5236\u5668\uff01_1675404758.6911418.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Sa","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7202","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=7202"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7202\/revisions"}],"predecessor-version":[{"id":13099,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7202\/revisions\/13099"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7201"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}