{"id":7469,"date":"2025-05-29T13:01:36","date_gmt":"2025-06-05T06:00:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7469"},"modified":"2025-06-05T13:01:36","modified_gmt":"2025-06-05T06:00:36","slug":"https-badgameshow-com-steven-405","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-405\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u6578\u5b57\u8f2a\u64ad\u5668\uff1a\u7c21\u55ae\u5be6\u73fe\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udd22\ud83d\udd04"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u6578\u5b57\u8f2a\u64ad\u5668, iOS \u958b\u767c, UIScrollView, Swift \u6559\u5b78\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>Swift \u6578\u5b57\u8f2a\u64ad\u5668 \ud83d\udd22\ud83d\udd04 \u662f\u4e00\u500b\u6709\u8da3\u4e14\u5be6\u7528\u7684\u529f\u80fd\uff0c\u80fd\u5920\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\u8f15\u9b06\u5275\u5efa\u52d5\u614b\u986f\u793a\u6578\u5b57\u7684\u4ecb\u9762\u3002\u96a8\u8457 Swift \u8a9e\u8a00\u7684\u767c\u5c55\uff0c\u9019\u7bc7\u6587\u7ae0\u5c07\u5e36\u60a8\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u4f86\u5be6\u73fe\u6578\u5b57\u8f2a\u64ad\u5668\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Swift \u6578\u5b57\u8f2a\u64ad\u5668 \ud83d\udd22\ud83d\udd04<\/h2>\n<p>\u8981\u5728\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\u5275\u5efa\u4e00\u500b\u6578\u5b57\u8f2a\u64ad\u5668\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\uff1a<\/p>\n<p>1. **\u5275\u5efa\u4e00\u500b UIView**\uff1a<br \/>\n   \u5728\u60a8\u7684 UIViewController \u4e2d\uff0c\u9996\u5148\u5275\u5efa\u4e00\u500b UIView\uff0c\u4f5c\u70ba\u6578\u5b57\u8f2a\u64ad\u5668\u7684\u5bb9\u5668\u3002<\/p>\n<p>   &#8220;`swift<br \/>\n   let numberCarouselView = UIView()<br \/>\n   numberCarouselView.frame = CGRect(x: 0, y: 0, width: 300, height: 200)<br \/>\n   view.addSubview(numberCarouselView)<br \/>\n   &#8220;`<\/p>\n<p>2. **\u6dfb\u52a0 UIScrollView**\uff1a<br \/>\n   \u5728\u5bb9\u5668\u8996\u5716\u4e2d\uff0c\u6dfb\u52a0\u4e00\u500b UIScrollView\uff0c\u9019\u5c07\u7528\u65bc\u986f\u793a\u6578\u5b57\u3002<\/p>\n<p>   &#8220;`swift<br \/>\n   let scrollView = UIScrollView()<br \/>\n   scrollView.frame = numberCarouselView.bounds<br \/>\n   numberCarouselView.addSubview(scrollView)<br \/>\n   &#8220;`<\/p>\n<p>3. **\u914d\u7f6e\u6578\u5b57**\uff1a<br \/>\n   \u4f7f\u7528\u4e00\u500b\u6578\u7d44\u4f86\u5132\u5b58\u60a8\u60f3\u8981\u986f\u793a\u7684\u6578\u5b57\uff0c\u7136\u5f8c\u5c07\u5b83\u5011\u6dfb\u52a0\u5230 UIScrollView \u4e2d\u3002<\/p>\n<p>   &#8220;`swift<br \/>\n   let numbers = [1, 2, 3, 4, 5]\n   let labelWidth: CGFloat = 100<br \/>\n   for (index, number) in numbers.enumerated() {<br \/>\n       let label = UILabel()<br \/>\n       label.text = &#8220;\\(number)&#8221;<br \/>\n       label.frame = CGRect(x: CGFloat(index) * labelWidth, y: 0, width: labelWidth, height: 200)<br \/>\n       label.textAlignment = .center<br \/>\n       scrollView.addSubview(label)<br \/>\n   }<br \/>\n   scrollView.contentSize = CGSize(width: labelWidth * CGFloat(numbers.count), height: 200)<br \/>\n   &#8220;`<\/p>\n<p>4. **\u8a2d\u7f6e\u81ea\u52d5\u8f2a\u64ad**\uff1a<br \/>\n   \u4f7f\u7528 Timer \u4f86\u5be6\u73fe\u6578\u5b57\u81ea\u52d5\u8f2a\u64ad\u529f\u80fd\u3002<\/p>\n<p>   &#8220;`swift<br \/>\n   var currentIndex = 0<br \/>\n   var timer: Timer?<\/p>\n<p>   func startCarousel() {<br \/>\n       timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { [weak self] _ in<br \/>\n           guard let self = self else { return }<br \/>\n           self.currentIndex = (self.currentIndex + 1) % self.numbers.count<br \/>\n           let offset = CGPoint(x: CGFloat(self.currentIndex) * labelWidth, y: 0)<br \/>\n           self.scrollView.setContentOffset(offset, animated: true)<br \/>\n       }<br \/>\n   }<\/p>\n<p>   override func viewWillDisappear(_ animated: Bool) {<br \/>\n       super.viewWillDisappear(animated)<br \/>\n       timer?.invalidate()<br \/>\n   }<br \/>\n   &#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>&#8211; **\u7121\u6cd5\u6efe\u52d5**\uff1a\u6aa2\u67e5 UIScrollView \u7684 contentSize \u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u3002<br \/>\n&#8211; **\u6578\u5b57\u4e0d\u986f\u793a**\uff1a\u78ba\u8a8d UILabel \u7684 frame \u548c\u4f4d\u7f6e\u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u5c07\u6578\u5b57\u8f2a\u64ad\u5668\u61c9\u7528\u65bc\u591a\u7a2e\u5834\u666f\uff0c\u4f8b\u5982\u5c55\u793a\u7d71\u8a08\u6578\u64da\u3001\u8a08\u6578\u5668\u6216\u986f\u793a\u6642\u9593\u3002\u60a8\u9084\u53ef\u4ee5\u6dfb\u52a0\u52d5\u756b\u6548\u679c\uff0c\u4f7f\u5176\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift \u6578\u5b57\u8f2a\u64ad\u5668 \ud83d\udd22\ud83d\udd04 \u662f\u4e00\u500b\u7c21\u55ae\u537b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u63d0\u5347\u60a8\u61c9\u7528\u7a0b\u5e8f\u7684\u4e92\u52d5\u6027\u3002\u9075\u5faa\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u4e00\u500b\u6578\u5b57\u8f2a\u64ad\u5668\uff0c\u70ba\u7528\u6236\u63d0\u4f9b\u66f4\u597d\u7684\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u6578\u5b57\u8f2a\u64ad\u5668-\ud83d\udd22\ud83d\udd04_1675476385.7554271.webp\" alt=\"Swift \u6578\u5b57\u8f2a\u64ad\u5668 \ud83d\udd22\ud83d\udd04 \u793a\u4f8b\" title=\"Swift \u6578\u5b57\u8f2a\u64ad\u5668\u3001Swift \u6578\u5b57\u8f2a\u64ad\u3001Swift \u8f2a\u64ad\u5668\u3001Swift \u6578\u5b57\u3001Swift \u51fd\u6578\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u6211\u53ef\u4ee5\u5c07\u6578\u5b57\u8f2a\u64ad\u5668\u61c9\u7528\u5728\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\u4e0a\u55ce\uff1f<\/h3>\n<p>A1: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5c07\u8f2a\u64ad\u5668\u61c9\u7528\u65bc\u4efb\u4f55\u985e\u578b\u7684\u6578\u64da\u5c55\u793a\uff0c\u4f8b\u5982\u5716\u7247\u3001\u6587\u672c\u6216\u5716\u5f62\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u4f7f\u6578\u5b57\u8f2a\u64ad\u5668\u66f4\u5177\u5438\u5f15\u529b\uff1f<\/h3>\n<p>A2: \u60a8\u53ef\u4ee5\u6dfb\u52a0\u52d5\u756b\u6548\u679c\u3001\u984f\u8272\u8b8a\u5316\u6216\u97f3\u6548\u4f86\u589e\u5f37\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h3>Q3: \u5982\u679c\u6211\u60f3\u505c\u6b62\u8f2a\u64ad\uff0c\u6211\u8a72\u600e\u9ebc\u505a\uff1f<\/h3>\n<p>A3: \u60a8\u53ef\u4ee5\u4f7f\u7528 `timer?.invalidate()` \u65b9\u6cd5\u4f86\u505c\u6b62 Timer\uff0c\u5f9e\u800c\u505c\u6b62\u6578\u5b57\u8f2a\u64ad\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u6578\u5b57\u8f2a\u64ad\u5668\ud83d\udd22\ud83d\udd04\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u51fd\u6578\u4f86\u5efa\u7acb\u6578\u5b57\u8f2a\u64ad\u5668\uff0c\u4e26\u638c\u63e1\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u6539\u5584\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":7468,"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-7469","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-\u6578\u5b57\u8f2a\u64ad\u5668-\ud83d\udd22\ud83d\udd04_1675476385.7554271.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Wt","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7469","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=7469"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7469\/revisions"}],"predecessor-version":[{"id":13464,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7469\/revisions\/13464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7468"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}