// 在AppDelegate中添加viewController头文件
#import "ViewController.h"
// 在didFinishLaunchingWithOptions中创建导航控制器
_window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
// 为了方便,定义两个宏,视图宽,视图高
#define SWidth self.view.frame.size.width
#define SHeight self.view.frame.size.height
// 在@interface ViewController () 中定义一下属性
{
UIScrollView *_scrollView;
UIButton *_lastBtn;
UIView *_barView;
UITableView *_table;
NSArray *_dataArr;
NSArray *_contentArr;
}
// 在viewDidLoad中创建控件
_dataArr = @[@"111",@"222",@"333",@"444",@"555",@"666",@"+"];
_contentArr = @[@"添加",@"删除",@"关闭"];
// 设置标题
self.title = @"Demo";
self.edgesForExtendedLayout = UIRectEdgeNone;
// 创建滚动视图
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, SHeight-40)];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(SWidth*(_dataArr.count-1), SHeight-40);
_scrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_scrollView];
for (int i = 0; i<_dataArr.count-1; i++) {
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(SWidth*i, 0, SWidth, _scrollView.frame.size.height)];
v.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0)green:((float)arc4random_uniform(256) / 255.0)blue:((float)arc4random_uniform(256) / 255.0)alpha:1.0];
[_scrollView addSubview:v];
}
// 创建自制bar
_barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SWidth, 40)];
_barView.backgroundColor = [UIColor whiteColor];
for (int i=0; i<_dataArr.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:_dataArr[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
btn.frame = CGRectMake(SWidth/_dataArr.count*i, 0, SWidth/_dataArr.count, 40);
btn.tag = 100+i;
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
if (i==0) {
btn.selected = YES;
btn.backgroundColor = [UIColor orangeColor];
_lastBtn = btn;
}
btn.layer.cornerRadius = 5;
btn.layer.masksToBounds = YES;
[_barView addSubview:btn];
}
[self.view addSubview:_barView];
// 创建表格
_table = [[UITableView alloc]initWithFrame:CGRectMake(SWidth-10, 41, 0.1, 0.1)];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
// 按钮点击方式
-(void)click:(UIButton *)sender{
if (sender.tag != 100+_dataArr.count-1) {
_lastBtn.selected = NO;
_lastBtn.backgroundColor = [UIColor whiteColor];
sender.backgroundColor = [UIColor orangeColor];
_lastBtn = sender;
if (sender.selected == YES) {
sender.selected = NO;
}
else if (sender.selected == NO){
sender.selected = YES;
}
_scrollView.contentOffset = CGPointMake(SWidth*(sender.tag-100), 0);
}
else{
if (sender.selected == YES) {
sender.selected = NO;
_table.hidden = YES;
sender.backgroundColor = [UIColor whiteColor];
_table.frame = CGRectMake(SWidth-10, 41, 0.1, 0.1);
}
else if (sender.selected == NO){
sender.selected = YES;
_table.hidden = NO;
sender.backgroundColor = [UIColor orangeColor];
[UIView animateWithDuration:0.3 animations:^{
_table.frame = CGRectMake(SWidth*2/3, 41, SWidth/3-10, 128);
}];
}
}
}
#pragma mark scrollView的代理
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat n = scrollView.contentOffset.x/SWidth;
UIButton *btn = [_barView viewWithTag:n+100];
[self click:btn];
}
#pragma mark tableView的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
cell.textLabel.text = _contentArr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 显示提示框
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:_contentArr[indexPath.row] message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
@end
网友评论