由于产品的需要,做了一个和网易新闻标题类似的动画效果,现在新闻类的APP都是采用这样的动画效果,来显示更多的内容。先看一下动画效果:



  由于这个动画效果在很多场合都有应用,所以我专门封装了一个控件"FHSegmentControl",它继承于UIView,同学们只要简单的调用就可以了,非常简单。

1.把“FHSegmentControl”文件夹拖入到你的工程中。

2.这样的动画效果都是和流水布局在一起应用的,所以需要设置子视图控制器,由于这次的重点不在这里,所以我的子视图控制器很简单,只有一个UIWebview;

#import <UIKit/UIKit.h>

@interface FHChildViewController : UIViewController

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

@end

3.添加SegmentControl、流水布局

#import "ViewController.h"
#import "FHSegmentControl.h"
#import "FHChildViewController.h" #define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height @interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> @property (nonatomic, strong)FHSegmentControl *segment;
@property (nonatomic, strong) UICollectionView *collectionView; @end @implementation ViewController - (void)viewDidLoad {
[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];
}
} @end

  我封装的这个控件会上传到GitHub,欢迎大家应用,如果有错误,联系我,一定会第一时间更改。

iOS动画案例(2) 仿网易新闻标题动画的更多相关文章

  1. iOS仿网易新闻栏目拖动重排添加删除效果

    仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...

  2. Android Studio精彩案例(一)《ActionBar和 ViewPager版仿网易新闻客户端》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 为了能更好的分享高质量的文章,所以开设了此专栏.文章代码都以Android Studio亲测运行,读者朋友可在后面直接下载源码.该专栏 ...

  3. iOS界面-仿网易新闻左侧抽屉式交互 续(添加新闻内容页和评论页手势)

     本文转载至  http://blog.csdn.net/totogo2010/article/details/8637430       1.介绍 有的博友看了上篇博文iOS界面-仿网易新闻左侧抽屉 ...

  4. 类似掌盟的Tab页 Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签 (转)

    原博客地址  :http://blog.csdn.net/xiaanming/article/details/10766053 本文转载,记录学习用,如有需要,请到原作者网站查看(上面这个网址) 之前 ...

  5. Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻clientTab标签

    之前用JakeWharton的开源框架ActionBarSherlock和ViewPager实现了对网易新闻clientTab标签的功能,ActionBarSherlock是在3.0下面的机器支持Ac ...

  6. Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/10766053 之前用JakeWharton的开源框架ActionBarSherlock ...

  7. Android 开源框架ActionBarSherlock 和 ViewPager 仿网易新闻客户端

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9971721 大家都知道Android的ActionBar是在3.0以上才有的,那么在3 ...

  8. 仿网易新闻app下拉标签选择菜单

    仿网易新闻app下拉标签选择菜单 仿网易新闻app下拉标签选择菜单,长按拖动排序,点击增删标签控件 ##示例  ##EasyTagDragView的使用 在layout布局里添加:  

  9. 仿网易nec首页动画效果

    仿网页nec首页动画效果nec链接:http://nec.netease.com/ 首先,介绍animationanimation检索或设置对象所应用的动画特效.animation由“keyframe ...

随机推荐

  1. 在阿里云ECS(CentOS6.5)上安装jdk

    JDK安装 在安装前先确定服务器上没有安装过JDK 命令: java -version 结果: 查看所有java安装包 命令: yum -y list java* 结果: 选择安装所需要的JDK 命令 ...

  2. MQ-2烟雾传感器启动

    MQ-2气体传感器所使用的气敏材料是在清洁空气中电导率较低的二氧化锡(SnO2).当传感器所处环境中 存在可燃气体时,传感器的电导率随空气中可燃气体浓度的增加而增大.使用简单的电路即可将电导率的 变化 ...

  3. ubuntu12.04+fuerte 下跑通lsd-slam——使用usb摄像头

    上一篇介绍了如何使用数据集跑lsd-slam,这篇介绍如何用一个普通的usb摄像头跑lsd-slam,默认ubuntu12.04,fuerte已经安装好,workspace也已设置,如果没有,请参考上 ...

  4. Zynq和microblaze的区别

    Zynq钩中PS端的外设之后不需要初始化过程,但是如果在microblaze中连接外设之后需要有初始化过程.

  5. EasyUI datagrid 的checkbox设置

    参考url: http://blog.csdn.net/baronyang/article/details/9323463 我的需求: 抓取数据生成的日志,日志中有部分是抓取失败的,需要将失败的发送到 ...

  6. java实现——003二维数组中的查找

    import java.util.Scanner; public class T003 { public static void main(String[] args) { Scanner in = ...

  7. sql2005数据库置疑修复断电崩溃索引损坏 数据库索引错误修复/数据库表损坏/索引损坏/系统表混乱等问题修复

    sql2005数据库置疑修复断电崩溃索引损坏 数据库索引错误修复/数据库表损坏/索引损坏/系统表混乱等问题修复 客 户 名 称 济南某电子商务公司 数 据 类 型 SQL2005数据库 故 障 检 测 ...

  8. UVa 10653 - Bombs! NO they are Mines!!

    题目大意:给你一个二维迷宫,给定入口和出口,找出最短路径. 无权图上的单源最短路,用BFS解决. #include <cstdio> #include <queue> #inc ...

  9. 华硕A450c详细清灰拆机教程

    很久都想写点东西,但又无从下笔. 上次把自己的笔记本清了灰,这次有时间就整理一下,随便作为我的第一次随笔. 准备:笔记本(我的是华硕A450c),拆机工具(螺丝刀等) 温馨提示:要慢点 1,先翻开笔记 ...

  10. UVa 136 - Ugly Numbers

    题目大意:只有素因子2,3,5的数叫做丑数.输出第1500个丑数即可. 这个...好吧,直接输出就是了.自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写 ...