{"id":286,"date":"2015-07-01T15:03:47","date_gmt":"2015-07-01T07:03:47","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=286"},"modified":"2016-07-14T22:52:05","modified_gmt":"2016-07-14T14:52:05","slug":"%e8%bd%bb%e6%9d%be%e5%ae%9e%e7%8e%b0uiscrollview%e5%be%aa%e7%8e%af%e6%bb%9a%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2015\/07\/01\/150347286.html","title":{"rendered":"\u8f7b\u677e\u5b9e\u73b0UIScrollView\u5faa\u73af\u6eda\u52a8"},"content":{"rendered":"<p><strong>1\u3001\u5148\u4e0a\u56fe<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-287\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/\u672a\u547d\u540d-1.gif\" alt=\"\u672a\u547d\u540d-1\" width=\"320\" height=\"569\" \/><\/p>\n<p><strong>2\u3001\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>2.1\u3001\u591a\u4e2a\u89c6\u56fe\u9700\u8981\u5faa\u73af\u64ad\u653e\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u6709\u591a\u5c11\u4e2a\u89c6\u56fe\u521b\u5efa\u4e2a\u591a\u5c11\u4e2aView\u6392\u5217\u5728ScrollView\u5373\u53ef\uff0c\u4f46\u5982\u679c\u89c6\u56fe\u8fc7\u591a\u4f1a\u76f4\u63a5\u5f71\u54cd\u5230\u624b\u673a\u6027\u80fd\u53ca\u5207\u6362\u6548\u679c\u3002<\/p>\n<p>2.2\u3001\u89c6\u56fe\u4e2a\u6570\u4e0d\u786e\u5b9a\u7684\u573a\u666f\uff0c\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u91c7\u7528\u7684\u662fMVC\u7684\u6807\u51c6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5373\u89c6\u56fe\u7531\u6570\u636e\u51b3\u5b9a\uff0c\u4f46\u6570\u636e\u7684\u53d8\u5316\u6ca1\u6cd5\u4e8b\u5148\u786e\u5b9a\uff0c\u56e0\u6b64\u89c6\u56fe\u5fc5\u987b\u5177\u5907\u4e00\u5b9a\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>3\u3001\u8bbe\u8ba1\u601d\u60f3<\/strong><\/p>\n<p>3.1\u3001\u521b\u5efa\u57fa\u7840ScrollView\u5e76\u6dfb\u52a0\u4e09\u4e2a\u5b50\u63a7\u4ef6\u5230ScrollView\uff0c\u4e3a\u4ec0\u4e48\u662f\u4e09\u4e2a\u5462\uff1f\u8fd9\u4e2a\u95ee\u9898\u95ee\u5f97\u597d\uff0c\u6211\u4eec\u4e00\u822c\u60c5\u51b5\u4e0b\u5c55\u793a\u7b2c\u4e8c\u4e2a\uff0c\u5f80\u5de6\u5207\u6362\u5230\u7b2c\u4e00\u4e2a\uff0c\u5f80\u53f3\u5207\u6362\u5230\u7b2c\u4e09\u4e2a\uff0c\u5207\u6362\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u4e00\u7cfb\u5217\u7b97\u6cd5\u7acb\u5373\u5c06\u5f53\u524d\u89c6\u56fe\u5207\u6362\u5230\u4e2d\u95f4\u4e00\u4e2a\u7684\u4f4d\u7f6e\uff0c\u8c03\u6574\u5b50\u89c6\u56fe\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u5b9e\u73b0\u91cd\u590d\u6548\u679c\uff01<\/p>\n<p>3.2\u3001\u770b\u5982\u4e0b\u793a\u610f\u56fe\uff1a<!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-288\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/000.png\" alt=\"000\" width=\"600\" height=\"596\" \/><\/p>\n<p>3.3\u3001\u9996\u5148\u5c06\u5b50\u63a7\u4ef6\u7684tag\u8fdb\u884c\u7f16\u53f7\uff0c\u59821\u30012\u30013\uff08\u5c3d\u91cf\u4e0d\u8981\u75280\uff0c\u56e0\u4e3a\u4e0d\u6392\u961f\u6709\u5176\u4ed6\u5b50\u63a7\u4ef6\u7684\u5f71\u54cd\uff0cview\u9ed8\u8ba4tag\u4e3a0\uff09\uff0c\u901a\u8fc7tag\u51b3\u5b9a\u4f4d\u7f6e\uff0c\u5982\uff1atag\u4e3a1\u7684\u5b50\u63a7\u4ef6\uff0cx\u4e3awidth * 0\uff0ctag\u4e3a1\u7684\u5b50\u63a7\u4ef6x\u4e3awidth * 1\uff0ctag\u4e3a2\u7684\u5b50\u63a7\u4ef6x\u4e3awidth * 2\uff0c\u5bbd\u5ea6\u9ad8\u5ea6\u76f8\u7b49\uff0cy\u9ed8\u8ba4\u90fd\u4e3a0\u3002<\/p>\n<p>3.4\u3001\u6709\u4e86\u4e0a\u9762\u8fd9\u4e2a\u524d\u63d0\u5c31\u65b9\u4fbf\u4e86\uff0c\u6211\u4eec\u8c03\u6574\u5b50\u63a7\u4ef6\u5373\u8f6c\u6362\u4e3a\u8c03\u6574\u5b50\u63a7\u4ef6\u7684tag\u503c\u5373\u53ef\uff0c\u7136\u540e\u518d\u901a\u8fc7tag\u503c\u91cd\u65b0\u8ba1\u7b97\u5b50\u63a7\u4ef6\u7684\u4f4d\u7f6e\u5c31\u80fd\u5b9e\u73b0\u9700\u8981\u7684\u6548\u679c\u3002<\/p>\n<p>3.5\u3001\u6709\u70b9\u62bd\u8c61\uff1f\u770b\u4ee3\u7801\uff01<\/p>\n<p><strong>4\u3001\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? ViewController.m\n\/\/? Test\n\/\/\n\/\/? Created by \u4f59\u897f\u5b89 on 15\/7\/1.\n\/\/? Copyright (c) 2015\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import \"ViewController.h\"\n?\n@interface ViewController () \n@property (nonatomic, assign) BOOL???????? firstAppear;\n@property (nonatomic, strong) UIScrollView *scrollView;\n@end\n?\n@implementation ViewController\n?\n- (void)viewDidLoad\n{\n????[super viewDidLoad];\n????self.firstAppear = YES;\n????\/\/ \u521b\u5efa\u57fa\u7840ScrollView\u63a7\u4ef6\n????UIScrollView *scrollView = [[UIScrollView alloc] init];\n????scrollView.pagingEnabled = YES;\n????scrollView.delegate = self;\n?\n????\/\/ \u521b\u5efa\u4e09\u4e2a\u5b50\u63a7\u4ef6\u5e76\u6dfb\u52a0\u5230ScrollView\uff08\u5b50\u63a7\u4ef6\u53ef\u4ee5\u662fUIView\u7684\u4efb\u4f55\u5b50\u7c7b\uff0c\u5e38\u7528\u7684\u5982\uff1aUIImageView\u3001UIWebView\u3001UITextView\u7b49\uff09\n????for (int i = 0; i &lt; 3; i++) {\n????????UILabel *label = [[UILabel alloc] init];\n????????label.tag = i + 1;\n????????label.textAlignment = NSTextAlignmentCenter;\n????????label.text = [NSString stringWithFormat:@\"%ld\", label.tag];\n????????label.font = [UIFont systemFontOfSize:160];\n????????[scrollView addSubview:label];\n????}\n????self.scrollView = scrollView;\n????[self.view addSubview:scrollView];\n}\n?\n- (void)viewWillAppear:(BOOL)animated\n{\n????[super viewWillAppear:animated];\n????if (self.firstAppear){\n????????[self viewFirstAppear:animated];\n????}\n}\n?\n- (void)viewDidAppear:(BOOL)animated\n{\n????[super viewDidAppear:animated];\n????self.firstAppear = NO;\n}\n?\n\/\/\/ \u7b2c\u4e00\u6b21\u51fa\u73b0\u8be5View\u65f6\u8c03\u6574\u6240\u6709\u63a7\u4ef6\u4f4d\u7f6e\u5c3a\u5bf8\n- (void)viewFirstAppear:(BOOL)aninated\n{\n????self.scrollView.frame = self.view.bounds;\n????CGSize size = self.view.bounds.size;\n????for (UIView *view in self.scrollView.subviews) {\n????????NSInteger index = view.tag - 1;\n????????\/\/ \u8fc7\u6ee4\u6389scrollView\u539f\u6709\u7684\u5b50\u63a7\u4ef6\n????????if (index &lt; 0) continue;\n????????CGFloat x = index * size.width;\n????????view.frame = (CGRect){x, 0, size};\n????}\n????self.scrollView.contentOffset = CGPointMake(size.width, 0);\n????self.scrollView.contentSize = CGSizeMake(size.width * 3, size.height);\n}\n?\n\/\/\/ scrollView\u505c\u6b62\u51cf\u901f\u65f6\u8c03\u7528\uff0c\u6838\u5fc3\u7b97\u6cd5\u5728\u8fd9\u91cc\uff01\uff01\n- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView\n{\n????CGFloat width = scrollView.bounds.size.width;\n????CGFloat offsetX = scrollView.contentOffset.x;\n????NSInteger index = offsetX \/ width;\n????\/\/ \u8ba9scrollView\u518d\u6b21\u8fd4\u56de\u5230\u6700\u4e2d\u95f4\u4f4d\u7f6e\n????scrollView.contentOffset = CGPointMake(width, 0);\n????\/\/ index\u53d6\u503c\u4e3a0\u30011\u30012\u5206\u522b\u5bf9\u5e94\u5f53\u524d\u4e3a\u7b2c\u51e0\u4e2a\u89c6\u56fe\n????switch (index){\n????????case 0:{??? \/\/ \u5f80\u5de6\u4fa7\u6ed1\uff08\u624b\u52bf\u5f80\u5de6\uff09\uff0c\u6240\u6709\u63a7\u4ef6\u5f80\u5de6\u79fb\u4e00\u4e2a\u5355\u4f4d\uff0c\u51fa\u754c\u8865\u6700\u53f3\u8fb9\n????????????for (UIView *view in self.scrollView.subviews) {\n????????????????if (view.tag &lt; 1) continue; \/\/ \u8fc7\u6ee4 ????????????????\/\/ \u5229\u7528tag\u6765\u51b3\u5b9a\u4f4d\u7f6e\uff081\u30012\u30013\uff09-&gt; (2\u30013\u30011)\n????????????????view.tag = (view.tag? % 3) + 1;\n????????????????CGFloat x = (view.tag - 0.5) * width;\n????????????????CGFloat y = view.center.y;\n????????????????view.center = CGPointMake(x, y);\n????????????}\n????????}break;\n?????????????\n????????case 2:{??? \/\/ \u5f80\u53f3\u4fa7\u6ed1\uff08\u624b\u52bf\u5f80\u53f3\uff09\uff0c\u6240\u6709\u63a7\u4ef6\u5f80\u53f3\u79fb\u4e00\u4e2a\u5355\u4f4d\uff0c\u51fa\u754c\u8865\u6700\u5de6\u8fb9\n????????????for (UIView *view in self.scrollView.subviews) {\n????????????????if (view.tag &lt; 1) continue; ????????????????\/\/ \u5229\u7528tag\u6765\u51b3\u5b9a\u4f4d\u7f6e\uff081\u30012\u30013\uff09-&gt; (3\u30011\u30012)\n????????????????view.tag = ((view.tag? + 1) % 3) + 1;\n????????????????CGFloat x = (view.tag - 0.5) * width;\n????????????????CGFloat y = view.center.y;\n????????????????view.center = CGPointMake(x, y);\n????????????}\n????????}break;\n????????default:break;\n????}\n}\n?\n@end<\/pre>\n<p><strong>5\u3001Demo\u4e0b\u8f7d<\/strong> <a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/ScrollView.zip\">ScrollView<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u4e0a\u56fe 2\u3001\u5e94\u7528\u573a\u666f 2.1\u3001\u591a\u4e2a\u89c6\u56fe\u9700\u8981\u5faa\u73af\u64ad\u653e\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u6709\u591a\u5c11\u4e2a\u89c6\u56fe\u521b\u5efa\u4e2a\u591a\u5c11\u4e2aView\u6392\u5217\u5728ScrollView\u5373\u53ef\uff0c\u4f46\u5982\u679c\u89c6\u56fe\u8fc7\u591a\u4f1a\u76f4\u63a5\u5f71\u54cd\u5230\u624b\u673a\u6027\u80fd\u53ca\u5207\u6362\u6548\u679c\u3002 2.2\u3001\u89c6\u56fe\u4e2a\u6570\u4e0d\u786e\u5b9a\u7684\u573a\u666f\uff0c\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u91c7\u7528\u7684\u662fMVC\u7684\u6807\u51c6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5373\u89c6\u56fe\u7531\u6570\u636e\u51b3\u5b9a\uff0c\u4f46\u6570\u636e\u7684\u53d8\u5316\u6ca1\u6cd5\u4e8b\u5148\u786e\u5b9a\uff0c\u56e0\u6b64\u89c6\u56fe\u5fc5\u987b\u5177\u5907\u4e00\u5b9a\u7075\u6d3b\u6027\u3002 3\u3001\u8bbe\u8ba1\u601d\u60f3 3.1\u3001\u521b\u5efa\u57fa\u7840ScrollView\u5e76\u6dfb\u52a0\u4e09\u4e2a\u5b50\u63a7\u4ef6\u5230ScrollView\uff0c\u4e3a\u4ec0\u4e48\u662f\u4e09\u4e2a\u5462\uff1f\u8fd9\u4e2a\u95ee\u9898\u95ee\u5f97\u597d\uff0c\u6211\u4eec\u4e00\u822c\u60c5\u51b5\u4e0b\u5c55\u793a\u7b2c\u4e8c\u4e2a\uff0c\u5f80\u5de6\u5207\u6362\u5230\u7b2c\u4e00\u4e2a\uff0c\u5f80\u53f3\u5207\u6362\u5230\u7b2c\u4e09\u4e2a\uff0c\u5207\u6362\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u4e00\u7cfb\u5217\u7b97\u6cd5\u7acb\u5373\u5c06\u5f53\u524d\u89c6\u56fe\u5207\u6362\u5230\u4e2d\u95f4\u4e00\u4e2a\u7684\u4f4d\u7f6e\uff0c\u8c03\u6574\u5b50\u89c6\u56fe\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u5b9e\u73b0\u91cd\u590d\u6548\u679c\uff01 3.2\u3001\u770b\u5982\u4e0b\u793a\u610f\u56fe\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[15,17],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-project","tag-module","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}