ios 頂部tab
主要使用了scrolview做的
//
// HJPagerViewController.h
// MusicLove
//
// Created by niuxinghua on 15/1/29.
// Copyright (c) 2015年 Hjojo. All rights reserved.
//
#import
@interface HJPagerViewController :UIViewController
@property(nonatomic,strong)UIScrollView* scrolview;
@property(nonatomic,strong)UIButton* btn1;
@property(nonatomic,strong)UIButton* btn2;
@property(nonatomic,strong)UIButton* btn3;
@property(nonatomic,strong)UIImageView* imageLine;
@property(nonatomic,strong)UITableView* tableView;
@end
/
//
// HJPagerViewController.m
// MusicLove
//
// Created by niuxinghua on 15/1/29.
// Copyright (c) 2015年 Hjojo. All rights reserved.
//
#import "HJPagerViewController.h"
#import "MJRefresh.h"
NSString *const MJTableViewCellIdentifier =@"Cell1";
/**
* 隨機資料
*/
#define MJRandomData [NSString stringWithFormat:@"隨機資料---%d", arc4random_uniform(1000000)]
@interface HJPagerViewController ()
@property (strong,nonatomic) NSMutableArray *fakeData;
@end
@implementation HJPagerViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.tabBarController.tabBar.hidden=YES;
self.fakeData=[[NSMutableArrayalloc]init];
CGFloat mainWith=[UIScreenmainScreen].bounds.size.width;
CGFloat mainHeight=[UIScreenmainScreen].bounds.size.height;
_btn1=[[UIButtonalloc]initWithFrame:CGRectMake(0,66, mainWith/3,40)];
_btn1.backgroundColor=[UIColorcolorWithRed:242/255.0green:87/255.0blue:96/255.0alpha:1.0f];
// [_btn1 setBackgroundColor:[UIColor blackColor]];
[_btn1setTitle:@"距離"forState:UIControlStateNormal];
_btn2=[[UIButtonalloc]initWithFrame:CGRectMake(mainWith/3,66, mainWith/3,40)];
_btn2.backgroundColor=[UIColorcolorWithRed:242/255.0green:87/255.0blue:96/255.0alpha:1.0f];
[_btn2setTitle:@"價格"forState:UIControlStateNormal];
_btn3=[[UIButtonalloc]initWithFrame:CGRectMake(mainWith/3*2,66, mainWith/3,40)];
_btn3.backgroundColor=[UIColorcolorWithRed:242/255.0green:87/255.0blue:96/255.0alpha:1.0f];
[_btn3setTitle:@"篩選"forState:UIControlStateNormal];
_imageLine=[[UIImageViewalloc]initWithFrame:CGRectMake(0,104, mainWith/3,5)];
_imageLine.center=CGPointMake(mainWith/6,107.5);
[_imageLinesetBackgroundColor:[UIColorgreenColor]];
_scrolview=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,111, mainWith, mainHeight-111)];
_scrolview.delegate=self;
_scrolview.pagingEnabled=YES;
_scrolview.showsHorizontalScrollIndicator=NO;
_scrolview.bounces=NO;
_scrolview.contentSize=CGSizeMake(mainWith*3, mainHeight-111);
self.tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0, mainWith, mainHeight-111)];
// 2.整合重新整理控制項
[selfsetupRefresh];
[self.tableViewregisterClass:[UITableViewCellclass] forCellReuseIdentifier:MJTableViewCellIdentifier];
self.tableView.delegate=self;
self.tableView.dataSource=self;
UIImageView* image1=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0, mainWith, mainHeight-111)];
UIImageView* image2=[[UIImageViewalloc]initWithFrame:CGRectMake(mainWith,0, mainWith, mainHeight-111)];
UIImageView* image3=[[UIImageViewalloc]initWithFrame:CGRectMake(mainWith*2,0, mainWith, mainHeight-111)];
image1.backgroundColor=[UIColorredColor];
image2.backgroundColor=[UIColorblackColor];
image3.backgroundColor=[UIColorgreenColor];
[_scrolviewaddSubview:self.tableView];
[_scrolviewaddSubview:image2];
[_scrolviewaddSubview:image3];
[self.viewaddSubview:_btn1];
[self.viewaddSubview:_btn2];
[self.viewaddSubview:_btn3];
[self.viewaddSubview:_scrolview];
[self.viewaddSubview:_imageLine];
[_btn1setTag:101];
[_btn2setTag:102];
[_btn3setTag:103];
[_btn1addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];
[_btn2addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];
[_btn3addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];
}
-(void)btnClicked:(UIButton*)sender{
int tag=[sendertag];
if (tag==101) {
[UIViewanimateWithDuration:0.5fanimations:^{
_imageLine.center=CGPointMake(self.view.bounds.size.width/6,_imageLine.center.y);
_scrolview.contentOffset=CGPointMake(0,0);
}];
}
elseif (tag==102){
[UIViewanimateWithDuration:0.5fanimations:^{
_imageLine.center=CGPointMake(self.view.bounds.size.width/2,_imageLine.center.y);
_scrolview.contentOffset=CGPointMake(self.view.bounds.size.width,0);
}];
}
else{
[UIView animateWithDuration:0.5f animations:^{
_imageLine.center=CGPointMake(self.view.bounds.size.width/6*5, _imageLine.center.y);
_scrolview.contentOffset=CGPointMake(self.view.bounds.size.width*2,0);
}];
}
}
-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
_imageLine.center=CGPointMake(scrollView.contentOffset.x/3+self.view.bounds.size.width/6, _imageLine.center.y);
}
/**
* 整合重新整理控制項
*/
- (void)setupRefresh
{
// 1.下拉重新整理(進入重新整理狀態就會調用self的headerRereshing)
// [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
// dateKey用於儲存重新整理時間,可以保證不同介面擁有不同的重新整理時間
[self.tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
[self.tableView headerBeginRefreshing];
// 2.上拉載入更多(進入重新整理狀態就會調用self的footerRereshing)
[self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
// 設定文字(也可以不設定,預設的文字在MJRefreshConst中修改)
self.tableView.headerPullToRefreshText =@"下拉可以重新整理了";
self.tableView.headerReleaseToRefreshText =@"鬆開馬上重新整理了";
self.tableView.headerRefreshingText =@"正在重新整理中。。。";
self.tableView.footerPullToRefreshText =@"上拉可以載入更多資料了";
self.tableView.footerReleaseToRefreshText =@"鬆開馬上載入更多資料了";
self.tableView.footerRefreshingText =@"正在載入中。。。";
}
#pragma mark 開始進入重新整理狀態
- (void)headerRereshing
{
// 1.添加假資料
for (int i =0; i<5; i++) {
[self.fakeData insertObject:MJRandomData atIndex:0];
}
// 2.類比2秒後重新整理表格UI(真實開發中,可以移除這段gcd代碼)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 重新整理表格
[self.tableView reloadData];
// (最好在重新整理表格後調用)調用endRefreshing可以結束重新整理狀態
[self.tableView headerEndRefreshing];
});
}
- (void)footerRereshing
{
// 1.添加假資料
for (int i =0; i<5; i++) {
[self.fakeData addObject:MJRandomData];
}
// 2.類比2秒後重新整理表格UI(真實開發中,可以移除這段gcd代碼)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 重新整理表格
[self.tableView reloadData];
// (最好在重新整理表格後調用)調用endRefreshing可以結束重新整理狀態
[self.tableView footerEndRefreshing];
});
}
#pragma mark - Table view data source
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
// return 5;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnself.fakeData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MJTableViewCellIdentifier forIndexPath:indexPath];
cell.textLabel.text = self.fakeData[indexPath.row];
NSLog(@"%@",self.fakeData[indexPath.row]);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end