{"id":297,"date":"2014-12-13T15:27:12","date_gmt":"2014-12-13T07:27:12","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=297"},"modified":"2016-07-14T22:45:28","modified_gmt":"2016-07-14T14:45:28","slug":"%e4%b8%80%e8%a1%8c%e4%bb%a3%e7%a0%81%e6%90%9e%e5%ae%9a%e6%b7%98%e5%ae%9d%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\/13\/152712297.html","title":{"rendered":"\u4e00\u884c\u4ee3\u7801\u641e\u5b9a\u6dd8\u5b9d\u89c6\u56fe\u5207\u6362\u6548\u679c"},"content":{"rendered":"<p><strong>1\u3001\u5148\u4e0a\u56fe\uff0c\u65e0\u56fe\u65e0\u771f\u76f8\u65e0\u53d1\u8a00\u6743<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-298\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.55.jpg\" alt=\"iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.55\" width=\"150\" height=\"225\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-299\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.56.jpg\" alt=\"iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.56\" width=\"150\" height=\"225\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-300\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.57.jpg\" alt=\"iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.57\" width=\"150\" height=\"225\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-301\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.58.jpg\" alt=\"iOS-Simulator-Screen-Shot-2014\u5e7412\u670813\u65e5-\u4e0b\u53483.09.58\" width=\"150\" height=\"225\" \/><\/p>\n<p><strong>2\u3001\u57fa\u672c\u8bbe\u8ba1\u601d\u8def<\/strong><!--more--><\/p>\n<p>2.1\u3001\u8be5\u63a7\u4ef6\u7ee7\u627f\u81eaUIView\uff0c\u5728UIView\u4e0a\u6dfb\u52a0\u4e00\u4e2aScrollView\uff0c\u518d\u5728ScrollView\u4e0a\u6dfb\u52a0\u6309\u94ae\u53ca\u6807\u8bb0(UIView)<br \/>\n2.2\u3001\u521d\u59cb\u5316\u7684\u65f6\u5019\u521b\u5efa\u76f8\u5173\u5b50\u89c6\u56fe\uff08ScrollView\u3001UIButton\u3001UIView\uff09<br \/>\n2.3\u3001\u7528\u6237\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u6839\u636e\u8be5\u6570\u7ec4\u521b\u5efa\u76f8\u5bf9\u5e94\u7684\u6309\u94ae\uff0c\u5e76\u81ea\u52a8\u6309\u987a\u5e8f\u5747\u5300\u6446\u653e\u5728\u89c6\u56fe\u4e0a<br \/>\n2.4\u3001\u5f53\u7528\u6237\u70b9\u51fb\u5176\u4e2d\u4e00\u4e2a\u6309\u94ae\u65f6\uff0c\u8be5\u6309\u94ae\u5904\u7406\u9009\u4e2d\u72b6\u6001\uff0c\u4e0b\u6807\u901a\u8fc7\u52a8\u753b\u5f62\u5f0f\u79fb\u52a8\u5230\u8be5\u6309\u94ae\u4e0b\u65b9\uff0c\u5e76\u901a\u77e5\u4ee3\u7406\u5904\u7406\u76f8\u5173\u4e8b\u4ef6\uff08\u5982\uff1a\u5207\u6362\u89c6\u56fe\uff09<br \/>\n2.5\u3001\u6269\u5c55\u6027\uff1a\u4e3a\u4ec0\u4e48\u4e0b\u9762\u8fd8\u8981\u653e\u4e00\u4e2aScrollView\u5462\uff1f\u56e0\u4e3a\u5f53\u6dfb\u52a0\u7684\u6309\u94ae\u8fc7\u591a\uff0c\u65e0\u6cd5\u4e00\u5c4f\u663e\u793a\u7684\u65f6\u5019\u9700\u8981\u80fd\u591f\u5de6\u53f3\u62d6\u52a8\uff08\u8fd9\u91cc\u8fd8\u672a\u5b9e\u73b0\uff0c\u4f46\u9884\u7559\u4e86\uff09<\/p>\n<p><strong>3\u3001\u4f7f\u7528\u8bf4\u660e<\/strong><\/p>\n<p>3.1\u3001\u4ee3\u7801\u521b\u5efa\uff1a<br \/>\n<span style=\"color: #ff0000;\">SASwitchBar *switchBar = [SASwitchBar alloc] initWithItems:@[@&#8221;\u6807\u9898\u4e00&#8221; @&#8221;\u6807\u9898\u4e8c&#8221;, @&#8221;\u6807\u9898\u4e09&#8221;, @&#8221;\u6807\u9898\u56db&#8221;]];<\/span><br \/>\n<span style=\"color: #ff0000;\"> switchBar.frame = (CGRect){0, 0, 320, 40};<\/span><br \/>\n<span style=\"color: #ff0000;\"> [self.view addsubViews:switchBar]<\/span><br \/>\n3.2\u3001xib\u521b\u5efa\uff1a<br \/>\n3.2.1\u3001\u62d6\u62fd\u4e00\u4e2aUIView\u5230\u4f60\u60f3\u8981\u7684\u4f4d\u7f6e\uff0c\u5e76\u8bbe\u7f6e\u5927\u5c0f<br \/>\n3.2.2\u3001\u8bbe\u7f6e\u8be5View\u7684Class\u4e3aSASwitchBar<br \/>\n3.2.3\u3001\u8fde\u7ebf\uff0c\u8bbe\u7f6e\u6807\u9898\uff0c\u5982\uff1a<span style=\"color: #ff0000;\">self.switchBar.title = [NSArray arrayWithObjects:@&#8221;\u6807\u9898\u4e00&#8221; @&#8221;\u6807\u9898\u4e8c&#8221;, @&#8221;\u6807\u9898\u4e09&#8221;, @&#8221;\u6807\u9898\u56db&#8221;, nil];<\/span><\/p>\n<p><strong>4\u3001\u5173\u952e\u4ee3\u7801\uff1a<\/strong><\/p>\n<p>SASwitchBar.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? SASwitchBar.m\n\/\/? MIG\n\/\/\n\/\/? Created by \u4f59\u897f\u5b89 on 14\/12\/13.\n\/\/? Copyright (c) 2014\u5e74 Sian. All rights reserved.\n\/\/\n#define kSignalH?? 2??????? \/\/\u4e0b\u6807\u9ad8\u5ea6\n#import \"SASwitchBar.h\"\n?\n@interface SASwitchBar()\n{\n????UIScrollView??? *_scrollView;\n????UIView????????? *_signal;?????????? \/\/ \u63a7\u4ef6\u4e0b\u6807\n????UIButton??????? *_selectedItem;???? \/\/ \u5f53\u524d\u63a7\u4ef6\n}\n@end\n?\n@implementation SASwitchBar\n#pragma mark - \u521d\u59cb\u5316\u65b9\u6cd5\n\/\/ \u57fa\u672c\u63a7\u4ef6\u521b\u5efa\n- (void)initialize\n{\n????_signal = [[UIView alloc] init];\n????_scrollView = [[UIScrollView alloc] init];\n????[self addSubview:_scrollView];\n????[_scrollView addSubview:_signal];\n?????\n}\n?\n\/\/ \u4f9bxib\u5f62\u5f0f\u521b\u5efa\n- (instancetype)initWithCoder:(NSCoder *)aDecoder\n{\n????if (self = [super initWithCoder:aDecoder]) {\n????????[self initialize];\n????}\n????return self;\n}\n?\n\/\/ \u4ee5\u4ee3\u7801\u5f62\u5f0f\u521b\u5efa\n- (instancetype)init\n{\n????if (self = [super init]) {\n????????[self initialize];\n????}\n????return self;\n}\n?\n\/\/ \u4ee5\u4ee3\u7801\u5f62\u5f0f\u521b\u5efa\n- (instancetype)initWithItems:(NSArray *)items\n{\n????if (self = [super init]) {\n????????[self initialize];\n????????[self setItems:items];\n????}\n????return self;\n}\n?\n\/\/ \u5b50\u63a7\u4ef6\u4f4d\u7f6e\u5c3a\u5bf8\u8c03\u6574\n- (void)layoutSubviews\n{\n????_scrollView.frame = (CGRect){CGPointZero, self.frame.size};\n?????\n????NSInteger items = self.items.count;\n????CGFloat btnW = self.frame.size.width \/ items;\n????CGFloat btnH = self.frame.size.height - kSignalH;\n????for (int i = 0; i &lt; items; i ++) {\n????????UIButton *btn = self.items[i];\n????????btn.frame = CGRectMake(i * btnW, 0, btnW, btnH);\n????}\n????_signal.frame = CGRectMake(0, btnH, btnW, kSignalH);\n}\n?\n#pragma mark - \u6210\u5458\u5c5e\u6027\u8bbe\u7f6e\n\/\/ \u8bbe\u7f6e\u9009\u62e9\u9879(\u4f20\u5165\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u521b\u5efa\u5bf9\u5e94\u9009\u62e9\u63a7\u4ef6)\n- (void)setItems:(NSArray *)items\n{\n????NSMutableArray *array = [NSMutableArray array];\n?????\n????\/\/ \u521b\u5efa\u591a\u4e2a\u6309\u94ae\u5e76\u8bbe\u7f6etag\uff0c\u6dfb\u52a0\u5230ScrollView\n????for (int i = 0; i &lt; items.count; i++) {\n????????UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];\n????????btn.tag = i + 1;\n????????[array addObject:btn];\n????????[btn setTitle:items[i] forState:UIControlStateNormal];\n????????btn.titleLabel.font = [UIFont boldSystemFontOfSize:15];\n????????[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];\n????????[btn addTarget:self action:@selector(switchItem:) forControlEvents:UIControlEventTouchUpInside];\n????????[_scrollView addSubview:btn];\n????}\n????_items = [NSArray arrayWithArray:array];\n?????\n????\/\/ \u9ed8\u8ba4\u9009\u4e2d\u989c\u8272\u4e3a\u6a59\u8272\uff0c\u9009\u4e2d\u7b2c\u4e00\u4e2a\u63a7\u4ef6\n????self.selColor = [UIColor orangeColor];\n????self.selectedIndex = 0;;\n}\n?\n\/\/ \u5f53\u524d\u9009\u4e2d\u5e8f\u53f7\uff08\u4e0b\u6807\u8ddf\u968f\u9009\u4e2d\u63a7\u4ef6\u79fb\u52a8\u4f4d\u7f6e\uff09\n- (void)setSelectedIndex:(NSInteger)selectedIndex\n{\n????_selectedIndex = selectedIndex;\n????[self switchItem:self.items[selectedIndex]];\n}\n?\n\/\/ \u8bbe\u7f6e\u9ad8\u4eae\u989c\u8272\uff08\u9009\u4e2d\u63a7\u4ef6\u6587\u5b57\u989c\u8272\u53ca\u4e0b\u6807\u989c\u8272\uff09\n- (void)setSelColor:(UIColor *)selColor\n{\n????_selColor = selColor;\n????_signal.backgroundColor = selColor;\n????for (UIButton *btn in self.items) {\n????????[btn setTitleColor:selColor forState:UIControlStateSelected];\n????}\n}\n?\n#pragma mark - \u4e8b\u4ef6\u5904\u7406\n\/\/ \u6309\u94ae\u4e8b\u4ef6\n- (void)switchItem:(UIButton *)button\n{\n????\/\/ \u5207\u6362\u6309\u952e\uff0c\u53d8\u91cf\u6309\u94ae\u63a7\u4ef6\u72b6\u6001\n????_selectedItem.selected = NO;\n????_selectedItem = button;\n????button.selected = YES;\n?????\n????\/\/ \u8c03\u6574\u4e0b\u6807\u4f4d\u7f6e\uff0c\u52a8\u753b\u6548\u679c\u8fc7\u6e21\n????CGRect rect = button.frame;\n????CGFloat x = rect.origin.x;\n????CGFloat y = rect.origin.y + rect.size.height;\n????CGFloat w = rect.size.width;\n????CGFloat h = rect.size.height;\n????[UIView animateWithDuration:0.3 animations:^{\n????????_signal.frame = CGRectMake(x, y, w, h);\n????}];\n?????\n????\/\/ \u901a\u77e5\u4ee3\u7406\n????if ([self.delegate respondsToSelector:@selector(switchBar:seletedIndex:)]) {\n????????[self.delegate switchBar:self seletedIndex:button.tag];\n????}\n}\n@end<\/pre>\n<p><strong>5\u3001Demo\u4e0b\u8f7d\uff1a<\/strong><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/SASwitchBar.zip\">SASwitchBar<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u4e0a\u56fe\uff0c\u65e0\u56fe\u65e0\u771f\u76f8\u65e0\u53d1\u8a00\u6743 2\u3001\u57fa\u672c\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-297","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\/297","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=297"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}