ios 頂部tab

來源:互聯網
上載者:User

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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.