成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iOS開發(fā)之多表視圖滑動切換示例(仿"頭條"客戶端)

移動開發(fā)
廢話少說,先介紹一下功能點,下圖是整個Demo的功能點,最上面左邊的TabBarButtonItem是用來減少條目的,比如下圖有三個按鈕,點擊減號會減少一個條目。右邊的為增加一個條目。點擊相應的按鈕是切換到對應的表視圖上,下方紅色的是滑動的指示器,同時支持手勢滑動。運行具體效果如下圖所示。

好長時間沒為大家?guī)韎OS開發(fā)干貨的東西了,今天給大家分享一個頭條新聞客戶端各個類別進行切換的一個示例。在Demo中對所需的組件進行的簡單封裝,在封裝的組件中使用的是純代碼的形式,如果想要在項目中進行使用,稍微進行修改即可。

廢話少說,先介紹一下功能點,下圖是整個Demo的功能點,最上面左邊的TabBarButtonItem是用來減少條目的,比如下圖有三個按鈕,點擊減號會減少一個條目。右邊的為增加一個條目。點擊相應的按鈕是切換到對應的表視圖上,下方紅色的是滑動的指示器,同時支持手勢滑動。運行具體效果如下圖所示。

blob.png

一:實現(xiàn)方案

最上方是一個View, View上面實例化了一些按鈕,平分屏幕的寬度,下方是一個ScrollView, ScrollView上面放了一些表視圖,點擊不同的Button, 滑動到對應的表示圖上。除了點擊按鈕,還可以進行滑動切換,切換時,紅色的指示器也會隨之滑動。

主要的技術點就是通過ScrollView的回調,通過事件的響應來改變ScrollView的ContentOffset的值。在回調中根據(jù)ContentOffset的值來計算紅色指示器的偏移量。

二:核心代碼

1.組件中的主要屬性

把上面整個視圖進行了封裝,命名為SlideTabBarView,下面的代碼是主要屬性:

 
  1. @interface SlideTabBarView()///@brife 整個視圖的大小 
  2. @property (assign) CGRect mViewFrame; 
  3. ///@brife 下方的ScrollView 
  4. @property (strong, nonatomic) UIScrollView *scrollView; 
  5. ///@brife 上方的按鈕數(shù)組 
  6. @property (strong, nonatomic) NSMutableArray *topViews; 
  7. ///@brife 下方的表格數(shù)組 
  8. @property (strong, nonatomic) NSMutableArray *scrollTableViews; 
  9. ///@brife TableViews的數(shù)據(jù)源 
  10. @property (strong, nonatomic) NSMutableArray *dataSource; 
  11. ///@brife 當前選中頁數(shù) 
  12. @property (assign) NSInteger currentPage; 
  13. ///@brife 下面滑動的View 
  14. @property (strong, nonatomic) UIView *slideView; 
  15. @end 

2.初始化方法如下,在調用初始化方法時需要傳入SlideTabBarView的frame和選項卡的個數(shù),初始化函數(shù)會調用一系列的初始化方法對組件進行初始化,代碼如下:

 
  1. -(instancetype)initWithFrame:(CGRect)frame WithCount: (NSInteger) count{ 
  2.     self = [super initWithFrame:frame]; 
  3.       
  4.     if (self) { 
  5.         _mViewFrame = frame; 
  6.         _tabCount = count; 
  7.         _topViews = [[NSMutableArray alloc] init]; 
  8.         _scrollTableViews = [[NSMutableArray alloc] init]; 
  9.           
  10.         [self initDataSource]; 
  11.           
  12.         [self initScrollView]; 
  13.           
  14.         [self initTopTabs]; 
  15.           
  16.         [self initDownTables]; 
  17.           
  18.         [self initDataSource]; 
  19.           
  20.         [self initSlideView]; 
  21.           
  22.     } 
  23.       
  24.     return self; 

3.initDataSource方法主要負責模擬生成下方TableView要顯示的數(shù)據(jù)。代碼如下:

 
  1. #pragma mark -- 初始化表格的數(shù)據(jù)源 
  2. -(void) initDataSource{ 
  3.     _dataSource = [[NSMutableArray alloc] initWithCapacity:_tabCount]; 
  4.       
  5.     for (int i = 1; i <= _tabCount; i ++) { 
  6.           
  7.         NSMutableArray *tempArray  = [[NSMutableArray alloc] initWithCapacity:20]; 
  8.           
  9.         for (int j = 1; j <= 20; j ++) { 
  10.               
  11.             NSString *tempStr = [NSString stringWithFormat:@"我是第%d個TableView的第%d條數(shù)據(jù)。", i, j]; 
  12.             [tempArray addObject:tempStr]; 
  13.         } 
  14.           
  15.         [_dataSource addObject:tempArray]; 
  16.     } 
  17.  
  18.               

4.紅色滑動指示器的初始化代碼如下所示:

  1. #pragma mark -- 初始化滑動的指示View 
  2. -(void) initSlideView{ 
  3.      CGFloat width = _mViewFrame.size.width / _tabCount; 
  4.     _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, TOPHEIGHT - 5, width, 5)]; 
  5.     [_slideView setBackgroundColor:[UIColor redColor]]; 
  6.     [self addSubview:_slideView]; 

5.ScrollView的初始化代碼如下, 指定ScrollView的大小位置以及背景顏色,并且設置分頁可用并添加代理。

  1. #pragma mark -- 實例化ScrollView 
  2. -(void) initScrollView{ 
  3.     _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0
  4. _mViewFrame.origin.y, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT)]; 
  5.     _scrollView.contentSize = CGSizeMake(_mViewFrame.size.width * _tabCount,
  6.  _mViewFrame.size.height - 60); 
  7.     _scrollView.backgroundColor = [UIColor grayColor]; 
  8.       
  9.     _scrollView.pagingEnabled = YES; 
  10.       
  11.     _scrollView.delegate = self; 
  12.     [self addSubview:_scrollView]; 

6.添加上方的按鈕,根據(jù)傳入的個數(shù)來實例化多個按鈕。

  1. #pragma mark -- 實例化頂部的tab 
  2. -(void) initTopTabs{ 
  3.     CGFloat width = _mViewFrame.size.width / _tabCount; 
  4.       
  5.     for (int i = 0; i < _tabCount; i ++) { 
  6.           
  7.         UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TOPHEIGHT)]; 
  8.           
  9.         view.backgroundColor = [UIColor lightGrayColor]; 
  10.           
  11.         if (i % 2) { 
  12.             view.backgroundColor = [UIColor grayColor]; 
  13.         } 
  14.           
  15.         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(00, width, TOPHEIGHT)]; 
  16.         button.tag = i; 
  17.         [button setTitle:[NSString stringWithFormat:@"按鈕%d", i+1] forState:UIControlStateNormal]; 
  18.         [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside]; 
  19.         [view addSubview:button]; 
  20.           
  21.           
  22.         [_topViews addObject:view]; 
  23.         [self addSubview:view]; 
  24.     } 

7.點擊按鈕觸發(fā)的方法如下:

 
  1. #pragma mark --點擊頂部的按鈕所觸發(fā)的方法 
  2. -(void) tabButton: (id) sender{ 
  3.     UIButton *button = sender; 
  4.     [_scrollView setContentOffset:CGPointMake(button.tag * _mViewFrame.size.width, 0) animated:YES]; 

8.初始化下方的多個表視圖:實例化表視圖,并指定委托回調。

  1. #pragma mark --初始化下方的TableViews 
  2. -(void) initDownTables{ 
  3.       
  4.     for (int i = 0; i < _tabCount; i ++) { 
  5.           
  6.         UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * _mViewFrame.size.width, 0, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT)]; 
  7.         tableView.delegate = self; 
  8.         tableView.dataSource = self; 
  9.           
  10.         [_scrollTableViews addObject:tableView]; 
  11.         [_scrollView addSubview:tableView]; 
  12.     } 

9.ScrollView的回調方法如下,下面***一個代理方法是根據(jù)ScrollView的偏移量來計算紅色指示器的偏移量,第二個是滑動到哪個tableView,然后進行哪個TableView的數(shù)據(jù)加載。

  1. #pragma mark -- scrollView的代理方法 
  2. -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ 
  3.     [self scrollViewDidEndDecelerating:scrollView]; 
  4. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
  5.     _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width; 
  6.       
  7.     UITableView *currentTable = _scrollTableViews[_currentPage]; 
  8.     [currentTable reloadData]; 
  9.       
  10. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
  11.     if ([_scrollView isEqual:scrollView]) { 
  12.         CGRect frame = _slideView.frame; 
  13.         frame.origin.x = scrollView.contentOffset.x/_tabCount; 
  14.         _slideView.frame = frame; 
  15.     } 

10.TableView的代理方法如下,數(shù)據(jù)源就是我們剛才做的假數(shù)據(jù),Cell是由Xib實現(xiàn)的,使用的時候注冊一下就可用了。

  1. #pragma mark -- talbeView的代理方法 
  2. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
  3.     return 1
  4. -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
  5.     NSMutableArray *tempArray = _dataSource[_currentPage]; 
  6.     return tempArray.count; 
  7. -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
  8.     return 60
  9. -(UITableViewCell *)tableView:tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
  10.       
  11.     BOOL nibsRegistered=NO; 
  12.     if (!nibsRegistered) { 
  13.         UINib *nib=[UINib nibWithNibName:@"SlideBarCell" bundle:nil]; 
  14.         [tableView registerNib:nib forCellReuseIdentifier:@"SlideBarCell"]; 
  15.         nibsRegistered=YES; 
  16.     } 
  17.       
  18.       
  19.     SlideBarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SlideBarCell"]; 
  20.     if ([tableView isEqual:_scrollTableViews[_currentPage]]) { 
  21.          cell.tipTitle.text = _dataSource[_currentPage][indexPath.row]; 
  22.     } 
  23.      
  24.     return cell; 

Demo在GitHub上的分享地址:https://github.com/lizelu/SliderTabBar

責任編輯:倪明 來源: 青玉伏案的博客
相關推薦

2015-07-06 10:48:56

iOS開發(fā)技巧

2015-12-09 11:22:24

高仿今日頭條android源碼

2013-07-22 14:29:35

iOS開發(fā)ASIHTTPRequ

2014-08-11 16:35:35

KafkaJava客戶端

2011-06-15 17:28:23

Qt 多視圖 架構

2015-01-09 11:49:26

Android源碼下載

2015-03-30 14:24:06

網(wǎng)易布局

2009-12-25 15:12:01

WPF平臺

2017-09-12 17:05:02

AndroidLoading客戶端

2011-08-17 10:10:59

2021-09-22 15:46:29

虛擬桌面瘦客戶端胖客戶端

2014-09-02 10:55:25

iOS開發(fā)視圖切換

2013-07-04 10:01:04

2011-10-26 13:17:05

2011-03-02 14:36:24

Filezilla客戶端

2010-12-21 11:03:15

獲取客戶端證書

2011-03-24 13:00:31

配置nagios客戶端

2010-05-31 10:11:32

瘦客戶端

2022-11-29 17:08:03

開發(fā)Web客戶端

2011-03-21 14:53:36

Nagios監(jiān)控Linux
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美一级免费观看 | 免费国产黄网站在线观看视频 | 久久久国产一区 | 日韩欧美精品在线 | 国产乱码精品一品二品 | 成人字幕网zmw| 九九激情视频 | 男人的天堂在线视频 | 日韩成人免费视频 | 伊人网站| 国产精品自拍啪啪 | 日本特黄a级高清免费大片 成年人黄色小视频 | 久久6视频 | 精国产品一区二区三区 | 自拍偷拍小视频 | 日韩午夜网站 | 99久久精品国产麻豆演员表 | 香蕉91| 亚洲国产精品人人爽夜夜爽 | 久久国产日韩 | 日本免费视频在线观看 | 欧美黄在线观看 | 农村真人裸体丰满少妇毛片 | 国产98在线 | 免费, | 天天人人精品 | 我想看一级黄色毛片 | 国产 日韩 欧美 在线 | 国产一区二区精品在线观看 | 99精品99| 国产精品久久久久一区二区三区 | 亚洲欧美日韩国产综合 | 欧美精品tv | 国产精品久久久久久一区二区三区 | h视频在线免费 | 日日摸夜夜添夜夜添特色大片 | 蜜臀av日日欢夜夜爽一区 | 久久99精品久久久久蜜桃tv | 国产一区黄色 | 精品1区 | 99色视频 | 国产在线一区二区 |