{"id":587,"date":"2023-02-25T19:50:27","date_gmt":"2023-02-25T11:50:27","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=587"},"modified":"2023-04-29T20:30:37","modified_gmt":"2023-04-29T12:30:37","slug":"swift-ui-use-uiscrollview-to-achieve-page-scrolling-switch","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-use-uiscrollview-to-achieve-page-scrolling-switch\/","title":{"rendered":"Swift UI &#8211; \u4f7f\u7528UIScrollView\u5b9e\u73b0\u9875\u9762\u6eda\u52a8\u5207\u6362"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<p><code>UIScrollView<\/code>\u63d0\u4f9b\u4e86\u4ee5\u9875\u9762\u4e3a\u5355\u4f4d\u6eda\u52a8\u663e\u793a\u5404\u4e2a\u5b50\u9875\u9762\u5185\u5bb9\u7684\u529f\u80fd\uff0c\u6bcf\u6b21\u624b\u6307\u6ed1\u52a8\u540e\u4f1a\u6eda\u52a8\u4e00\u5c4f\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u9700\u8981\u5982\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u5c06<code>UIScrollView<\/code>\u7684<code>isPagingEnabled<\/code>\u5c5e\u6027\u8bbe\u7f6e\u6210true<\/li>\n<li>\u5fc5\u987b\u901a\u8fc7<code>contentSize<\/code>\u5c5e\u6027\u8bbe\u7f6e\u5404\u4e2a\u9875\u9762\u76f8\u52a0\u7684\u5bbd\u5ea6\u3002\u6bd4\u5982iPhone\u624b\u673a\u4e00\u5c4f\u5bbd\u5ea6\u662f320\uff0c\u5982\u679c\u67093\u4e2a\u9875\u9762\uff0c\u5219<code>contentSize<\/code>\u5c31\u9700\u8981\u8bbe\u7f6e\u4e3a320*3=960<\/li>\n<li>\u6700\u597d\u5c06<code>showsHorizontalScrollIndicator<\/code>\u548c<code>showsVerticalScrollIndicator<\/code>\u8bbe\u7f6e\u6210<code>false<\/code>\u9690\u85cf\u6a2a\u5411\u548c\u7eb5\u5411\u6eda\u52a8\u6761\u3002<\/li>\n<li>\u5982\u679c<code>scrollsToTop<\/code>\u4e0d\u9700\u8981\u4e5f\u8bbe\u7f6e\u6210false\u3002<\/li>\n<\/ol>\n<p><!-- more --><\/p>\n<h3>\u4e3b\u9875\u9762<\/h3>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController {\n    let numOfPages = 3\n    let pageWidth = 320\n    let pageHeight = 360\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        \/\/scrollView\u7684\u521d\u59cb\u5316\n        let scrollView = UIScrollView()\n        scrollView.frame = self.view.bounds\n        \/\/\u4e3a\u4e86\u8ba9\u5185\u5bb9\u6a2a\u5411\u6eda\u52a8\uff0c\u8bbe\u7f6e\u6a2a\u5411\u5185\u5bb9\u5bbd\u5ea6\u4e3a3\u4e2a\u9875\u9762\u7684\u5bbd\u5ea6\u603b\u548c\n        scrollView.contentSize = CGSize(width: pageWidth * numOfPages,\n                                        height: pageHeight)\n        scrollView.isPagingEnabled = true\n        scrollView.showsHorizontalScrollIndicator = false\n        scrollView.showsVerticalScrollIndicator = false\n        scrollView.scrollsToTop = false\n\n        \/\/\u6dfb\u52a0\u5b50\u9875\u9762\n        for i in 0..&lt;numOfPages {\n            let myViewController = MyViewController(number:(i+1))\n            myViewController.view.frame = CGRect(x:pageWidth*i, y:0,\n                                                width:pageWidth, height:pageHeight)\n            scrollView.addSubview(myViewController.view)\n        }\n        self.view.addSubview(scrollView)\n    }\n}<\/code><\/pre>\n<h3>\u5b50\u9875\u9762<\/h3>\n<pre><code class=\"language-swift\">import UIKit\n\nclass MyViewController: UIViewController{\n    var number:Int!\n\n    let colorMap = [\n        1:UIColor.black,\n        2:UIColor.orange,\n        3:UIColor.blue\n    ]\n\n    init(number initNumber:Int) {\n        self.number = initNumber\n        super.init(nibName:nil, bundle:nil)\n    }\n\n    required init?(coder aDecoder: NSCoder) {\n        fatalError(&quot;init(coder:) has not been implemented&quot;)\n    }\n\n    override func viewDidLoad() {\n        let numberLabel = UILabel(frame:CGRect(x:130, y:50, width:50, height:30))\n        numberLabel.text = &quot;\u7b2c\\(number!)\u9875&quot;\n        numberLabel.textColor = UIColor.white\n        self.view.addSubview(numberLabel)\n        self.view.backgroundColor = colorMap[number]\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 UIScrollView\u63d0\u4f9b\u4e86\u4ee5\u9875\u9762\u4e3a\u5355\u4f4d\u6eda\u52a8\u663e\u793a\u5404\u4e2a\u5b50\u9875\u9762\u5185\u5bb9\u7684\u529f\u80fd\uff0c\u6bcf\u6b21\u624b\u6307 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-587","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=587"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/587\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}