{"id":7278,"date":"2025-05-30T12:46:09","date_gmt":"2025-06-05T05:03:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7278"},"modified":"2025-06-05T12:46:09","modified_gmt":"2025-06-05T05:03:09","slug":"https-badgameshow-com-steven-311","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-311\/","title":{"rendered":"Swift \u5716\u7247\u8f2a\u64ad\u5668 &#8211; \u4f7f\u7528\u6700\u65b0 Swift 5.8 \u5275\u5efa\u5716\u7247\u8f2a\u64ad\u5668 \ud83d\udcbb"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u5716\u7247\u8f2a\u64ad\u5668, \u5275\u5efa, \u56fe\u7247\u8f2a\u64ad\u5668, Swift 5.8\"><\/p>\n<h2>Swift \u5716\u7247\u8f2a\u64ad\u5668 &#8211; \u4f7f\u7528\u6700\u65b0 Swift 5.8 \u5275\u5efa\u5716\u7247\u8f2a\u64ad\u5668 \ud83d\udcbb<\/h2>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c\u5275\u5efa\u4e00\u500b\u5716\u7247\u8f2a\u64ad\u5668\u662f\u4e00\u500b\u76f8\u7576\u5e38\u898b\u7684\u9700\u6c42\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u6ed1\u52d5\u67e5\u770b\u4e0d\u540c\u7684\u5716\u7247\u3002\u5728 2025 \u6700\u65b0\u7684 Swift 5.8 \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u7528 UICollectionView \u4f86\u5be6\u73fe\u5716\u7247\u8f2a\u64ad\u5668\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u5275\u5efa\u4e00\u500b\u9ad8\u6548\u7684\u5716\u7247\u8f2a\u64ad\u5668\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/h3XhVVU.png\" alt=\"UICollectionView\" title=\"UICollectionView \u793a\u7bc4\" \/><\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u8a2d\u7f6e UICollectionView<\/h3>\n<p>\u9996\u5148\uff0c\u6253\u958b\u4f60\u7684 Storyboard\uff0c\u62d6\u62c9\u4e00\u500b UICollectionView \u5230\u4f60\u7684 ViewController \u4e2d\uff0c\u4e26\u5c07\u5b83\u7684 delegate \u548c dataSource \u90fd\u6307\u5411 ViewController\u3002\u78ba\u4fdd\u4f60\u5df2\u7d93\u8a2d\u7f6e\u4e86 UICollectionView \u7684\u81ea\u9069\u61c9\u5927\u5c0f\u4ee5\u9069\u61c9\u5c4f\u5e55\u3002<\/p>\n<h3>\u6b65\u9a5f 2\uff1a\u5be6\u4f5c UICollectionViewDelegate \u548c UICollectionViewDataSource<\/h3>\n<p>\u5728 ViewController \u4e2d\u5be6\u4f5c UICollectionViewDelegate \u548c UICollectionViewDataSource \u7684\u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {\n    var images: [UIImage] = [] \/\/ \u5132\u5b58\u5716\u7247\u7684\u9663\u5217\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \/\/ \u52a0\u8f09\u5716\u7247\n        loadImages()\n    }\n\n    func loadImages() {\n        \/\/ \u5728\u9019\u88e1\u53ef\u4ee5\u5f9e\u672c\u5730\u6216\u7db2\u8def\u52a0\u8f09\u5716\u7247\n        images = [UIImage(named: \"image1\")!, UIImage(named: \"image2\")!, UIImage(named: \"image3\")!]\n    }\n\n    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n        \/\/ \u8fd4\u56de\u5716\u7247\u6578\u91cf\n        return images.count\n    }\n\n    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n        \/\/ \u53d6\u5f97 cell\n        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: \"ImageCell\", for: indexPath) as! ImageCell\n\n        \/\/ \u8a2d\u5b9a cell \u5167\u5bb9\n        cell.imageView.image = images[indexPath.row]\n\n        return cell\n    }\n\n    \/\/ \u652f\u6301\u81ea\u52d5\u6efe\u52d5\n    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {\n        let pageIndex = Int(scrollView.contentOffset.x \/ scrollView.frame.size.width)\n        print(\"\u7576\u524d\u9801\u9762\u7d22\u5f15: \\(pageIndex)\")\n    }\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 3\uff1a\u8a2d\u8a08 UICollectionViewCell<\/h3>\n<p>\u5728 Storyboard \u4e2d\u8a2d\u8a08\u4e00\u500b UICollectionViewCell\uff0c\u4e26\u5c07\u5176 class \u8a2d\u5b9a\u70ba ImageCell\u3002\u78ba\u4fdd\u4f60\u5728 ImageCell \u4e2d\u5275\u5efa\u4e00\u500b UIImageView\uff0c\u4e26\u5c07\u5b83\u7684 outlet \u8a2d\u5b9a\u70ba imageView\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/XhX6XVX.png\" alt=\"ImageCell\" title=\"UICollectionViewCell \u8a2d\u7f6e\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/h6fhU6V.png\" alt=\"UIImageView\" title=\"UIImageView \u8a2d\u7f6e\" \/><\/p>\n<h3>\u6b65\u9a5f 4\uff1a\u57f7\u884c\u8207\u6e2c\u8a66<\/h3>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u5df2\u7d93\u5b8c\u6210\u4e86\u6240\u6709\u6b65\u9a5f\uff0c\u53ea\u9700\u5c07\u5716\u7247\u653e\u5165 images \u9663\u5217\u4e2d\uff0c\u5373\u53ef\u5728 UICollectionView \u4e2d\u67e5\u770b\u5716\u7247\u8f2a\u64ad\u5668\u7684\u6548\u679c\u3002\u904b\u884c\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u78ba\u4fdd\u5716\u7247\u53ef\u4ee5\u6b63\u78ba\u986f\u793a\u3002<\/p>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>&#8211; \u5982\u679c\u5716\u7247\u4e0d\u986f\u793a\uff0c\u8acb\u6aa2\u67e5\u5716\u7247\u540d\u7a31\u662f\u5426\u6b63\u78ba\u3002<br \/>\n&#8211; \u78ba\u4fdd UICollectionView \u7684 delegate \u548c dataSource \u5df2\u6b63\u78ba\u8a2d\u7f6e\u3002<br \/>\n&#8211; \u6aa2\u67e5 UICollectionViewCell \u7684\u91cd\u7528\u6a19\u8b58\u7b26\u662f\u5426\u5339\u914d\u3002<\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u4f60\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u589e\u52a0\u81ea\u52d5\u64ad\u653e\u3001\u904e\u6e21\u6548\u679c\u7b49\u529f\u80fd\uff0c\u8b93\u5716\u7247\u8f2a\u64ad\u5668\u66f4\u52a0\u8c50\u5bcc\u548c\u5438\u5f15\u4eba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5716\u7247\u8f2a\u64ad\u5668-\u5275\u5efa\u4e00\u500b\u56fe\u7247\u8f2a\u64ad\u5668-\ud83d\udcbb_1675410015.251012.webp\" alt=\"Swift \u5716\u7247\u8f2a\u64ad\u5668 - \u5275\u5efa\u4e00\u500b\u56fe\u7247\u8f2a\u64ad\u5668 \ud83d\udcbb\" title=\"Swift \u5716\u7247\u8f2a\u64ad\u5668\u793a\u7bc4\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>Q1: \u5982\u4f55\u5728\u8f2a\u64ad\u5668\u4e2d\u6dfb\u52a0\u52d5\u756b\u6548\u679c\uff1f<\/h3>\n<p>A: \u4f60\u53ef\u4ee5\u5728 UICollectionView \u7684 delegate \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u52d5\u756b\u6548\u679c\uff0c\u4f8b\u5982\u4f7f\u7528 UIView.animate() \u65b9\u6cd5\u4f86\u5be6\u73fe\u904e\u6e21\u52d5\u756b\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5f9e\u7db2\u7d61\u52a0\u8f09\u5716\u7247\uff1f<\/h3>\n<p>A: \u53ef\u4ee5\u4f7f\u7528 URLSession \u6216\u7b2c\u4e09\u65b9\u5eab\u5982 SDWebImage \u4f86\u5f9e\u7db2\u7d61\u52a0\u8f09\u5716\u7247\uff0c\u4e26\u5c07\u5176\u6dfb\u52a0\u5230 images \u9663\u5217\u4e2d\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5be6\u73fe\u81ea\u52d5\u64ad\u653e\u529f\u80fd\uff1f<\/h3>\n<p>A: \u53ef\u4ee5\u4f7f\u7528 Timer \u4f86\u5b9a\u6642\u6efe\u52d5 UICollectionView\uff0c\u4e26\u5728 scrollViewDidEndDecelerating \u65b9\u6cd5\u4e2d\u66f4\u65b0\u7576\u524d\u9801\u9762\u7d22\u5f15\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u5275\u5efa\u4e00\u500b\u5716\u7247\u8f2a\u64ad\u5668\uff0c\u4e26\u8a73\u7d30\u4ecb\u7d39Swift\u4e2d\u7684methods()\u51fd\u6578\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u5275\u5efa\u4e00\u500b\u5716\u7247\u8f2a\u64ad\u5668\u3002<\/p>\n","protected":false},"author":1,"featured_media":7277,"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-7278","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-\u5716\u7247\u8f2a\u64ad\u5668-\u5275\u5efa\u4e00\u500b\u56fe\u7247\u8f2a\u64ad\u5668-\ud83d\udcbb_1675410015.251012.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1To","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7278","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=7278"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7278\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7277"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}