{"id":3943,"date":"2025-06-03T11:44:47","date_gmt":"2025-06-05T03:48:46","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3943"},"modified":"2025-06-05T11:44:47","modified_gmt":"2025-06-05T03:48:46","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e8%87%aa%e5%ae%9a%e7%be%a9%e8%a6%96%e5%9c%96","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e8%87%aa%e5%ae%9a%e7%be%a9%e8%a6%96%e5%9c%96\/","title":{"rendered":"2025 \u5e74\u6700\u65b0 Swift \u81ea\u5b9a\u7fa9\u8996\u5716\u6559\u5b78\uff1a\u5275\u5efa\u7368\u7279\u7684\u7528\u6236\u754c\u9762"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, UIView, drawRect, \u81ea\u5b9a\u7fa9\u8996\u5716, \u81ea\u5b9a\u7fa9\u5716\u5f62, Swift 2025, UIBezierPath, iOS \u958b\u767c\"><\/p>\n<h1>2025 \u5e74\u6700\u65b0 Swift \u81ea\u5b9a\u7fa9\u8996\u5716\u6559\u5b78\uff1a\u5275\u5efa\u7368\u7279\u7684\u7528\u6236\u754c\u9762<\/h1>\n<p>\u5728 Swift \u4e2d\uff0c\u81ea\u5b9a\u7fa9\u8996\u5716\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u80fd\u8b93\u4f60\u5275\u5efa\u51fa\u7368\u7279\u800c\u5438\u5f15\u4eba\u7684\u7528\u6236\u754c\u9762\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4e2d\u7684\u81ea\u5b9a\u7fa9\u8996\u5716\uff0c\u5305\u62ec\u5982\u4f55\u7e7c\u627f UIView \u985e\u4f86\u5be6\u73fe\u81ea\u5b9a\u7fa9\u8996\u5716\u3001\u5982\u4f55\u91cd\u5beb `drawRect` \u65b9\u6cd5\u4f86\u7e6a\u88fd\u81ea\u5b9a\u7fa9\u5716\u5f62\uff0c\u4ee5\u53ca\u5982\u4f55\u9032\u884c\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<h2>\u7e7c\u627f UIView \u985e\u4f86\u5be6\u73fe\u81ea\u5b9a\u7fa9\u8996\u5716<\/h2>\n<p>\u8981\u5be6\u73fe\u81ea\u5b9a\u7fa9\u8996\u5716\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500b\u7e7c\u627f\u81ea UIView \u985e\u7684\u5b50\u985e\u3002\u5728\u9019\u500b\u5b50\u985e\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9a\u7fa9\u6240\u9700\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>&#8220;`swift<br \/>\nclass MyView: UIView {<br \/>\n    \/\/ \u5b9a\u7fa9\u5c6c\u6027<br \/>\n    var myProperty: String<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u65b9\u6cd5<br \/>\n    init(myProperty: String) {<br \/>\n        self.myProperty = myProperty<br \/>\n        super.init(frame: .zero)<br \/>\n    }<\/p>\n<p>    required init?(coder: NSCoder) {<br \/>\n        fatalError(&#8220;init(coder:) has not been implemented&#8221;)<br \/>\n    }<\/p>\n<p>    \/\/ \u5b9a\u7fa9\u65b9\u6cd5<br \/>\n    func myMethod() {<br \/>\n        \/\/ \u5be6\u73fe\u65b9\u6cd5<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u589e\u52a0\u4e86\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u80fd\u5920\u8a2d\u7f6e\u5c6c\u6027\u3002\u9019\u662f Swift 2025 \u7684\u6700\u4f73\u5be6\u8e10\uff0c\u4fdd\u8b49\u6240\u6709\u5fc5\u8981\u7684\u521d\u59cb\u5316\u90fd\u80fd\u6b63\u78ba\u5b8c\u6210\u3002<\/p>\n<h2>\u91cd\u5beb drawRect \u65b9\u6cd5\u4f86\u7e6a\u88fd\u81ea\u5b9a\u7fa9\u5716\u5f62<\/h2>\n<p>\u5728\u81ea\u5b9a\u7fa9\u8996\u5716\u4e2d\uff0c\u53ef\u4ee5\u91cd\u5beb `drawRect` \u65b9\u6cd5\u4f86\u7e6a\u88fd\u81ea\u5b9a\u7fa9\u5716\u5f62\u3002\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 UIKit \u63d0\u4f9b\u7684\u7e6a\u5716 API \u4f86\u5275\u5efa\u4efb\u4f55\u6240\u9700\u7684\u5716\u5f62\u3002<\/p>\n<p>&#8220;`swift<br \/>\noverride func draw(_ rect: CGRect) {<br \/>\n    \/\/ \u4f7f\u7528 UIKit \u4e2d\u63d0\u4f9b\u7684\u7e6a\u5716 API \u4f86\u7e6a\u88fd\u5716\u5f62<br \/>\n    let path = UIBezierPath(ovalIn: rect)<br \/>\n    UIColor.red.setFill()<br \/>\n    path.fill()<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528 `UIBezierPath` \u985e\u4f86\u7e6a\u88fd\u4e00\u500b\u5713\u5f62\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>&#8220;`swift<br \/>\noverride func draw(_ rect: CGRect) {<br \/>\n    let path = UIBezierPath(ovalIn: rect)<br \/>\n    UIColor.red.setFill()<br \/>\n    path.fill()<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u4f60\u5c31\u80fd\u5275\u5efa\u51fa\u4e00\u500b\u81ea\u5b9a\u7fa9\u7684\u5713\u5f62\u8996\u5716\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u958b\u767c\u81ea\u5b9a\u7fa9\u8996\u5716\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>1. **\u5716\u5f62\u4e0d\u986f\u793a**\uff1a\u78ba\u4fdd `drawRect` \u65b9\u6cd5\u88ab\u6b63\u78ba\u8abf\u7528\uff0c\u4e26\u4e14\u8996\u5716\u7684 frame \u5df2\u6b63\u78ba\u8a2d\u7f6e\u3002<br \/>\n2. **\u984f\u8272\u4e0d\u6b63\u78ba**\uff1a\u6aa2\u67e5\u984f\u8272\u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\uff0c\u4e26\u4e14\u5728\u7e6a\u88fd\u4e4b\u524d\u8abf\u7528 `setFill()`\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u81ea\u5b9a\u7fa9\u8996\u5716\u7684\u61c9\u7528\u975e\u5e38\u5ee3\u6cdb\uff0c\u4f60\u53ef\u4ee5\u7528\u5b83\u4f86\u5275\u5efa\u5404\u7a2e UI \u5143\u7d20\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; \u81ea\u5b9a\u7fa9\u6309\u9215<br \/>\n&#8211; \u52d5\u756b\u6548\u679c<br \/>\n&#8211; \u5716\u7247\u8f09\u5165\u6548\u679c\u7b49<\/p>\n<p>\u9019\u4e9b\u90fd\u80fd\u4f7f\u4f60\u7684\u61c9\u7528\u66f4\u52a0\u751f\u52d5\u548c\u5438\u5f15\u4eba\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4e2d\u7684\u81ea\u5b9a\u7fa9\u8996\u5716\uff0c\u5305\u62ec\u5982\u4f55\u7e7c\u627f UIView \u985e\u4f86\u5be6\u73fe\u81ea\u5b9a\u7fa9\u8996\u5716\u3001\u5982\u4f55\u91cd\u5beb `drawRect` \u65b9\u6cd5\u4f86\u7e6a\u88fd\u81ea\u5b9a\u7fa9\u5716\u5f62\u4ee5\u53ca\u4e00\u4e9b\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u7684\u6280\u5de7\u3002\u900f\u904e\u9019\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5275\u5efa\u51fa\u7368\u7279\u7684\u7528\u6236\u754c\u9762\uff0c\u63d0\u5347\u61c9\u7528\u7684\u6574\u9ad4\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u6211\u5982\u4f55\u5728\u81ea\u5b9a\u7fa9\u8996\u5716\u4e2d\u52a0\u5165\u624b\u52e2\u8b58\u5225\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u81ea\u5b9a\u7fa9\u8996\u5716\u4e2d\u6dfb\u52a0\u624b\u52e2\u8b58\u5225\u5668\uff0c\u900f\u904e `addGestureRecognizer` \u65b9\u6cd5\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 `UITapGestureRecognizer` \u4f86\u6aa2\u6e2c\u9ede\u64ca\u4e8b\u4ef6\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u8abf\u6574\u81ea\u5b9a\u7fa9\u8996\u5716\u7684\u5927\u5c0f\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u5728\u81ea\u5b9a\u7fa9\u8996\u5716\u7684\u521d\u59cb\u5316\u904e\u7a0b\u4e2d\u8a2d\u7f6e\u5176 frame\uff0c\u6216\u4f7f\u7528 Auto Layout \u4f86\u81ea\u52d5\u8abf\u6574\u5927\u5c0f\u3002<\/p>\n<h3>Q3: \u4f7f\u7528 `drawRect` \u65b9\u6cd5\u7684\u6700\u4f73\u5be6\u8e10\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A3: \u78ba\u4fdd\u5728\u6bcf\u6b21\u9700\u8981\u91cd\u65b0\u7e6a\u88fd\u8996\u5716\u6642\u8abf\u7528 `setNeedsDisplay()`\uff0c\u4ee5\u4fbf\u89f8\u767c `drawRect` \u65b9\u6cd5\u7684\u8abf\u7528\uff0c\u4e26\u4fdd\u6301\u6027\u80fd\u6700\u4f73\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u81ea\u5b9a\u7fa9\u8996\u5716\uff0c\u5305\u62ec\u7e7c\u627fUIView\u985e\u4ee5\u5be6\u73fe\u81ea\u5b9a\u7fa9\u8996\u5716\uff0c\u4ee5\u53ca\u5982\u4f55\u91cd\u5bebdrawRect\u65b9\u6cd5\u4f86\u7e6a\u88fd\u81ea\u5b9a\u7fa9\u5716\u5f62\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","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-3943","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-11B","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3943","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=3943"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3943\/revisions"}],"predecessor-version":[{"id":3944,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3943\/revisions\/3944"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}