美文网首页
模仿网易

模仿网易

作者: 法库德 | 来源:发表于2017-10-26 10:30 被阅读0次

#import "ViewController.h"

#import "FHSegmentControl.h"

#import "FHChildViewController.h"

#define ScreenWidth  [UIScreen mainScreen].bounds.size.width

#define ScreenHeight  [UIScreen mainScreen].bounds.size.height

<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)FHSegmentControl *segment;

@property (nonatomic, strong) UICollectionView *collectionView;

[super viewDidLoad];

// 添加Segment

[self setupSegmentControl];

// 添加流水布局

[self setupBottomeView];

// 添加子视图

[self addChildVC];

// 添加Segment

- (void)setupSegmentControl {

// 自定义导航

UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];

navView.backgroundColor =  [UIColor colorWithRed:95/255.0 green:171/255.0 blue:128/255.0 alpha:1.0f];

[self.view addSubview:navView];

// 添加_segment

_segment = [[FHSegmentControl alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];

// 设置标题 不需要设置标题长度,会根据标题字数自动生成,标题字号17

_segment.titles = @[@"百度",@"搜狐",@"新浪",@"雅虎",@"网易",@"腾讯",@"凤凰网",@"百度",@"百度",@"百度"];

// 标题颜色

_segment.titleColor = [UIColor redColor];

// 选中标题颜色

_segment.selectedTitleColor = [UIColor blackColor];

__weak typeof(self) weakSelf = self;

// 回调  index 就是你点击的那个标题所在数组中的索引

_segment.SegmentControlDidSelectedIndexBlock = ^(NSInteger index) {

// 与流水布局联动

CGFloat offsetX = index * ScreenWidth;

weakSelf.collectionView.contentOffset = CGPointMake(offsetX, 0);

};

[navView addSubview:_segment];

}

// 添加流水布局

- (void)setupBottomeView {

self.automaticallyAdjustsScrollViewInsets = NO;

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

flowLayout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight - 64);

flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

flowLayout.minimumInteritemSpacing = 0.0f;

flowLayout.minimumLineSpacing = 0.0f;

_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) collectionViewLayout:flowLayout];

_collectionView.backgroundColor = [UIColor whiteColor];

_collectionView.pagingEnabled = YES;

_collectionView.showsHorizontalScrollIndicator = YES;

_collectionView.bounces= NO;

_collectionView.dataSource = self;

_collectionView.delegate = self;

[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ID"];

[self.view addSubview:_collectionView];

}

#pragma mark - UICollectionViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

NSInteger index = scrollView.contentOffset.x / ScreenWidth;

// 与标题联动

[_segment setSelectedIndex:index];

}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return self.childViewControllers.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

FHChildViewController *childVC = self.childViewControllers[indexPath.row];

childVC.view.frame = cell.contentView.bounds;

[cell.contentView addSubview:childVC.view];

return cell;

}

// 添加子视图

- (void)addChildVC {

NSArray *urlArr = @[@"http://www.baidu.com",@"http://www.sohu.com",@"http://www.sina.com.cn",@"http://www.yahoo.com",@"http://www.163.com",@"http://www.qq.com",@"http://www.ifeng.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com"];

for (int i = 0; i < urlArr.count; i++) {

FHChildViewController *childVC = [FHChildViewController new];

childVC.view.backgroundColor = [UIColor whiteColor];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlArr[i]]];

[childVC.webView loadRequest:request];

[self addChildViewController:childVC];

}

}

创建页面FHChildViewController继承UIViewController

.h

@property (weak, nonatomic) IBOutlet UIWebView *webView;

相关文章

  • 模仿网易

    #import "ViewController.h" #import "FHSegmentControl.h" #...

  • PPT模仿网易云banner制作过程解析

    今日学习:PPT作品模仿,模仿作品来自于网易云音乐首页banner 制作过程: 1 PPT尺寸为16:9,原打算调...

  • 《易读》一个模仿网易云音乐UI,Rxjava+Retrofit+

    易读一款模仿网易云音乐UI,使用Retrofit + RxJava + dagger2 + MVP的阅读类开源项目...

  • Android - 仿网易云音乐歌单详情页

    前段时间模仿网易云音乐UI使用DataBinding做了一个App:CloudReader,今天把其中的类似歌单详...

  • iOS 一些第三方资料

    一:源代码实例一个针对iOS模块化开发的解决方案 快速搭建项目源代码 模仿网易新闻做的精仿网易新闻 支付宝高仿版 ...

  • iOS比较常用的第三方及实例

    一:源代码实例 一个针对iOS模块化开发的解决方案 快速搭建项目源代码 模仿网易新闻做的精仿网易新闻 支付宝高仿版...

  • IOS

    一:源代码实例 一个针对iOS模块化开发的解决方案 快速搭建项目源代码 模仿网易新闻做的精仿网易新闻 支付宝高仿版...

  • 2016年优秀项目

    一:源代码实例一个针对iOS模块化开发的解决方案 快速搭建项目源代码 模仿网易新闻做的精仿网易新闻 支付宝高仿版 ...

  • vue模仿网易云客户端

    和小伙伴使用vue全家桶写了一个网易云客户端,目前实现了部分功能 代码地址: https://github.com...

  • iOS scrollView与tableView嵌套(模仿网易云

    本文主要是模仿网易云课堂课程详情页面实现,需求: tableView在默认高度向下滑动时,header固定 tab...

网友评论

      本文标题:模仿网易

      本文链接:https://www.haomeiwen.com/subject/sdvhpxtx.html