這個實現(xiàn)用到第三方框架 HMSegmentControl 具體實現(xiàn)看代碼?
#import "ViewController.h"#import "HMSegmentedControl.h"#define Width self.view.bounds.size.width@interface ViewController (){
UIScrollView *_scrollView;
HMSegmentedControl *_seg;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createSegmentControl];
[self createScrollView];
}
- (void)createScrollView {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, Width, self.view.bounds.size.height - 100)];
_scrollView.contentSize = CGSizeMake(7 * Width, 0);
_scrollView.pagingEnabled = YES;
[self.view addSubview:_scrollView];
_scrollView.delegate = self;
UIView *lastView = nil;
for (int i = 0; i < 7; i++) {
UIView *view = [[UIView alloc] init];
view.backgroundColor = [self randomColor];
view.frame = CGRectMake(lastView?(lastView.frame.origin.x + lastView.bounds.size.width) : 0,0, Width, _scrollView.bounds.size.height);
[_scrollView addSubview:view];
lastView = view;
}
}
- (void)createSegmentControl {
HMSegmentedControl *seg = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"消息",@"關(guān)注",@"收藏",@"我的",@"首頁",@"聯(lián)動",@"購物"]];
seg.frame = CGRectMake(0, 64, self.view.bounds.size.width, 36);
[self.view addSubview:seg];
//? ? seg.backgroundColor = [UIColor grayColor];
seg.selectedSegmentIndex = 0;
// 正常的屬性
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor greenColor];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
seg.titleTextAttributes = dict;
// 選中的屬性
NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary];
selectedDict[NSFontAttributeName] = [UIFont systemFontOfSize:16];
selectedDict[NSForegroundColorAttributeName] = [UIColor redColor];
seg.selectedTitleTextAttributes = selectedDict;
// 指示條
seg.selectionIndicatorColor = [UIColor purpleColor];
seg.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
seg.selectionIndicatorHeight = 2.0f;
// 垂直線
seg.verticalDividerEnabled = YES;
seg.verticalDividerColor = [UIColor blackColor];
seg.verticalDividerWidth = 2.0f;
// 選擇類型
seg.selectionStyle = HMSegmentedControlSelectionStyleBox;
// 設(shè)置segment的偏移量
seg.segmentEdgeInset = UIEdgeInsetsMake(0, 20, 0, 20);
[seg addTarget:self action:@selector(selectedChangeValue:) forControlEvents:UIControlEventValueChanged];
_seg = seg;
}
- (void)selectedChangeValue:(HMSegmentedControl *)seg {
NSLog(@"select index:%ld",seg.selectedSegmentIndex);
// 設(shè)置偏移量
[_scrollView setContentOffset:CGPointMake(Width * seg.selectedSegmentIndex, 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger selectedIndex = scrollView.contentOffset.x / Width;
// 設(shè)置選中的索引
[_seg setSelectedSegmentIndex:selectedIndex animated:YES];
}
- (UIColor *)randomColor {
CGFloat redValue = arc4random_uniform(255);
CGFloat greenValue = arc4random_uniform(255);
CGFloat blueValue = arc4random_uniform(255);
UIColor *color = [UIColor colorWithRed:redValue/255 green:greenValue/255 blue:blueValue/255 alpha:1];
return color;
}