{"id":586,"date":"2023-02-25T19:49:32","date_gmt":"2023-02-25T11:49:32","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=586"},"modified":"2023-04-29T20:31:00","modified_gmt":"2023-04-29T12:31:00","slug":"swift-ui-scroll-view-uiscrollview","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-scroll-view-uiscrollview\/","title":{"rendered":"Swift UI &#8211; \u6eda\u52a8\u89c6\u56fe\uff08UIScrollView\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h3>\u57fa\u672c\u4f7f\u7528<\/h3>\n<p>\u5f53\u5185\u5bb9\u5c3a\u5bf8\u8d85\u8fc7\u5c4f\u5e55\u65f6\uff0c\u4f7f\u7528UIScrollView\u53ef\u4ee5\u5b9e\u73b0\u6eda\u52a8\u6761\u89c6\u56fe\uff0c\u5373\u624b\u6307\u89e6\u6478\u6eda\u52a8\u5c4f\u5e55\u65b9\u4fbf\u6d4f\u89c8\u6574\u4e2a\u9875\u9762\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">let scrollView = UIScrollView()\nscrollView.frame = self.view.bounds\nlet imageView = UIImageView(image:UIImage(named:&quot;bigpic&quot;))\nscrollView.contentSize = imageView.bounds.size\nscrollView.addSubview(imageView)\nself.view.addSubview(scrollView)<\/code><\/pre>\n<h3>\u83b7\u53d6\u6eda\u52a8\u89c6\u56fe\u79fb\u52a8\u7684x,y\u5750\u6807<\/h3>\n<p>\u901a\u8fc7<code>scrollView.contentOffset.x<\/code>\u4e0e<code>scrollView.contentOffset.y<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u53d6\u5230\u79fb\u52a8\u7684\u504f\u79fb\u4f4d\u7f6e<\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController, UIScrollViewDelegate {\n\n    var scrollView:UIScrollView!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        scrollView = UIScrollView()\n        \/\/\u8bbe\u7f6e\u4ee3\u7406\n        scrollView.delegate = self\n        scrollView.frame = self.view.bounds\n        let imageView = UIImageView(image:UIImage(named:&quot;bigpic.jpg&quot;))\n        scrollView.contentSize = imageView.bounds.size\n        scrollView.addSubview(imageView)\n        self.view.addSubview(scrollView)\n    }\n\n    \/\/\u89c6\u56fe\u6eda\u52a8\u4e2d\u4e00\u76f4\u89e6\u53d1\n    func scrollViewDidScroll(_ scrollView: UIScrollView) {\n        print(&quot;x:\\(scrollView.contentOffset.x) y:\\(scrollView.contentOffset.y)&quot;)\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/code><\/pre>\n<h3>\u624b\u52bf\u653e\u5927\u7f29\u5c0f<\/h3>\n<p>\u8981\u5b9e\u73b0\u653e\u5927\u7f29\u5c0f\u529f\u80fd\uff0c\u9700\u8981\u6307\u5b9aUIScrollView\u7684\u5141\u8bb8\u7f29\u653e\u6700\u5927\u6bd4\u4f8b\u548c\u6700\u5c0f\u6bd4\u4f8b\uff08\u9ed8\u8ba4\u90fd\u662f\u662f1.0\uff09\u3002\u540c\u65f6delegate\u5c5e\u6027\u6307\u5b9a\u4e00\u4e2a\u59d4\u6258\u7c7b\uff0c\u59d4\u6258\u7c7b\u8981\u7ee7\u627fUIScrollViewDelegate\u534f\u8bae\uff0c\u5e76\u5728\u59d4\u6258\u7c7b\u4e2d\u5b9e\u73b0viewForZooming\u65b9\u6cd5\u3002<\/p>\n<blockquote>\n<p>\u6ce8\uff1a\u5728\u6a21\u62df\u5668\u4e2d\u6d4b\u8bd5\uff0c\u9700\u8981\u6309\u4f4foption\u952e\u518d\u62d6\u52a8\u5185\u5bb9<\/p>\n<\/blockquote>\n<pre><code class=\"language-swift\">scrollView.minimumZoomScale = 0.1  \/\/\u6700\u5c0f\u6bd4\u4f8b\nscrollView.maximumZoomScale = 3  \/\/\u6700\u5927\u6bd4\u4f8b\nscrollView.delegate = self\n\nfunc viewForZooming(in scrollView: UIScrollView) -&gt; UIView? {\n    for subview : AnyObject in scrollView.subviews {\n        if subview.isKind(of: UIImageView.self) {\n            return subview as? UIView\n        }\n    }\n    return nil\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u57fa\u672c\u4f7f\u7528 \u5f53\u5185\u5bb9\u5c3a\u5bf8\u8d85\u8fc7\u5c4f\u5e55\u65f6\uff0c\u4f7f\u7528UIScrollView\u53ef\u4ee5\u5b9e\u73b0\u6eda\u52a8\u6761\u89c6\u56fe\uff0c [&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-586","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/586","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=586"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/586\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}