根据滑动显隐状态栏的iOS实现
之前很多兄弟问如何实现类似于淘宝客户端搜索列表那种动态显隐的效果,这几天刚好有时间,就实现了几个例子搞一下,其实原理很简单,也参考了github上一位兄弟的实现.不多说,上代码
@interface D1ScrollingNaviBarViewController : UIViewController
//滑动隐藏,显示导航栏
-(void)followRollingScrollView:(UIView *)scrollView;
//用来处理导航栏下还有其他内容也需要跟随导航栏一起隐藏,显示的情况,类似于淘宝客户端的搜索
-(void)followRollingScrollView:(UIView *)scrollView secondAreaHeight:(CGFloat)secondAreaHeight;
@end
#define NAVI_BAR_HEIGHT 44.0
@interface D1ScrollingNaviBarViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) UIView *scrollView;
@property (retain, nonatomic)UIPanGestureRecognizer *panGesture;
@property (retain, nonatomic)UIView *overLay;
@property (assign, nonatomic)BOOL isHidden;
@property(nonatomic,assign)CGFloat secondAreaHeight;
@end @implementation D1ScrollingNaviBarViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.secondAreaHeight = 0.0;
}
return self;
} //设置跟随滚动的滑动试图
-(void)followRollingScrollView:(UIView *)scrollView
{
self.scrollView = scrollView; self.panGesture = [[UIPanGestureRecognizer alloc] init];
self.panGesture.delegate=self;
self.panGesture.minimumNumberOfTouches = 1;
[self.panGesture addTarget:self action:@selector(handlePanGesture:)];
[self.scrollView addGestureRecognizer:self.panGesture]; CGRect rect = self.navigationController.navigationBar.bounds;
self.overLay = [[UIView alloc] initWithFrame:rect];
self.overLay.alpha=0;
self.overLay.backgroundColor=self.navigationController.navigationBar.barTintColor;
[self.navigationController.navigationBar addSubview:self.overLay];
[self.navigationController.navigationBar bringSubviewToFront:self.overLay]; }
-(void)followRollingScrollView:(UIView *)scrollView secondAreaHeight:(CGFloat)secondAreaHeight
{
self.secondAreaHeight = secondAreaHeight;
[self followRollingScrollView:scrollView];
}
#pragma mark - 兼容其他手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
} #pragma mark - 手势调用函数
-(void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
{
CGPoint translation = [panGesture translationInView:[self.scrollView superview]];
//显示
if (translation.y >= 5) {
if (self.isHidden) { self.overLay.alpha=0;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
CGRect scrollViewFrame=self.scrollView.frame; navBarFrame.origin.y = 20;
scrollViewFrame.origin.y += self.secondAreaHeight+NAVI_BAR_HEIGHT;
scrollViewFrame.size.height -= self.secondAreaHeight+NAVI_BAR_HEIGHT; [UIView animateWithDuration:0.5 animations:^{
self.navigationController.navigationBar.frame = navBarFrame;
self.scrollView.frame=scrollViewFrame;
}];
self.isHidden= NO;
}
}
//隐藏
if (translation.y <= -20) {
if (!self.isHidden) {
CGRect frame =self.navigationController.navigationBar.frame;
CGRect scrollViewFrame=self.scrollView.frame;
frame.origin.y = -24;
scrollViewFrame.origin.y -= self.secondAreaHeight+NAVI_BAR_HEIGHT;
scrollViewFrame.size.height += self.secondAreaHeight+NAVI_BAR_HEIGHT; [UIView animateWithDuration:0.5 animations:^{
self.navigationController.navigationBar.frame = frame;
self.scrollView.frame=scrollViewFrame;
} completion:^(BOOL finished) {
self.overLay.alpha=1;
}];
self.isHidden=YES;
}
} } -(void)viewDidAppear:(BOOL)animated{
[self.navigationController.navigationBar bringSubviewToFront:self.overLay];
}
@end 要使用的类继承这个基类
//设置导航栏的背景色的两种方式
self.navigationController.navigationBar.barTintColor=[UIColor greenColor];//使用颜色 self.navigationController.navigationBar.barTintColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:@"nav_bg_h64"]];//使用图片 //44高度会被自动拉伸,所以需要注意图片的实际效果
self.navigationController.navigationBar.barTintColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:@"navi_bg_h44"]];//使用图片 //使用自动滑动显隐代码,这个50,是指紧挨着导航栏的要跟随显隐的区域的高度,如果没有这个区域,可以调用没有这个参数的接口,一样的
[self followRollingScrollView:self.view secondAreaHeight:50];
根据滑动显隐状态栏的iOS实现的更多相关文章
- C#中显/隐式实现接口及其访问方法
原贴地址: http://www.cnblogs.com/dudu837/archive/2009/12/07/1618663.html 在实现接口的时候,VS提供了两个菜单,一个是"实现接 ...
- NavigationBar的显隐和颜色设置
[self.navigationController setNavigationBarHidden:NO animated:NO]; self.navigationController.navigat ...
- JS/jquery实现鼠标控制页面元素显隐
最近网站要上一个活动广告横幅,当用户鼠标划过时显隐二维码.像这种鼠标事件控制页面元素显隐的情况,码农们会经常遇到,可以通过javascript或jquery代码实现,下面就几种常见需求一起归纳一下. ...
- 盒子显隐,伪类边框,盒子阴影,2d平面形变
-盒子显隐 显隐的盒子尽量不影响其他盒子的布局 display:none; 消失的时候不占位置,显示的时候占位 opacity:0-1; 盒子透明度 overflow: hidden; 超出部分隐藏 ...
- day50 盒子显隐2D形变
复习 1.浮动布局 解决block盒子同行显示 => 不完全脱离文档流 => 不再撑开父级高度 脱离文档流: 不在页面中占位(显示层次高于文档流) 不完全: 可以通过清浮动操作, 让子级重 ...
- Python12/11--盒子的显隐/布局/z-index/流式布局思想
1.盒子的显隐 display:none 在页面中不占位,采用定位布局后,显示隐藏都不会影响其他标签,不需要用动画处理时,一般用这个 opacoity : 0 在页面中占位,采 ...
- jq svg 修改image的xmlns:xlink及图片的显隐
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 字体图标,盒子显隐,overflow属性,伪类设计边框,盒子阴影2d形变
字体图标 ''' fa框架: http://fontawesome.dashgame.com/ 下载 => 引入css文件 引入字体图标库 <link rel="styleshe ...
- 浏览器页面的显隐对js的setInterval()执行所产生的bug
前段时间,所写的一个”js无间隙滚动效果“,当页面离开后,重新返回时,会出现动画的错乱.我以为是因为我代码逻辑的原因导致的,但是,当在火狐浏览器上进行浏览时却没有动画错乱的问题. 于是乎,在网上查找是 ...
随机推荐
- 学习从零开始写jQuery框架
随着时代发展,javascript阵营里面出现了越来越多的优秀的框架,大大简化了我们的开发工作,在我们使用这些框架的时候是不是也应该饮水思源想想它们都是怎样构建起来的呢?如果你不满足于仅仅是使用一些现 ...
- 深入浅出设计模式——中介者模式(Mediator Pattern)
模式动机 在用户与用户直接聊天的设计方案中,用户对象之间存在很强的关联性,将导致系统出现如下问题: 系统结构复杂:对象之间存在大量的相互关联和调用,若有一个对象发生变化,则需要跟踪和该对象关联的其他 ...
- bootloader(转)
本文详细地介绍了基于嵌入式系统中的 OS 启动加载程序 ―― Boot Loader 的概念.软件设计的主要任务以及结构框架等内容. 1. 引言在专用的嵌入式板子运行 GNU/Linux 系统已经变得 ...
- NPM 使用介绍
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...
- oracle连接本地数据库
连接方式: 通过SQL Developer进行连接: 通过sql plus 进行连接: SQL Developer进行连接1.安装Oracle 11g会自带一个叫做SQL Developer的工具,它 ...
- 再说最后一次!关于不再更新SkySRS的理由!
再说最后一次!关于不再更新SkySRS的理由! https://www.itiankong.net/thread-195937-1-1.html Skyfree 发表于 2012-5-1 14:53: ...
- 带事物处理的DBHelp和sql语句
DBHelp语句 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- github文件上传及github pages博客搭建教程
一.与github建立连接 1.安装node.js和git 2.桌面新建文件夹[github],右键“git bash here” 3.注册github账号,新建仓库“new repository”, ...
- 三星在GPL下发布其exFAT文件系统实现源码
exFAT文件系统是微软的一个产品,设计让外置储存设备和PC之间实现无缝的TB级数据转移和数据交换,它只支持Windows和OS X,不支持Linux.作为一个含有大量专利的私有产品,没有人会预计它会 ...
- Question and Answer
1.VS2013使用EntityFrame问题解决办法 解决办法参照博客http://pinter.org/?p=2374 使用到EntityFrame的项目配置文件修改如下: 项目中凡是使用到DbC ...