{"id":456,"date":"2025-06-03T10:39:20","date_gmt":"2025-06-05T03:32:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=456"},"modified":"2025-06-05T10:39:20","modified_gmt":"2025-06-05T03:32:19","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%e6%95%88%e6%9e%9c-%e5%b9%bb%e7%87%88%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%e6%95%88%e6%9e%9c-%e5%b9%bb%e7%87%88%e7%89%87\/","title":{"rendered":"Swift \u5716\u7247\u6de1\u5165\u6de1\u51fa\u6548\u679c\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<h3>\u7c21\u8981<\/h3>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u4f7f\u7528 Swift \u7684 <code>CABasicAnimation<\/code> \u4f86\u5be6\u73fe\u5716\u7247\u7684\u6de1\u5165\u6de1\u51fa\u6548\u679c\u3002\u9019\u662f Swift \u52d5\u756b\u4e2d\u7684\u4e00\u500b\u57fa\u672c\u7bc4\u4f8b\uff0c\u80fd\u5e6b\u52a9\u4f60\u8f15\u9b06\u5207\u63db\u5716\u7247\u4e26\u589e\u5f37\u4f7f\u7528\u8005\u9ad4\u9a57\u3002\u96a8\u8457 iOS \u7684\u66f4\u65b0\uff0c\u9019\u4e9b\u65b9\u6cd5\u4e5f\u5728\u4e0d\u65b7\u6f14\u9032\uff0c\u56e0\u6b64\u6211\u5011\u5c07\u63d0\u4f9b 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\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>\u57fa\u672c\u8a2d\u7f6e<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6e\u4e00\u500b <code>UIView<\/code> \u4f5c\u70ba\u6211\u5011\u7684\u986f\u793a\u5340\u57df\u3002\u9019\u88e1\u6211\u5011\u5275\u5efa\u4e00\u500b\u908a\u9577\u70ba 100 \u7684\u6b63\u65b9\u5f62\uff0c\u4e26\u5c07\u5176\u6dfb\u52a0\u5230\u4e3b\u8996\u5716\u4e2d\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let testView = UIView()\ntestView.frame = CGRect(x: 200, y: 200, width: 100, height: 100)\ntestView.backgroundColor = UIColor.darkGray\nself.view.addSubview(testView)\n\n\/* \n \u9019\u88e1\u662f CABasicAnimation \u52d5\u756b\u5340 \n \u4e0b\u65b9\u6703\u8b1b\u89e3\u586b\u7a7a\n*\/\n<\/code><\/pre>\n<h3>CABasicAnimation<\/h3>\n<p><code>CABasicAnimation<\/code> \u63d0\u4f9b\u4e86\u591a\u7a2e\u52d5\u756b\u6548\u679c\uff0c\u53ef\u4ee5\u8a2d\u5b9a <code>keyPath<\/code> \u4f86\u6c7a\u5b9a\u8f49\u63db\u6548\u679c\u3002\u4ee5\u4e0b\u5c07\u4ecb\u7d39\u5e7e\u7a2e\u5e38\u898b\u7684\u52d5\u756b\u6548\u679c\u3002<\/p>\n<h4>\u65cb\u8f49<\/h4>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u65cb\u8f49\u52d5\u756b\nlet basicAnimation = CABasicAnimation(keyPath: \"transform.rotation.y\")\nbasicAnimation.duration = 10\nbasicAnimation.repeatCount = 5\nbasicAnimation.fromValue = 0\nbasicAnimation.toValue = 2 * Double.pi\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/DPpOpvj.gif\" alt=\"\u65cb\u8f49\u52d5\u756b\u793a\u7bc4\" title=\"\"><\/p>\n<h4>\u7e2e\u653e<\/h4>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u7e2e\u653e\u52d5\u756b\nlet basicAnimation = CABasicAnimation(keyPath: \"transform.scale\")\nbasicAnimation.duration = 10\nbasicAnimation.repeatCount = 5\nbasicAnimation.fromValue = 0\nbasicAnimation.toValue = 2.0\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/7GtQCEQ.gif\" alt=\"\u7e2e\u653e\u52d5\u756b\u793a\u7bc4\" title=\"\"><\/p>\n<h4>\u79fb\u52d5<\/h4>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u79fb\u52d5\u52d5\u756b\nlet basicAnimation = CABasicAnimation(keyPath: \"position\")\nbasicAnimation.duration = 10\nbasicAnimation.repeatCount = 5\nbasicAnimation.fromValue = CGPoint(x: 100, y: 100)\nbasicAnimation.toValue = CGPoint(x: 400, y: 400)\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/H8akKJD.gif\" alt=\"\u79fb\u52d5\u52d5\u756b\u793a\u7bc4\" title=\"\"><\/p>\n<h4>\u9583\u720d<\/h4>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u9583\u720d\u52d5\u756b\nlet basicAnimation = CABasicAnimation(keyPath: \"opacity\")\nbasicAnimation.duration = 0.2\nbasicAnimation.repeatCount = 10\nbasicAnimation.fromValue = 1.0\nbasicAnimation.toValue = 0.0\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/ZZ58OYG.gif\" alt=\"\u9583\u720d\u52d5\u756b\u793a\u7bc4\" title=\"\"><\/p>\n<h4>\u8b8a\u8272<\/h4>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u8b8a\u8272\u52d5\u756b\nlet basicAnimation = CABasicAnimation(keyPath: \"backgroundColor\")\nbasicAnimation.duration = 0.2\nbasicAnimation.repeatCount = 10\nbasicAnimation.fromValue = UIColor.red.cgColor\nbasicAnimation.toValue = UIColor.blue.cgColor\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/7XRbm3e.gif\" alt=\"\u8b8a\u8272\u52d5\u756b\u793a\u7bc4\" title=\"\"><\/p>\n<h4>\u5716\u7247\u5207\u63db<\/h4>\n<p>\u4f7f\u7528 <code>CABasicAnimation<\/code> \u7684 <code>contents<\/code> \u5c6c\u6027\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u73fe\u5716\u7247\u7684\u6de1\u5165\u6de1\u51fa\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u6309\u9215\u4f86\u5207\u63db\u5716\u7247\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@IBAction func buttonClicked(sender: UIButton) {\n    let buttonTag = sender.tag\n    sender.tag += 1\n    let nextImage = UIImage(named: \"food\" + String(buttonTag)) \/\/ \u66f4\u65b0\u70ba\u65b0\u7684\u5716\u7247\n    let basicAnimation = CABasicAnimation(keyPath: \"contents\")\n    basicAnimation.fromValue = imageViewA.image?.cgImage\n    basicAnimation.toValue = nextImage?.cgImage\n    basicAnimation.duration = 1.5\n    imageViewA.layer.contents = imageViewA.image?.cgImage\n    imageViewA.layer.add(basicAnimation, forKey: nil)\n    imageViewA.image = nextImage \/\/ \u66f4\u65b0 UIImageView\n}\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/LC8rKFH.gif\" alt=\"\u5716\u7247\u5207\u63db\u52d5\u756b\u793a\u7bc4\" title=\"\"><\/p>\n<p>\u9019\u500b\u65b9\u6cd5\u7c21\u5316\u4e86\u7269\u4ef6\u7684\u4f7f\u7528\uff0c\u8b93\u7a0b\u5f0f\u78bc\u66f4\u70ba\u7c21\u6f54\u3002\u5efa\u8b70\u5927\u5bb6\u89aa\u81ea\u8a66\u8a66\u770b\uff0c\u9ad4\u9a57\u9019\u4e9b\u52d5\u756b\u6548\u679c\u7684\u9b45\u529b\u3002<\/p>\n<h2>\u5b8c\u6210<\/h2>\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 \u7a0b\u5f0f\u6559\u5b78\" title=\"\"><br \/>\n<\/a><\/p>\n<h3>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h3>\n<h4>1. \u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528\u52d5\u756b\uff1f<\/h4>\n<p>\u4f7f\u7528 <code>CABasicAnimation<\/code> \u662f\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u52d5\u756b\u6548\u679c\uff0c\u4f60\u53ea\u9700\u8a2d\u5b9a\u95dc\u9375\u5b57\u8def\u5f91\u548c\u52d5\u756b\u5c6c\u6027\u5373\u53ef\u3002<\/p>\n<h4>2. \u53ef\u4ee5\u5728\u52d5\u756b\u4e2d\u6dfb\u52a0\u591a\u500b\u6548\u679c\u55ce\uff1f<\/h4>\n<p>\u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>CAAnimationGroup<\/code> \u4f86\u5c07\u591a\u500b\u52d5\u756b\u6548\u679c\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u52d5\u756b\u6548\u679c\u3002<\/p>\n<h4>3. \u5982\u4f55\u8abf\u6574\u52d5\u756b\u7684\u6301\u7e8c\u6642\u9593\u548c\u91cd\u8907\u6b21\u6578\uff1f<\/h4>\n<p>\u5728\u5275\u5efa\u52d5\u756b\u6642\uff0c\u53ef\u4ee5\u901a\u904e <code>duration<\/code> \u548c <code>repeatCount<\/code> \u5c6c\u6027\u4f86\u8a2d\u5b9a\u52d5\u756b\u7684\u6301\u7e8c\u6642\u9593\u548c\u91cd\u8907\u6b21\u6578\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u4f7f\u7528`UIView.animate`<br \/>\n\u5b8c\u6210`\u5716\u7247\u5806\u758a`+`\u6de1\u5165\u6de1\u51fa`\u6548\u679c<br \/>\n\u4f46\u9019\u6bd4\u8f03\u7b97\u662f`\u571f\u70ae`\u65b9\u6cd5<br \/>\n\u5c31\u50cf`LeetCode`\u6709\u5343\u767e\u7a2e\u89e3\u6cd5<br \/>\n\u6240\u4ee5\u6211\u53c8\u53bb\u7814\u7a76\u4e00\u4e0b\u52d5\u756b\u65b9\u9762\u7684\u77e5\u8b58<br \/>\n\u767c\u73fe`later`\u88e1\u9762\u672c\u4f86\u5c31\u6709`animate`<\/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-456","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-7m","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/456","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=456"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/456\/revisions\/564"}],"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=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}