{"id":7238,"date":"2025-06-04T12:08:14","date_gmt":"2025-06-05T04:40:13","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/uncategorized\/https-badgameshow-com-steven-292\/"},"modified":"2025-06-05T12:08:14","modified_gmt":"2025-06-05T04:40:13","slug":"https-badgameshow-com-steven-292","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-292\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Swift \u81ea\u5b9a\u7fa9\u6309\u9215\u8a2d\u8a08\u6559\u5b78 \ud83c\udfa8"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u81ea\u5b9a\u7fa9\u6309\u9215, \u6309\u9215\u8a2d\u8a08, Swift \u6559\u5b78\"><\/p>\n<h2>\u4ec0\u9ebc\u662f Swift \u81ea\u5b9a\u7fa9\u6309\u9215\uff1f<\/h2>\n<p>Swift \u81ea\u5b9a\u7fa9\u6309\u9215 \ud83c\udfa8 \u662f\u4e00\u500b\u975e\u5e38\u6709\u8da3\u7684\u8a71\u984c\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u81ea\u7531\u5275\u9020\u51fa\u5c6c\u65bc\u81ea\u5df1\u7684\u6309\u9215\uff0c\u8b93\u4f60\u7684 App \u754c\u9762\u66f4\u52a0\u7368\u7279\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u6703\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5275\u5efa\u81ea\u5b9a\u7fa9\u6309\u9215\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e00\u4e9b\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u4f9b\u53c3\u8003\uff0c\u5e6b\u52a9\u4f60\u638c\u63e1\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5275\u5efa\u81ea\u5b9a\u7fa9\u6309\u9215\u7684\u6b65\u9a5f<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u5275\u5efa\u81ea\u5b9a\u7fa9\u6309\u9215\u53ef\u4ee5\u4f7f\u7528 <strong>UIButton<\/strong> \u985e\u5225\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u8a2d\u5b9a\u6309\u9215\u7684\u6a23\u5f0f\u3001\u6587\u5b57\u3001\u5716\u7247\u7b49\u7b49\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u81ea\u5b9a\u7fa9\u6309\u9215\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u521d\u59cb\u5316 UIButton<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>UIButton<\/strong> \u7684 <strong>init(frame:)<\/strong> \u65b9\u6cd5\u4f86\u5275\u5efa\u4e00\u500b\u6309\u9215\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))<\/code><\/pre>\n<h3>2. \u8a2d\u5b9a\u6309\u9215\u6587\u5b57<\/h3>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>setTitle(_:for:)<\/strong> \u65b9\u6cd5\u4f86\u8a2d\u5b9a\u6309\u9215\u7684\u6587\u5b57\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">button.setTitle(\"My Button\", for: .normal)<\/code><\/pre>\n<h3>3. \u8a2d\u5b9a\u6587\u5b57\u984f\u8272<\/h3>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>setTitleColor(_:for:)<\/strong> \u65b9\u6cd5\u4f86\u8a2d\u5b9a\u6309\u9215\u6587\u5b57\u7684\u984f\u8272\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">button.setTitleColor(.blue, for: .normal)<\/code><\/pre>\n<h3>4. \u6dfb\u52a0\u9ede\u64ca\u4e8b\u4ef6<\/h3>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>addTarget(_:action:for:)<\/strong> \u65b9\u6cd5\u4f86\u8a2d\u5b9a\u6309\u9215\u7684\u9ede\u64ca\u4e8b\u4ef6\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)<\/code><\/pre>\n<h2>\u81ea\u5b9a\u7fa9\u6309\u9215\u7684\u64f4\u5c55\u61c9\u7528<\/h2>\n<p>\u4f60\u53ef\u4ee5\u6839\u64da\u81ea\u5df1\u7684\u9700\u6c42\u4f86\u8abf\u6574\u6309\u9215\u7684\u6a23\u5f0f\uff0c\u8b93\u4f60\u7684 App \u754c\u9762\u66f4\u52a0\u7368\u7279\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u8a2d\u8a08\u4e00\u500b\u5713\u89d2\u7684\u6309\u9215\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">button.layer.cornerRadius = 10\nbutton.layer.masksToBounds = true<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5275\u5efa\u4e00\u500b\u5e36\u6709\u6f38\u8b8a\u80cc\u666f\u7684\u6309\u9215\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 CAGradientLayer\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let gradientLayer = CAGradientLayer()\ngradientLayer.frame = button.bounds\ngradientLayer.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]\nbutton.layer.insertSublayer(gradientLayer, at: 0)<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5982\u6309\u9215\u4e0d\u986f\u793a\u6216\u9ede\u64ca\u7121\u53cd\u61c9\u3002\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u8a8d\u6309\u9215\u662f\u5426\u5df2\u6b63\u78ba\u6dfb\u52a0\u5230\u8996\u5716\u4e2d\u3002<\/li>\n<li>\u6aa2\u67e5 <code>addTarget<\/code> \u7684 selector \u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li>\u78ba\u4fdd\u6309\u9215\u7684 <code>isUserInteractionEnabled<\/code> \u5c6c\u6027\u8a2d\u70ba true\u3002<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5275\u5efa\u81ea\u5b9a\u7fa9\u6309\u9215\u7684\u57fa\u672c\u6b65\u9a5f\u4ee5\u53ca\u4e00\u4e9b\u9032\u968e\u61c9\u7528\u7bc4\u4f8b\u3002\u9019\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e6b\u52a9\u4f60\u8a2d\u8a08\u51fa\u66f4\u5177\u5438\u5f15\u529b\u7684 App \u754c\u9762\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u81ea\u5b9a\u7fa9\u6309\u9215-\ud83c\udfa8_1675409454.95891.webp\" alt=\"Swift \u81ea\u5b9a\u7fa9\u6309\u9215 \ud83c\udfa8\" title=\"Swift \u81ea\u5b9a\u7fa9\u6309\u9215\u3001Swift \u6309\u9215\u8a2d\u8a08\u3001Swift \u6309\u9215\u63a7\u5236\u3001Swift \u6309\u9215\u5916\u89c0\u3001Swift \u6309\u9215\u884c\u70ba\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728 Swift \u4e2d\u6dfb\u52a0\u5716\u7247\u5230\u6309\u9215\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u4f7f\u7528 <code>setImage(_:for:)<\/code> \u65b9\u6cd5\u4f86\u8a2d\u5b9a\u6309\u9215\u7684\u5716\u7247\uff0c\u4f8b\u5982\uff1a<code>button.setImage(UIImage(named: \"imageName\"), for: .normal)<\/code><\/p>\n<h3>Q2: \u5982\u4f55\u6539\u8b8a\u6309\u9215\u7684\u5927\u5c0f\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u76f4\u63a5\u8abf\u6574 <code>frame<\/code> \u5c6c\u6027\uff0c\u6216\u4f7f\u7528 Auto Layout \u8a2d\u5b9a\u7d04\u675f\u4f86\u6539\u8b8a\u6309\u9215\u7684\u5927\u5c0f\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5728\u6309\u9215\u4e0a\u6dfb\u52a0\u5713\u89d2\u6548\u679c\uff1f<\/h3>\n<p>A3: \u4f60\u53ef\u4ee5\u4f7f\u7528 <code>layer.cornerRadius<\/code> \u8a2d\u5b9a\u6309\u9215\u7684\u5713\u89d2\uff0c\u4f8b\u5982\uff1a<code>button.layer.cornerRadius = 10<\/code><\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u5728Swift\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u7fa9\u6309\u9215\u4f86\u5275\u5efa\u7368\u7279\u7684\u6309\u9215\uff0c\u4e26\u4f7f\u7528\u7c21\u55ae\u7684\u7a0b\u5f0f\u78bc\u4f86\u63a7\u5236\u5b83\u5011\u7684\u5916\u89c0\u548c\u884c\u70ba\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u81ea\u5b9a\u7fa9\u6309\u9215\uff0c\u4ee5\u53ca\u5982\u4f55\u8a2d\u7f6e\u5b83\u5011\u7684\u5916\u89c0\u548c\u884c\u70ba\u3002<\/p>\n","protected":false},"author":1,"featured_media":7236,"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-7238","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-\u81ea\u5b9a\u7fa9\u6309\u9215-\ud83c\udfa8_1675409454.95891.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1SK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7238","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=7238"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7238\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7236"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}