{"id":304,"date":"2014-12-19T09:54:22","date_gmt":"2014-12-19T01:54:22","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=304"},"modified":"2016-07-14T22:45:22","modified_gmt":"2016-07-14T14:45:22","slug":"%e8%bd%bb%e6%9d%be%e5%ae%9e%e7%8e%b0%e6%a0%87%e7%ad%be%e8%a7%86%e5%9b%be%e5%88%87%e6%8d%a2%e6%95%88%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/12\/19\/095422304.html","title":{"rendered":"\u8f7b\u677e\u5b9e\u73b0\u6807\u7b7e\u89c6\u56fe\u5207\u6362\u6548\u679c"},"content":{"rendered":"<p><strong>\u4e00\u3001\u5148\u4e0a\u6548\u679c\u56fe\uff0c\u770b\u4e86\u81ea\u7136\u5c31\u660e\u767d<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-305\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/2014-12-19-09_14_05.gif\" alt=\"2014-12-19 09_14_05\" width=\"280\" height=\"497\" \/><\/p>\n<p><strong>\u4e8c\u3001\u8bbe\u8ba1\u601d\u8def<\/strong><!--more--><\/p>\n<p>1\u3001\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\u6216\u6548\u679c\u5462\uff0c\u9996\u5148\u9876\u90e8\u6807\u7b7e\u5207\u6362\u6548\u679c\u5728\u524d\u9762\u5df2\u6709\u4e13\u95e8\u7684\u8d34\u5b50\u505a\u4e86\u8be6\u7ec6\u8bf4\u660e\uff0c\u53c2\u8003\uff1a<a href=\"http:\/\/www.yusian.com\/blog\/experience\/2014\/12\/13\/152712.html\" target=\"_blank\">\u4e00\u884c\u4ee3\u7801\u641e\u5b9a\u6dd8\u5b9d\u89c6\u56fe\u5207\u6362\u6548\u679c<\/a><\/p>\n<p>2\u3001\u4e0b\u9762\u89c6\u56fe\u5207\u6362\u7684\u57fa\u672c\u601d\u8def\u662f\u6839\u636e\u4e0a\u9762\u6807\u7b7e\u7684\u6570\u76ee\u521b\u5efa\u5bf9\u5e94\u591a\u4e2aView\uff0c\u653e\u5230ScrollView\u4e0a\u9762\uff0c\u901a\u8fc7pageingEnable\u5c5e\u6027\u5206\u5c4f\u5207\u6362<\/p>\n<p>3\u3001\u901a\u8fc7ScrollView\u7684\u4ee3\u7406\u65b9\u6cd5\uff0c\u76d1\u542c\u5f53\u524dScrollView\u7684\u6eda\u52a8\u72b6\u6001\u4e0e\u4e0a\u9762\u6807\u7b7e\u680f\u8054\u52a8<\/p>\n<p>4\u3001\u540c\u6837\uff0c\u4e0a\u9762\u6807\u7b7e\u680f\u70b9\u51fb\u4e8b\u4ef6\u52a8\u6001\u6539\u53d8ScrollView\u7684contentOffset\uff0c\u4f7f\u5f97\u4e0b\u9762\u89c6\u56fe\u4e0e\u6807\u7b7e\u4fdd\u6301\u5bf9\u5e94\u4e00\u81f4<\/p>\n<p><strong>\u4e09\u3001\u4f7f\u7528\u8bf4\u660e<\/strong><\/p>\n<p>1\u3001\u8be5\u63a7\u4ef6\u5305\u542b\u4e09\u4e2a\u7c7b\uff1aSASwitchBar\u3001SASwitchView\u3001SASwitchCtrl\uff0c\u5206\u522b\u4e3a\u6807\u7b7e\u680f\u3001\u89c6\u56fe\u3001\u63a7\u5236\u5668\uff0c\u524d\u4e24\u4e2a\u7c7b\u4e3a\u89c6\u56fe\u8d1f\u8d23\u5c55\u793a\u5e76\u76d1\u542c\u4e8b\u4ef6\uff0c\u6700\u540e\u4e00\u4e2a\u7c7b\u8d1f\u8d23\u4e8b\u4ef6\u5904\u7406\u4e0e\u89c6\u56fe\u63a7\u5236\uff1b<\/p>\n<p>2\u3001\u4f7f\u7528\u65f6\u53ea\u9700\u8981\u65b0\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u5e76\u7ee7\u627f\u81eaSASwitchCtrl\u5373\u53ef\uff0c\u7136\u540e\u8bbe\u7f6eSASwitchBar\u7684items\u6570\u7ec4\uff0c\u518d\u8bbe\u7f6eSASwitchView\u7684\u89c6\u56fe\u6570\u7ec4\uff0c\u641e\u5b9a\uff01<\/p>\n<p>3\u3001\u793a\u4f8b\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">    \/\/ \u8bbe\u7f6e\u6807\u7b7e\u9009\u9879\u5361\n    self.switchBar.items = @[@\"\u84dd\", @\"\u6a59\", @\"\u7ea2\"];\n    \n    \/\/ \u521b\u5efa\u4e09\u4e2a\u5207\u6362\u6f14\u793a\u89c6\u56fe\n    UIView *blue = [[UIView alloc] init];\n    blue.backgroundColor = [UIColor blueColor];\n    UIView *orange = [[UIView alloc] init];\n    orange.backgroundColor = [UIColor orangeColor];\n    UIView *red = [[UIView alloc] init];\n    red.backgroundColor = [UIColor redColor];\n    \n    \/\/ \u6dfb\u52a0\u6f14\u793a\u89c6\u56fe\n    self.switchView.views = @[blue, orange, red];<\/pre>\n<p><strong>\u56db\u3001\u5173\u952e\u4ee3\u7801<\/strong><\/p>\n<p>SASwitchView.m<\/p>\n<pre lang=\"objc\" line=\"1\">#import \"SASwitchView.h\"\n\n@implementation SASwitchView\n\n- (void)setViews:(NSArray *)views\n{\n    _views = views;\n    CGFloat width = [[UIScreen mainScreen] bounds].size.width;\n    \/\/ \u5728\u89c6\u56fe\u6570\u7ec4\u8d4b\u503c\u65f6\uff0c\u540c\u65f6\u6574\u7406\u5404\u89c6\u56fe\u7684\u5e03\u5c40\uff0c\u4e3b\u8981\u662fx\/y\u5750\u6807\uff0c\u6b64\u65f6\u8fd8\u4e0d\u80fd\u786e\u5b9a\u89c6\u56fe\u5927\u5c0f\n    for (int i = 0; i &lt; self.views.count; i++){\n        UIView *view = self.views[i];\n        view.frame = (CGRect){width * i, 0, width, 0};\n        [self addSubview:view];\n    }\n    self.contentSize = (CGSize){width * views.count, 0};\n}\n\n- (void)setFrame:(CGRect)frame\n{\n    [super setFrame:frame];\n    \/\/ \u5916\u90e8\u7ed9\u89c6\u56fe\u8bbe\u7f6e\u5927\u5c0f\u65f6\uff0c\u540c\u65f6\u8c03\u6574\u5185\u90e8\u5b50\u89c6\u56fe\uff0c\u4fdd\u6301\u5b50\u89c6\u56fe\u4e0e\u7236\u63a7\u5236\u5927\u5c0f\u4e00\u81f4\n    for (UIView *view in self.subviews) {\n        CGRect rect = view.frame;\n        rect.size.height = frame.size.height;\n        view.frame = rect;\n    }\n}\n\n@end<\/pre>\n<p>SASwitchCtrl.m<\/p>\n<pre lang=\"objc\" line=\"1\">#define kSASwitchBarH 44\n#import \"SASwitchCtrl.h\"\n\n@interface SASwitchCtrl () &lt;SASwitchBarDelegate, UIScrollViewDelegate&gt;\n\n@end\n\n@implementation SASwitchCtrl\n\n#pragma mark - \u521d\u59cb\u5316\u65b9\u6cd5\n- (instancetype)init\n{\n    if (self = [super init]) {\n        self.switchBar = [[SASwitchBar alloc] init];\n        self.switchView = [[SASwitchView alloc] init];\n        self.switchView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;\n        [self.view addSubview:self.switchBar];\n        [self.view addSubview:self.switchView];\n    }\n    return self;\n}\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    self.edgesForExtendedLayout = UIRectEdgeNone;\n    self.switchView.showsHorizontalScrollIndicator = NO;\n    self.switchView.pagingEnabled = YES;\n    self.switchBar.delegate = self;\n    self.switchView.delegate = self;\n}\n\n- (void)viewWillAppear:(BOOL)animated\n{\n    [super viewWillAppear:animated];\n    {\n        CGFloat w = self.view.frame.size.width;\n        CGFloat h = self.view.frame.size.height - kSASwitchBarH;\n        self.switchBar.frame = (CGRect){0, 0, w, kSASwitchBarH};\n        self.switchView.frame = (CGRect){0, kSASwitchBarH, w, h};\n    }\n}\n\n#pragma mark - \u4ee3\u7406\u65b9\u6cd5\n\/\/ \u4e0a\u90e8\u6807\u7b7e\u680f\u4e8b\u4ef6\u5904\u7406\n- (void)switchBar:(SASwitchBar *)switchBar seletedIndex:(NSInteger)index\n{\n    \/\/ \u6807\u7b7e\u70b9\u51fb\u7b2c\u51e0\u4e2a\uff0cscrollView\u7684offset\u5373\u8df3\u8f6c\u5230\u7b2c\u51e0\u5c4f\uff0c\u5e76\u4ee5\u52a8\u753b\u5f62\u5f0f\u8f6c\u573a\n    CGPoint offset = (CGPoint){self.view.frame.size.width * index, 0};\n    [UIView animateWithDuration:0.3 animations:^{\n        self.switchView.contentOffset = offset;\n    }];\n}\n\/\/ scrollView\u4ee3\u7406\u65b9\u6cd5\n- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView\n{\n    int page = scrollView.contentOffset.x \/ self.view.frame.size.width;\n    self.switchBar.selectedIndex = page;\n}\n@end<\/pre>\n<p><strong>\u4e94\u3001Demo\u4e0b\u8f7d\uff1a<\/strong><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/SASwitch.zip\">SASwitch<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5148\u4e0a\u6548\u679c\u56fe\uff0c\u770b\u4e86\u81ea\u7136\u5c31\u660e\u767d \u4e8c\u3001\u8bbe\u8ba1\u601d\u8def<\/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],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-project","tag-module"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/304","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=304"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}