分段滾動頁面是一個比較常見的需求,我把我的實現方式封裝了一下,方便以後重複使用。
實現效果:
GitHub地址:https://github.com/runThor/HTSegmentedScrollView
使用方式非常簡單,添加segment的title以及添加滾動的view即可,代碼如下:
#import "HTSegmentedScrollView.h"
- (void)viewDidLoad {
[super viewDidLoad];
HTSegmentedScrollView *segView = [[HTSegmentedScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[self.view addSubview:segView];
[segView addSegmentedItems:@[@"紅色", @"黃色", @"藍色"]]; // 添加title
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
UIView *yellowView = [[UIView alloc] init];
yellowView.backgroundColor = [UIColor yellowColor];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[segView addScrollViews:@[redView, yellowView, blueView]]; // 添加滾動的view
}