{"id":427,"date":"2025-05-30T10:37:51","date_gmt":"2025-06-05T03:31:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=427"},"modified":"2025-06-05T10:37:51","modified_gmt":"2025-06-05T03:31:50","slug":"swift-%e5%9c%96%e7%89%87-%e6%b7%a1%e5%85%a5%e6%b7%a1%e5%87%ba-%e5%88%87%e6%8f%9b%e5%9c%96%e7%89%87","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e5%9c%96%e7%89%87-%e6%b7%a1%e5%85%a5%e6%b7%a1%e5%87%ba-%e5%88%87%e6%8f%9b%e5%9c%96%e7%89%87\/","title":{"rendered":"Swift \u5716\u7247\u6de1\u5165\u6de1\u51fa\u5207\u63db\u6559\u5b78\uff5c2025 \u6700\u65b0\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<h3>\u7c21\u8981<\/h3>\n<p>\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Swift \u5be6\u73fe\u5716\u7247\u7684\u6de1\u5165\u6de1\u51fa\u6548\u679c\uff0c\u8b93\u5716\u7247\u5207\u63db\u66f4\u52a0\u6d41\u66a2\u8207\u7f8e\u89c0\u3002\u9019\u500b\u6548\u679c\u5728\u8a31\u591a\u61c9\u7528\u4e2d\u90fd\u975e\u5e38\u5be6\u7528\uff0c\u7279\u5225\u662f\u5728\u50cf <code>Uber Eats<\/code> \u9019\u6a23\u7684\u670d\u52d9\u4e2d\uff0c\u5716\u7247\u7684\u5c55\u793a\u6703\u5f71\u97ff\u7528\u6236\u7684\u9078\u64c7\u8207\u9ad4\u9a57\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/MMtNrni.gif\" alt=\"\u5716\u7247\u6de1\u5165\u6de1\u51fa\u6548\u679c\u793a\u7bc4\" title=\"\"><\/p>\n<h3>\u5716\u7247\u5e7b\u71c8\u7247<\/h3>\n<p>\u5be6\u73fe\u5716\u7247\u7684\u6de1\u5165\u6de1\u51fa\u6548\u679c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>UIImageView<\/code> \u4f86\u986f\u793a\u5716\u7247\u3002\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5c07\u4e00\u7d44\u5716\u7247\u653e\u5165\u4e00\u500b <code>Array<\/code> \u4e2d\uff0c\u7136\u5f8c\u8a2d\u7f6e\u5207\u63db\u7684\u6642\u9593\u548c\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u793a\u7bc4\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet imageView = UIImageView()<br \/>\nimageView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)<br \/>\nimageView.backgroundColor = UIColor.black<\/p>\n<p>let images = [<br \/>\n    UIImage(named: &#8220;food1&#8221;),<br \/>\n    UIImage(named: &#8220;food2&#8221;),<br \/>\n    UIImage(named: &#8220;food3&#8221;),<br \/>\n    UIImage(named: &#8220;food4&#8221;),<br \/>\n    UIImage(named: &#8220;food5&#8221;),<br \/>\n    UIImage(named: &#8220;food6&#8221;)<br \/>\n].compactMap { $0 } \/\/ \u4f7f\u7528 compactMap \u907f\u514d\u70ba nil \u7684\u5716\u7247<\/p>\n<p>let animatedImage = UIImage.animatedImage(with: images, duration: 3)<br \/>\nimageView.image = animatedImage<br \/>\nimageView.contentMode = .scaleAspectFit<br \/>\n&#8220;`<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/KtrkfXo.gif\" alt=\"\u52d5\u756b\u5716\u7247\u5207\u63db\u793a\u7bc4\" title=\"\"><\/p>\n<h3>\u6de1\u5165\u6de1\u51fa\u52d5\u756b<\/h3>\n<p>\u5728\u958b\u767c\u4e2d\uff0c\u7576\u6211\u5011\u9700\u8981\u5728 API \u8cc7\u6599\u56de\u50b3\u524d\u986f\u793a\u4e00\u500b\u900f\u660e\u7684\u5716\u7247\u6642\uff0c\u901a\u5e38\u6703\u5148\u5c07\u5176\u900f\u660e\u5ea6\u8a2d\u70ba 0\uff0c\u5f85\u8cc7\u6599\u56de\u50b3\u5f8c\uff0c\u4f7f\u7528\u52d5\u756b\u4f86\u6de1\u5165\u5716\u7247\u3002\u9019\u88e1\u662f\u76f8\u95dc\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nsender.alpha = 0.0<br \/>\nUIView.animate(withDuration: 1.5, animations: {<br \/>\n    sender.alpha = 1.0<br \/>\n})<br \/>\n&#8220;`<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/9lTG65B.gif\" alt=\"\u6de1\u5165\u52d5\u756b\u6548\u679c\u793a\u7bc4\" title=\"\"><\/p>\n<h3>\u5207\u63db\u5716\u7247<\/h3>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5169\u500b <code>UIImageView<\/code> \u4f86\u5be6\u73fe\u66f4\u6d41\u66a2\u7684\u5207\u63db\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u5be6\u4f5c\u6d41\u7a0b\uff1a<\/p>\n<p>1. **\u65b0\u589e\u7269\u4ef6**\uff1a\u521d\u59cb\u5316\u5169\u500b\u5716\u7247\u8996\u5716\uff0c<code>imageViewA<\/code> \u548c <code>imageViewB<\/code>\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlet imageViewA = UIImageView()<br \/>\nlet imageViewB = UIImageView()<\/p>\n<p>imageViewA.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)<br \/>\nimageViewA.contentMode = .scaleAspectFill<br \/>\nimageViewA.layer.masksToBounds = true<br \/>\nself.view.addSubview(imageViewA)<\/p>\n<p>imageViewB.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)<br \/>\nimageViewB.contentMode = .scaleAspectFill<br \/>\nimageViewB.layer.masksToBounds = true<br \/>\nself.view.addSubview(imageViewB)<br \/>\n&#8220;`<\/p>\n<p>2. **\u9ede\u64ca\u4e8b\u4ef6**\uff1a\u5728\u6309\u9215\u9ede\u64ca\u6642\u5207\u63db\u5716\u7247\uff0c\u4e26\u4f7f\u7528\u52d5\u756b\u4f86\u9054\u6210\u6de1\u5165\u6de1\u51fa\u7684\u6548\u679c\u3002<\/p>\n<p>&#8220;`swift<br \/>\n@IBAction func buttonClicked(sender: UIButton) {<br \/>\n    let buttonTag = sender.tag<br \/>\n    sender.tag = sender.tag + 1<br \/>\n    imageViewB.image = UIImage(named: &#8220;food&#8221; + String(buttonTag))<br \/>\n    imageViewB.alpha = 0.0<br \/>\n    UIView.animate(withDuration: 0.5, animations: {<br \/>\n        self.imageViewB.alpha = 1.0<br \/>\n    }, completion: { _ in<br \/>\n        self.imageViewB.alpha = 0.0<br \/>\n        self.imageViewA.image = self.imageViewB.image<br \/>\n    })<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/qarFLVW.gif\" alt=\"\u5716\u7247\u5207\u63db\u8207\u6de1\u5165\u6548\u679c\u793a\u7bc4\" title=\"\"><\/p>\n<h3>\u589e\u52a0\u906e\u7f69\u8207\u6587\u5b57\u6a19\u7c64<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u70ba\u5716\u7247\u4e0a\u65b9\u6dfb\u52a0\u4e00\u500b\u984f\u8272\u906e\u7f69\u53ca\u6587\u5b57\u6a19\u7c64\u3002\u9019\u4e0d\u50c5\u589e\u52a0\u4e86\u8996\u89ba\u6548\u679c\uff0c\u9084\u80fd\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlet maskView = UIView()<br \/>\nmaskView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)<br \/>\nmaskView.backgroundColor = UIColor.black.withAlphaComponent(0.5)<br \/>\nself.view.addSubview(maskView)<\/p>\n<p>let titleLabel = UILabel()<br \/>\ntitleLabel.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)<br \/>\ntitleLabel.textColor = .white<br \/>\ntitleLabel.textAlignment = .center<br \/>\ntitleLabel.font = UIFont.systemFont(ofSize: 30)<br \/>\nself.view.addSubview(titleLabel)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u6309\u9215\u9ede\u64ca\u4e8b\u4ef6\u4e2d\uff0c\u6211\u5011\u4e5f\u6703\u66f4\u65b0\u6a19\u984c\u6587\u5b57\uff1a<\/p>\n<p>&#8220;`swift<br \/>\ntitleLabel.text = &#8220;food&#8221; + String(buttonTag)<br \/>\n&#8220;`<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/gSPV7mf.gif\" alt=\"\u6dfb\u52a0\u6587\u5b57\u6a19\u7c64\u8207\u906e\u7f69\u793a\u7bc4\" title=\"\"><\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e\u4e0a\u8ff0\u7684\u5be6\u4f5c\u65b9\u5f0f\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u73fe\u4e00\u500b\u6d41\u66a2\u4e14\u7f8e\u89c0\u7684\u5716\u7247\u5207\u63db\u6548\u679c\uff0c\u589e\u5f37\u7528\u6236\u7684\u8996\u89ba\u9ad4\u9a57\u3002\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9019\u4e9b\u57fa\u790e\u77e5\u8b58\u9032\u884c\u66f4\u591a\u7684\u64f4\u5c55\u8207\u61c9\u7528\u3002<\/p>\n<hr \/>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u5982\u4f55\u89e3\u6c7a\u5716\u7247\u7121\u6cd5\u986f\u793a\u7684\u554f\u984c\uff1f**<br \/>\nA1: \u78ba\u8a8d\u5716\u7247\u540d\u7a31\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u6aa2\u67e5\u5716\u7247\u662f\u5426\u5df2\u6b63\u78ba\u52a0\u5165\u5c08\u6848\u4e2d\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8abf\u6574\u52d5\u756b\u7684\u6301\u7e8c\u6642\u9593\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u5728\u52d5\u756b\u7684 <code>withDuration<\/code> \u53c3\u6578\u4e2d\u8a2d\u5b9a\u6240\u9700\u7684\u6301\u7e8c\u6642\u9593\uff0c\u4f8b\u5982\u5c07\u5176\u8a2d\u70ba 1.0 \u4ee3\u8868 1 \u79d2\u3002<\/p>\n<p>**Q3: \u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5716\u7247\u5207\u63db\u6548\u679c\u55ce\uff1f**<br \/>\nA3: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u63a2\u7d22\u5176\u4ed6\u52d5\u756b\u6548\u679c\uff0c\u4f8b\u5982\u7e2e\u653e\u6216\u65cb\u8f49\uff0c\u4f86\u66ff\u4ee3\u6de1\u5165\u6de1\u51fa\u7684\u6548\u679c\u3002<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"\u76f8\u95dc\u9023\u7d50\u5716\u7247\" title=\"\"><\/a><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u5c0b\u9019\u9801\u90e8\u5206<br \/>\n\u5728\u4e0b\u9762\u7684`CollectionView`\u6709\u505a\u7279\u6548<br \/>\n\u6240\u6709\u5716\u7247\u5728\u56fa\u5b9a\u6642\u9593\u90fd\u6703\u63db\u5716\u7247<br \/>\n\u4eca\u5929\u5c31\u8a66\u8457\u4f86\u6548\u4eff\u539f\u751f `Uber eat`<\/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-427","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-6T","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/427","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=427"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":566,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/427\/revisions\/566"}],"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=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}