在做项目时,遇到两次这样的界面:上部是按钮栏,按钮下面有随意滑动的指示线,下面很多界面可以左右滑动来切换,线条也会随着界面滑动而滑动。
于是我把之前做的优化了一下,变得更容易添加按钮和界面
大体思路是将一个个tableView添加到scrollView中,扩大scrollView的滚动范围,根据scrollView的contentOffset属性判断滚到哪个视图,从而改变按钮状态(selected还是normal)和指示线的位置。
直接上代码:
ViewController.h
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
#import <UIKit/UIKit.h>
#import "TitleView.h"
@interface ViewController : UIViewController
//按钮栏
@property(nonatomic,strong)TitleView *titleView;
@property(nonatomic,strong)UIScrollView *scrollView;
//添加多少个tableView
- (void)addTableViewWithNumber:(NSInteger)number;
//按钮点击事件
- (void)btnClick:(UIButton *)btn;
//添加指示线
- (void)addLine;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
//目前被选中的按钮
@property(nonatomic,strong)UIButton *selectedBtn;
//指示线
@property(nonatomic,strong)UIVi