IOS 仿頭條佈局
pch檔案
#ifndef my_pch
#define my_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
//用於適配的巨集
#define SCR_W [UIScreen mainScreen].bounds.size.width // 螢幕寬度
#define SCR_H [UIScreen mainScreen].bounds.size.height // 螢幕高度
#define FIT_X(x) (SCR_W/375.*(x)) // 用於x軸適配
#define FIT_Y(y) (SCR_H/667.*(y)) // 用於y軸適配
#endif /* my_pch */
注:NewsCollectionViewCell 繼承於 UICollectionViewCell
AppDelegate.m
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *vc = [[ViewController alloc]init];
UINavigationController *navVc = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = navVc;
return YES;
}
ViewController.m
#import "ViewController.h"
#import "NewsCollectionViewCell.h"
#define BTN_W FIT_X(80) // 每個標題按鈕的寬度
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>
{
NSArray *_allTitles; //所有的新聞標題
NSArray *_allDatas; //說有的新聞資料
NSArray *_tableDatas; //給每一個網格單元格中的表格檢視載入資料的陣列
}
@property (nonatomic,strong)UIScrollView *titleScrView;//標題滾動檢視
@property (nonatomic,strong)UILabel *titleIndicateLable; //標題下方的指示標籤
@property (nonatomic,strong)UICollectionView *contentsColView; //新聞內容網格檢視
@end
@implementation ViewController
#pragma mark - 懶載入控制元件
//標題滾動檢視
- (UIScrollView *)titleScrView
{
if (!_titleScrView)
{
_titleScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, FIT_Y(64), SCR_W, FIT_Y(44))];
_titleScrView.contentSize = CGSizeMake(BTN_W * _allTitles.count, FIT_Y(44));
_titleScrView.showsHorizontalScrollIndicator = NO;
for (int i = 0 ; i < _allTitles.count; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*BTN_W, 0, BTN_W, FIT_Y(44));
[btn setTitle:_allTitles[i] forState:UIControlStateNormal];
[btn setTitle:_allTitles[i] forState:UIControlStateSelected];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
if (i == 0) {
btn.selected = YES;
}
btn.tag = i+100;
[btn addTarget:self action:@selector(titleBtnHandle:) forControlEvents:UIControlEventTouchUpInside];
[_titleScrView addSubview:btn];
}
[_titleScrView addSubview:self.titleIndicateLable];
}
return _titleScrView;
}
//標題指示標籤
- (UILabel *)titleIndicateLable
{
if (!_titleIndicateLable)
{
_titleIndicateLable = [[UILabel alloc]initWithFrame:CGRectMake(0, FIT_Y(44), BTN_W, FIT_Y(2))];
_titleIndicateLable.backgroundColor = [UIColor redColor];
}
return _titleIndicateLable;
}
//新聞內容檢視
- (UICollectionView *)contentsColView
{
if (!_contentsColView)
{
//例項化網格佈局物件
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
//設定每個單元格大小
flow.itemSize = CGSizeMake(SCR_W, SCR_H-FIT_Y(44));
//設定最小行間距
flow.minimumLineSpacing = 0;
//設定最小列間距
flow.minimumInteritemSpacing = 0;
//設定滾動方向水平滾動
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//例項化網格檢視
_contentsColView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, FIT_Y(110), SCR_W, SCR_H-FIT_Y(44)) collectionViewLayout:flow];
_contentsColView.tag = 444;
_contentsColView.dataSource = self;
_contentsColView.delegate = self;
_contentsColView.pagingEnabled = YES; //分頁滾動
//註冊一個cell
[_contentsColView registerClass:[NewsCollectionViewCell class] forCellWithReuseIdentifier:@"NewsCell"];
}
return _contentsColView;
}
//標題滾動button方法
- (void)titleBtnHandle:(UIButton *)btn
{
//點選按鈕讓collection滾動
[self.contentsColView scrollRectToVisible:CGRectMake((btn.tag-100)*SCR_W, 0, SCR_W, FIT_Y(44)) animated:YES];
}
#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _allTitles.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"NewsCell";
NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (!cell)
{
cell = [[NewsCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H-FIT_Y(44))];
}
cell.newsTable.dataSource = self;
cell.newsTable.delegate = self;
return cell;
}
#pragma mark - UICollectionViewDelegate
//將要出現cell時回撥的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//如果內容網格檢視滾動,執行if中的程式碼
if (scrollView.tag == 444)
{
int index = scrollView.contentOffset.x / SCR_W;
//其他的button變為normal顏色
for (int i = 0; i < _allTitles.count ; i++)
{
UIButton *btn = (UIButton *)[self.titleScrView viewWithTag:i+100];
btn.selected = NO;
if (index == i)
{
btn.selected = YES;
}
}
//移動指示標籤
[UIView animateWithDuration:0.185 animations:^{
self.titleIndicateLable.frame = CGRectMake(index * BTN_W, FIT_Y(42), BTN_W, FIT_Y(2));
}];
//讓標題滾動檢視滾動到指定位置
[self.titleScrView scrollRectToVisible:CGRectMake(BTN_W*index, 0, BTN_W, FIT_Y(44)) animated:YES];
}
}
//即將滾動出現cell時的回撥方法
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",indexPath.row);
//改變網格單元格中表格檢視的內容
_tableDatas = _allDatas[indexPath.row];
NewsCollectionViewCell *newsCell = (NewsCollectionViewCell *)cell;
[newsCell.newsTable reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _tableDatas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = _tableDatas[indexPath.row];
return cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
_allTitles = @[@"頭條",@"新聞",@"體育",@"科技",@"財經",@"軍事",@"娛樂",@"家居",@"健康",@"遊戲"];
_allDatas = @[@[@"頭條1",@"頭條2",@"頭條3"],
@[@"新聞1",@"新聞2",@"新聞3"],
@[@"體育1",@"體育2",@"體育3"],
@[@"科技1",@"科技2",@"科技3"],
@[@"財經1",@"財經2",@"財經3"],
@[@"軍事1",@"軍事2",@"軍事3"],
@[@"娛樂1",@"娛樂2",@"娛樂3"],
@[@"家居1",@"家居2",@"家居3"],
@[@"健康1"],
@[@"Game1",@"Game2"]
];
_tableDatas = _allDatas[0];
UIView *v = [[UIView alloc]init];
[self.view addSubview: v];
[self.view addSubview:self.titleScrView];
[self.view addSubview:self.contentsColView];
}
- (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
NewsCollectionViewCell.h
@interface NewsCollectionViewCell : UICollectionViewCell
@property (nonatomic,strong)UITableView *newsTable;
@end
NewsCollectionViewCell.m
#import "NewsCollectionViewCell.h"
@implementation NewsCollectionViewCell
#pragma Mark - 重寫
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.newsTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H-FIT_Y(44)) style:UITableViewStylePlain];
[self.contentView addSubview:self.newsTable];
}
return self;
}
@end
相關文章
- iOS混合開發庫(GICXMLLayout)佈局案例分析(1)今日頭條案例iOSXML
- thinkphp仿今日頭條原始碼PHP原始碼
- 騰訊“吃雞遊戲”突圍 頭條系佈局“偷塔”遊戲
- iOS UIWebview仿微信進度條iOSUIWebView
- iOS線性佈局iOS
- IOS 介面佈局 AutoLayoutiOS
- Vue實戰篇(Vue仿今日頭條)Vue
- React實戰篇(React仿今日頭條)React
- 首頁頭部提示佈局
- Swift iOS : 使用Cartography佈局SwiftiOS
- [譯] 揭祕 iOS 佈局iOS
- iOS 全屏佈局筆記iOS筆記
- Android——仿淘寶頭條垂直滾動廣告Android
- 仿今日頭條按鈕loading效果
- iOS開發之多表檢視滑動切換示例(仿”頭條”客戶端)iOS客戶端
- iOS UICollectionViewCell的一種佈局iOSUIView
- Flutter仿頭條頂部tab切換實現Flutter
- 仿天貓App實現商品列表佈局切換效果APP
- iOS自動佈局——Masonry詳解iOS
- iOS UICollectionView 橫向分頁佈局iOSUIView
- iOS Flexbox 佈局優化iOSFlex優化
- iOS 上的 FlexBox 佈局iOSFlex
- iOS導航欄佈局相關iOS
- CSS頭部內容和底部佈局效果CSS
- vue全家桶仿某魚部分佈局以及功能實現Vue
- iOS開發之多表檢視滑動切換示例(仿”頭條”客戶端)—優化篇iOS客戶端優化
- iOS 常用佈局方式之ConstraintiOSAI
- 【iOS】關於 UICollectionView 的自定義佈局iOSUIView
- iOS 自動佈局框架 – Masonry 詳解iOS框架
- iOS自動佈局框架 - Masonry詳解iOS框架
- CSS佈局 --- 居中佈局CSS
- css佈局-float佈局CSS
- 【iOS】今日頭條選單選項篇iOS
- 居中佈局、三欄佈局
- qt 佈局---表單佈局QT
- java:佈局方法(流佈局)Java
- flex佈局(彈性佈局)Flex
- 佈局技巧:合併佈局